perf: 添加 celery 健康检测

pull/9235/head
ibuler 2022-12-22 13:58:29 +08:00
parent a377317d6e
commit 2e91aa8ce5
2 changed files with 28 additions and 1 deletions

View File

@ -2,12 +2,14 @@
import os
from kombu import Exchange, Queue
from celery import Celery
from kombu import Exchange, Queue
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jumpserver.settings')
from jumpserver import settings
from .heatbeat import *
# from django.conf import settings
app = Celery('jumpserver')

View File

@ -0,0 +1,25 @@
from pathlib import Path
from celery.signals import heartbeat_sent, worker_ready, worker_shutdown
@heartbeat_sent.connect
def heartbeat(sender, **kwargs):
worker_name = sender.eventer.hostname.split('@')[0]
heartbeat_path = Path('/tmp/worker_heartbeat_{}'.format(worker_name))
heartbeat_path.touch()
@worker_ready.connect
def worker_ready(sender, **kwargs):
worker_name = sender.hostname.split('@')[0]
ready_path = Path('/tmp/worker_ready_{}'.format(worker_name))
ready_path.touch()
@worker_shutdown.connect
def worker_shutdown(sender, **kwargs):
worker_name = sender.hostname.split('@')[0]
for signal in ['ready', 'heartbeat']:
path = Path('/tmp/worker_{}_{}'.format(signal, worker_name))
path.unlink(missing_ok=True)