watchdock-errors 0.5.0__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.0/src/watchdock_errors.egg-info → watchdock_errors-0.5.2}/PKG-INFO +1 -1
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/celery.py +12 -41
- watchdock_errors-0.5.2/src/watchdock_errors/integrations/celery_schedule.py +63 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2/src/watchdock_errors.egg-info}/PKG-INFO +1 -1
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/SOURCES.txt +2 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/scm_file_list.json +2 -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.0/src/watchdock_errors.egg-info/scm_version.json +0 -8
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/.github/workflows/ci.yml +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/.github/workflows/release.yml +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/.gitignore +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/README.md +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/pyproject.toml +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/setup.cfg +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/__init__.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/client.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/config.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/event.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/__init__.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/django.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/fastapi.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/utils.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/dependency_links.txt +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/requires.txt +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/top_level.txt +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/tests/test_client.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/tests/test_event.py +0 -0
- {watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/tests/test_utils.py +0 -0
{watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/celery.py
RENAMED
|
@@ -43,13 +43,15 @@ from typing import TYPE_CHECKING
|
|
|
43
43
|
|
|
44
44
|
import requests
|
|
45
45
|
|
|
46
|
+
from .celery_schedule import PeriodicTaskNames, periodic_schedules_by_task_name
|
|
47
|
+
|
|
46
48
|
if TYPE_CHECKING:
|
|
47
49
|
from celery import Celery
|
|
48
50
|
|
|
49
51
|
logger = logging.getLogger("watchdock_errors")
|
|
50
52
|
|
|
51
53
|
|
|
52
|
-
def register(app:
|
|
54
|
+
def register(app: Celery) -> None:
|
|
53
55
|
"""Wire up the Watchdock Celery integration on the given Celery app."""
|
|
54
56
|
try:
|
|
55
57
|
from celery.signals import task_failure, task_prerun, task_retry, task_revoked, worker_ready
|
|
@@ -57,7 +59,7 @@ def register(app: "Celery") -> None:
|
|
|
57
59
|
logger.warning("watchdock_errors: celery is not installed — Celery integration disabled")
|
|
58
60
|
return
|
|
59
61
|
|
|
60
|
-
periodic_task_names =
|
|
62
|
+
periodic_task_names = PeriodicTaskNames(app)
|
|
61
63
|
|
|
62
64
|
@worker_ready.connect(weak=False)
|
|
63
65
|
def _on_worker_ready(sender=None, **kwargs) -> None:
|
|
@@ -123,15 +125,13 @@ def register(app: "Celery") -> None:
|
|
|
123
125
|
)
|
|
124
126
|
|
|
125
127
|
|
|
126
|
-
def _report_task_registry(app:
|
|
128
|
+
def _report_task_registry(app: Celery) -> None:
|
|
127
129
|
config = _get_config_or_warn()
|
|
128
130
|
if config is None:
|
|
129
131
|
return
|
|
130
132
|
|
|
131
133
|
tasks = _collect_tasks(app)
|
|
132
|
-
threading.Thread(
|
|
133
|
-
target=_send_registry, args=(config, tasks), daemon=True, name="watchdock-celery-registry"
|
|
134
|
-
).start()
|
|
134
|
+
threading.Thread(target=_send_registry, args=(config, tasks), daemon=True, name="watchdock-celery-registry").start()
|
|
135
135
|
|
|
136
136
|
|
|
137
137
|
def _report_execution(
|
|
@@ -185,18 +185,8 @@ def _get_config_or_warn():
|
|
|
185
185
|
return config
|
|
186
186
|
|
|
187
187
|
|
|
188
|
-
def
|
|
189
|
-
|
|
190
|
-
schedules_by_task_name = {}
|
|
191
|
-
for entry in beat_schedule.values():
|
|
192
|
-
task_name = entry.get("task") if isinstance(entry, dict) else getattr(entry, "task", None)
|
|
193
|
-
if task_name:
|
|
194
|
-
schedules_by_task_name[task_name] = _serialize_schedule(entry)
|
|
195
|
-
return schedules_by_task_name
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
def _collect_tasks(app: "Celery") -> list[dict]:
|
|
199
|
-
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)
|
|
200
190
|
|
|
201
191
|
tasks = []
|
|
202
192
|
for name, task in app.tasks.items():
|
|
@@ -215,28 +205,6 @@ def _collect_tasks(app: "Celery") -> list[dict]:
|
|
|
215
205
|
return tasks
|
|
216
206
|
|
|
217
207
|
|
|
218
|
-
def _serialize_schedule(entry) -> dict | None:
|
|
219
|
-
schedule = entry.get("schedule") if isinstance(entry, dict) else getattr(entry, "schedule", None)
|
|
220
|
-
if schedule is None:
|
|
221
|
-
return None
|
|
222
|
-
|
|
223
|
-
if hasattr(schedule, "_orig_minute"): # celery.schedules.crontab
|
|
224
|
-
return {
|
|
225
|
-
"type": "crontab",
|
|
226
|
-
"minute": schedule._orig_minute,
|
|
227
|
-
"hour": schedule._orig_hour,
|
|
228
|
-
"day_of_week": schedule._orig_day_of_week,
|
|
229
|
-
"day_of_month": schedule._orig_day_of_month,
|
|
230
|
-
"month_of_year": schedule._orig_month_of_year,
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
seconds = getattr(schedule, "seconds", schedule) # celery.schedules.schedule or plain number
|
|
234
|
-
try:
|
|
235
|
-
return {"type": "interval", "every_seconds": float(seconds)}
|
|
236
|
-
except (TypeError, ValueError):
|
|
237
|
-
return {"type": "unknown"}
|
|
238
|
-
|
|
239
|
-
|
|
240
208
|
def _send_registry(config, tasks: list[dict]) -> None:
|
|
241
209
|
if not tasks:
|
|
242
210
|
return
|
|
@@ -270,7 +238,10 @@ def _post(config, path: str, payload: dict, noun: str, count: int) -> None:
|
|
|
270
238
|
logger.info("watchdock_errors: reported %d celery %s(s) to %s", count, noun, path)
|
|
271
239
|
else:
|
|
272
240
|
logger.warning(
|
|
273
|
-
"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],
|
|
274
245
|
)
|
|
275
246
|
except requests.exceptions.ConnectionError as e:
|
|
276
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,8 +17,10 @@ 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
|
|
23
|
+
tests/test_celery_schedule.py
|
|
22
24
|
tests/test_client.py
|
|
23
25
|
tests/test_event.py
|
|
24
26
|
tests/test_utils.py
|
{watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/scm_file_list.json
RENAMED
|
@@ -11,9 +11,11 @@
|
|
|
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",
|
|
18
|
+
"tests/test_celery_schedule.py",
|
|
17
19
|
"tests/test_client.py",
|
|
18
20
|
"tests/test_event.py",
|
|
19
21
|
"tests/test_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
|
|
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.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/__init__.py
RENAMED
|
File without changes
|
{watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/django.py
RENAMED
|
File without changes
|
{watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors/integrations/fastapi.py
RENAMED
|
File without changes
|
|
File without changes
|
{watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
{watchdock_errors-0.5.0 → watchdock_errors-0.5.2}/src/watchdock_errors.egg-info/requires.txt
RENAMED
|
File without changes
|
{watchdock_errors-0.5.0 → 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
|