watchdock-errors 0.5.1__tar.gz → 0.5.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {watchdock_errors-0.5.1/src/watchdock_errors.egg-info → watchdock_errors-0.5.2}/PKG-INFO +1 -1
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/celery.py +12 -46
- watchdock_errors-0.5.2/src/watchdock_errors/integrations/celery_schedule.py +63 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2/src/watchdock_errors.egg-info}/PKG-INFO +1 -1
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/SOURCES.txt +1 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/scm_file_list.json +1 -0
- watchdock_errors-0.5.2/src/watchdock_errors.egg-info/scm_version.json +8 -0
- watchdock_errors-0.5.2/tests/test_celery_schedule.py +54 -0
- watchdock_errors-0.5.1/src/watchdock_errors.egg-info/scm_version.json +0 -8
- watchdock_errors-0.5.1/tests/test_celery_schedule.py +0 -27
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/.github/workflows/ci.yml +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/.github/workflows/release.yml +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/.gitignore +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/README.md +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/pyproject.toml +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/setup.cfg +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/__init__.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/client.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/config.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/event.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/__init__.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/django.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/fastapi.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/utils.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/dependency_links.txt +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/requires.txt +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/top_level.txt +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/tests/test_client.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/tests/test_event.py +0 -0
- {watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/tests/test_utils.py +0 -0
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/celery.py
RENAMED
|
@@ -39,18 +39,19 @@ from __future__ import annotations
|
|
|
39
39
|
|
|
40
40
|
import logging
|
|
41
41
|
import threading
|
|
42
|
-
from datetime import timedelta
|
|
43
42
|
from typing import TYPE_CHECKING
|
|
44
43
|
|
|
45
44
|
import requests
|
|
46
45
|
|
|
46
|
+
from .celery_schedule import PeriodicTaskNames, periodic_schedules_by_task_name
|
|
47
|
+
|
|
47
48
|
if TYPE_CHECKING:
|
|
48
49
|
from celery import Celery
|
|
49
50
|
|
|
50
51
|
logger = logging.getLogger("watchdock_errors")
|
|
51
52
|
|
|
52
53
|
|
|
53
|
-
def register(app:
|
|
54
|
+
def register(app: Celery) -> None:
|
|
54
55
|
"""Wire up the Watchdock Celery integration on the given Celery app."""
|
|
55
56
|
try:
|
|
56
57
|
from celery.signals import task_failure, task_prerun, task_retry, task_revoked, worker_ready
|
|
@@ -58,7 +59,7 @@ def register(app: "Celery") -> None:
|
|
|
58
59
|
logger.warning("watchdock_errors: celery is not installed — Celery integration disabled")
|
|
59
60
|
return
|
|
60
61
|
|
|
61
|
-
periodic_task_names =
|
|
62
|
+
periodic_task_names = PeriodicTaskNames(app)
|
|
62
63
|
|
|
63
64
|
@worker_ready.connect(weak=False)
|
|
64
65
|
def _on_worker_ready(sender=None, **kwargs) -> None:
|
|
@@ -124,15 +125,13 @@ def register(app: "Celery") -> None:
|
|
|
124
125
|
)
|
|
125
126
|
|
|
126
127
|
|
|
127
|
-
def _report_task_registry(app:
|
|
128
|
+
def _report_task_registry(app: Celery) -> None:
|
|
128
129
|
config = _get_config_or_warn()
|
|
129
130
|
if config is None:
|
|
130
131
|
return
|
|
131
132
|
|
|
132
133
|
tasks = _collect_tasks(app)
|
|
133
|
-
threading.Thread(
|
|
134
|
-
target=_send_registry, args=(config, tasks), daemon=True, name="watchdock-celery-registry"
|
|
135
|
-
).start()
|
|
134
|
+
threading.Thread(target=_send_registry, args=(config, tasks), daemon=True, name="watchdock-celery-registry").start()
|
|
136
135
|
|
|
137
136
|
|
|
138
137
|
def _report_execution(
|
|
@@ -186,18 +185,8 @@ def _get_config_or_warn():
|
|
|
186
185
|
return config
|
|
187
186
|
|
|
188
187
|
|
|
189
|
-
def
|
|
190
|
-
|
|
191
|
-
schedules_by_task_name = {}
|
|
192
|
-
for entry in beat_schedule.values():
|
|
193
|
-
task_name = entry.get("task") if isinstance(entry, dict) else getattr(entry, "task", None)
|
|
194
|
-
if task_name:
|
|
195
|
-
schedules_by_task_name[task_name] = _serialize_schedule(entry)
|
|
196
|
-
return schedules_by_task_name
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def _collect_tasks(app: "Celery") -> list[dict]:
|
|
200
|
-
schedules_by_task_name = _periodic_schedules_by_task_name(app)
|
|
188
|
+
def _collect_tasks(app: Celery) -> list[dict]:
|
|
189
|
+
schedules_by_task_name = periodic_schedules_by_task_name(app)
|
|
201
190
|
|
|
202
191
|
tasks = []
|
|
203
192
|
for name, task in app.tasks.items():
|
|
@@ -216,32 +205,6 @@ def _collect_tasks(app: "Celery") -> list[dict]:
|
|
|
216
205
|
return tasks
|
|
217
206
|
|
|
218
207
|
|
|
219
|
-
def _serialize_schedule(entry) -> dict | None:
|
|
220
|
-
schedule = entry.get("schedule") if isinstance(entry, dict) else getattr(entry, "schedule", None)
|
|
221
|
-
if schedule is None:
|
|
222
|
-
return None
|
|
223
|
-
|
|
224
|
-
if hasattr(schedule, "_orig_minute"): # celery.schedules.crontab
|
|
225
|
-
return {
|
|
226
|
-
"type": "crontab",
|
|
227
|
-
"minute": schedule._orig_minute,
|
|
228
|
-
"hour": schedule._orig_hour,
|
|
229
|
-
"day_of_week": schedule._orig_day_of_week,
|
|
230
|
-
"day_of_month": schedule._orig_day_of_month,
|
|
231
|
-
"month_of_year": schedule._orig_month_of_year,
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
# timedelta.seconds is only the seconds component (a 1-day timedelta has 0), so use the total.
|
|
235
|
-
if isinstance(schedule, timedelta):
|
|
236
|
-
seconds = schedule.total_seconds()
|
|
237
|
-
else:
|
|
238
|
-
seconds = getattr(schedule, "seconds", schedule) # celery.schedules.schedule or plain number
|
|
239
|
-
try:
|
|
240
|
-
return {"type": "interval", "every_seconds": float(seconds)}
|
|
241
|
-
except (TypeError, ValueError):
|
|
242
|
-
return {"type": "unknown"}
|
|
243
|
-
|
|
244
|
-
|
|
245
208
|
def _send_registry(config, tasks: list[dict]) -> None:
|
|
246
209
|
if not tasks:
|
|
247
210
|
return
|
|
@@ -275,7 +238,10 @@ def _post(config, path: str, payload: dict, noun: str, count: int) -> None:
|
|
|
275
238
|
logger.info("watchdock_errors: reported %d celery %s(s) to %s", count, noun, path)
|
|
276
239
|
else:
|
|
277
240
|
logger.warning(
|
|
278
|
-
"watchdock_errors: celery %s rejected — status=%s body=%s",
|
|
241
|
+
"watchdock_errors: celery %s rejected — status=%s body=%s",
|
|
242
|
+
noun,
|
|
243
|
+
response.status_code,
|
|
244
|
+
response.text[:500],
|
|
279
245
|
)
|
|
280
246
|
except requests.exceptions.ConnectionError as e:
|
|
281
247
|
logger.error("watchdock_errors: connection failed reporting celery %s — %s", noun, e)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Reading the Celery beat schedule: which tasks are periodic, and how often they run."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from datetime import timedelta
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from celery import Celery
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class PeriodicTaskNames:
|
|
13
|
+
"""Task names in the beat schedule, read on first use rather than when register() runs.
|
|
14
|
+
|
|
15
|
+
Apps often set app.conf.beat_schedule after calling register(); reading it eagerly
|
|
16
|
+
left the set empty, so periodic tasks never sent a heartbeat and showed as never run.
|
|
17
|
+
Read once per process: prefork children start before worker_ready fires in the parent.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, app: Celery) -> None:
|
|
21
|
+
self._app = app
|
|
22
|
+
self._names: frozenset[str] | None = None
|
|
23
|
+
|
|
24
|
+
def __contains__(self, task_name: object) -> bool:
|
|
25
|
+
if self._names is None:
|
|
26
|
+
self._names = frozenset(periodic_schedules_by_task_name(self._app))
|
|
27
|
+
return task_name in self._names
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def periodic_schedules_by_task_name(app: Celery) -> dict:
|
|
31
|
+
beat_schedule = app.conf.beat_schedule or {}
|
|
32
|
+
schedules_by_task_name = {}
|
|
33
|
+
for entry in beat_schedule.values():
|
|
34
|
+
task_name = entry.get("task") if isinstance(entry, dict) else getattr(entry, "task", None)
|
|
35
|
+
if task_name:
|
|
36
|
+
schedules_by_task_name[task_name] = serialize_schedule(entry)
|
|
37
|
+
return schedules_by_task_name
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def serialize_schedule(entry) -> dict | None:
|
|
41
|
+
schedule = entry.get("schedule") if isinstance(entry, dict) else getattr(entry, "schedule", None)
|
|
42
|
+
if schedule is None:
|
|
43
|
+
return None
|
|
44
|
+
|
|
45
|
+
if hasattr(schedule, "_orig_minute"): # celery.schedules.crontab
|
|
46
|
+
return {
|
|
47
|
+
"type": "crontab",
|
|
48
|
+
"minute": schedule._orig_minute,
|
|
49
|
+
"hour": schedule._orig_hour,
|
|
50
|
+
"day_of_week": schedule._orig_day_of_week,
|
|
51
|
+
"day_of_month": schedule._orig_day_of_month,
|
|
52
|
+
"month_of_year": schedule._orig_month_of_year,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
# timedelta.seconds is only the seconds component (a 1-day timedelta has 0), so use the total.
|
|
56
|
+
if isinstance(schedule, timedelta):
|
|
57
|
+
seconds = schedule.total_seconds()
|
|
58
|
+
else:
|
|
59
|
+
seconds = getattr(schedule, "seconds", schedule) # celery.schedules.schedule or plain number
|
|
60
|
+
try:
|
|
61
|
+
return {"type": "interval", "every_seconds": float(seconds)}
|
|
62
|
+
except (TypeError, ValueError):
|
|
63
|
+
return {"type": "unknown"}
|
|
@@ -17,6 +17,7 @@ src/watchdock_errors.egg-info/scm_version.json
|
|
|
17
17
|
src/watchdock_errors.egg-info/top_level.txt
|
|
18
18
|
src/watchdock_errors/integrations/__init__.py
|
|
19
19
|
src/watchdock_errors/integrations/celery.py
|
|
20
|
+
src/watchdock_errors/integrations/celery_schedule.py
|
|
20
21
|
src/watchdock_errors/integrations/django.py
|
|
21
22
|
src/watchdock_errors/integrations/fastapi.py
|
|
22
23
|
tests/test_celery_schedule.py
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/scm_file_list.json
RENAMED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"src/watchdock_errors/event.py",
|
|
12
12
|
"src/watchdock_errors/integrations/__init__.py",
|
|
13
13
|
"src/watchdock_errors/integrations/celery.py",
|
|
14
|
+
"src/watchdock_errors/integrations/celery_schedule.py",
|
|
14
15
|
"src/watchdock_errors/integrations/django.py",
|
|
15
16
|
"src/watchdock_errors/integrations/fastapi.py",
|
|
16
17
|
"src/watchdock_errors/utils.py",
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from datetime import timedelta
|
|
2
|
+
from types import SimpleNamespace
|
|
3
|
+
|
|
4
|
+
from watchdock_errors.integrations.celery_schedule import PeriodicTaskNames, serialize_schedule
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _app_with_beat_schedule(beat_schedule: dict | None) -> SimpleNamespace:
|
|
8
|
+
return SimpleNamespace(conf=SimpleNamespace(beat_schedule=beat_schedule))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_number_is_an_interval():
|
|
12
|
+
assert serialize_schedule({"schedule": 300}) == {"type": "interval", "every_seconds": 300.0}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def test_timedelta_uses_total_seconds():
|
|
16
|
+
assert serialize_schedule({"schedule": timedelta(minutes=5)})["every_seconds"] == 300.0
|
|
17
|
+
assert serialize_schedule({"schedule": timedelta(days=1)})["every_seconds"] == 86400.0
|
|
18
|
+
assert serialize_schedule({"schedule": timedelta(days=1, hours=2)})["every_seconds"] == 93600.0
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_celery_schedule_object_uses_its_seconds():
|
|
22
|
+
assert serialize_schedule({"schedule": SimpleNamespace(seconds=86400.0)})["every_seconds"] == 86400.0
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_crontab_is_not_an_interval():
|
|
26
|
+
cron = SimpleNamespace(
|
|
27
|
+
_orig_minute="0", _orig_hour="3", _orig_day_of_week="*", _orig_day_of_month="*", _orig_month_of_year="*"
|
|
28
|
+
)
|
|
29
|
+
assert serialize_schedule({"schedule": cron})["type"] == "crontab"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def test_missing_schedule():
|
|
33
|
+
assert serialize_schedule({}) is None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_beat_schedule_set_after_register_still_gets_heartbeats():
|
|
37
|
+
# Shukran's celery.py calls register(app) and only then assigns app.conf.beat_schedule.
|
|
38
|
+
app = _app_with_beat_schedule(None)
|
|
39
|
+
periodic_task_names = PeriodicTaskNames(app)
|
|
40
|
+
|
|
41
|
+
app.conf.beat_schedule = {"hourly-balance": {"task": "mpesa.tasks.update_balance", "schedule": 3600}}
|
|
42
|
+
|
|
43
|
+
assert "mpesa.tasks.update_balance" in periodic_task_names
|
|
44
|
+
assert "mpesa.tasks.not_scheduled" not in periodic_task_names
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_beat_schedule_is_read_once_per_process():
|
|
48
|
+
app = _app_with_beat_schedule({"tick": {"task": "lab.tick", "schedule": 30}})
|
|
49
|
+
periodic_task_names = PeriodicTaskNames(app)
|
|
50
|
+
assert "lab.tick" in periodic_task_names
|
|
51
|
+
|
|
52
|
+
app.conf.beat_schedule = {}
|
|
53
|
+
|
|
54
|
+
assert "lab.tick" in periodic_task_names
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
from datetime import timedelta
|
|
2
|
-
from types import SimpleNamespace
|
|
3
|
-
|
|
4
|
-
from watchdock_errors.integrations.celery import _serialize_schedule
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_number_is_an_interval():
|
|
8
|
-
assert _serialize_schedule({"schedule": 300}) == {"type": "interval", "every_seconds": 300.0}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def test_timedelta_uses_total_seconds():
|
|
12
|
-
assert _serialize_schedule({"schedule": timedelta(minutes=5)})["every_seconds"] == 300.0
|
|
13
|
-
assert _serialize_schedule({"schedule": timedelta(days=1)})["every_seconds"] == 86400.0
|
|
14
|
-
assert _serialize_schedule({"schedule": timedelta(days=1, hours=2)})["every_seconds"] == 93600.0
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def test_celery_schedule_object_uses_its_seconds():
|
|
18
|
-
assert _serialize_schedule({"schedule": SimpleNamespace(seconds=86400.0)})["every_seconds"] == 86400.0
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def test_crontab_is_not_an_interval():
|
|
22
|
-
cron = SimpleNamespace(_orig_minute="0", _orig_hour="3", _orig_day_of_week="*", _orig_day_of_month="*", _orig_month_of_year="*")
|
|
23
|
-
assert _serialize_schedule({"schedule": cron})["type"] == "crontab"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def test_missing_schedule():
|
|
27
|
-
assert _serialize_schedule({}) is None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/__init__.py
RENAMED
|
File without changes
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/django.py
RENAMED
|
File without changes
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/fastapi.py
RENAMED
|
File without changes
|
|
File without changes
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/requires.txt
RENAMED
|
File without changes
|
{watchdock_errors-0.5.1 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|