-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcelerymon.py
executable file
·54 lines (46 loc) · 1.86 KB
/
celerymon.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
51
52
53
54
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'celerymonitor.settings'
import django
from django.conf import settings
django.setup()
from djcelery.snapshot import Camera
from celery.utils.log import get_logger
from celery import Celery
import traceback
logger = get_logger(__name__)
import datetime
from djcelery.utils import DATABASE_ERRORS
class CeleryprojMonitorCamera(Camera):
"""
This function is aim to fix the bug when database encounter server domain name exchange, the monitor process
can't handle database lost exception.
"""
def on_shutter(self, state, commit_every=5):
def _handle_tasks():
for i, task in enumerate(state.tasks.items()):
self.handle_task(task)
try:
for worker in state.workers.items():
self.handle_worker(worker)
except DATABASE_ERRORS:
print ('database error',datetime.datetime.now())
logger.error(traceback.print_exc())
from django.db import connection
connection.close()
try:
_handle_tasks()
except DATABASE_ERRORS:
print({'subject':'Mysql database encounter exchange','detail':str(traceback.print_exc())})
print (datetime.datetime.now())
logger.error(traceback.print_exc())
from django.db import connection
connection.close()
def main(app, freq=1.0):
state = app.events.State()
with app.connection() as connection:
recv = app.events.Receiver(connection, handlers={'*': state.event})
with CeleryprojMonitorCamera(state, freq=freq):
recv.capture(limit=None, timeout=None)
if __name__ == '__main__':
app = Celery(broker=getattr(settings,'BROKER_URL'))
main(app)