-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcronitor.py
50 lines (40 loc) · 1.73 KB
/
cronitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from functools import wraps
import requests
from flask import current_app
def cronitor(task_name):
def decorator(func):
def ping_cronitor(command):
if not current_app.config["CRONITOR_ENABLED"]:
return
# it's useful to have a log that a periodic task has started in case it
# get stuck without generating any other logs - we know it got this far
current_app.logger.info("Pinging Cronitor for Celery task %s", task_name)
task_slug = current_app.config["CRONITOR_KEYS"].get(task_name)
if not task_slug:
current_app.logger.error("Cronitor enabled but task_name %s not found in environment", task_name)
return
if command not in {"run", "complete", "fail"}:
raise ValueError(f"command {command} not a valid cronitor command")
try:
resp = requests.get(
f"https://cronitor.link/{task_slug}/{command}",
# cronitor limits msg to 1000 characters
params={
"host": current_app.config["API_HOST_NAME"],
},
)
resp.raise_for_status()
except requests.RequestException as e:
current_app.logger.warning("Cronitor API failed for task %s due to %s", task_name, repr(e))
@wraps(func)
def inner_decorator(*args, **kwargs):
ping_cronitor("run")
status = "fail"
try:
ret = func(*args, **kwargs)
status = "complete"
return ret
finally:
ping_cronitor(status)
return inner_decorator
return decorator