2018-04-02 05:19:31 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2017-12-24 10:53:07 +00:00
|
|
|
|
import json
|
2016-11-13 14:34:38 +00:00
|
|
|
|
|
2020-12-09 08:27:04 +00:00
|
|
|
|
import redis_lock
|
|
|
|
|
import redis
|
2019-01-15 02:23:30 +00:00
|
|
|
|
from django.conf import settings
|
2019-02-24 13:34:45 +00:00
|
|
|
|
from django.utils.timezone import get_current_timezone
|
2017-12-25 17:54:10 +00:00
|
|
|
|
from django.db.utils import ProgrammingError, OperationalError
|
2019-09-18 14:06:46 +00:00
|
|
|
|
from django_celery_beat.models import (
|
|
|
|
|
PeriodicTask, IntervalSchedule, CrontabSchedule, PeriodicTasks
|
|
|
|
|
)
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from common.utils.timezone import local_now
|
2019-12-05 07:09:25 +00:00
|
|
|
|
from common.utils import get_logger
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
def create_or_update_celery_periodic_tasks(tasks):
|
|
|
|
|
"""
|
|
|
|
|
:param tasks: {
|
|
|
|
|
'add-every-monday-morning': {
|
|
|
|
|
'task': 'tasks.add' # A registered celery task,
|
|
|
|
|
'interval': 30,
|
|
|
|
|
'crontab': "30 7 * * *",
|
|
|
|
|
'args': (16, 16),
|
|
|
|
|
'kwargs': {},
|
|
|
|
|
'enabled': False,
|
2019-12-05 07:09:25 +00:00
|
|
|
|
'description': ''
|
2017-12-24 10:53:07 +00:00
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
# Todo: check task valid, task and callback must be a celery task
|
|
|
|
|
for name, detail in tasks.items():
|
|
|
|
|
interval = None
|
|
|
|
|
crontab = None
|
2021-07-01 02:42:49 +00:00
|
|
|
|
last_run_at = None
|
|
|
|
|
|
2017-12-24 15:21:05 +00:00
|
|
|
|
try:
|
|
|
|
|
IntervalSchedule.objects.all().count()
|
2017-12-25 17:54:10 +00:00
|
|
|
|
except (ProgrammingError, OperationalError):
|
2017-12-24 15:21:05 +00:00
|
|
|
|
return None
|
|
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
|
if isinstance(detail.get("interval"), int):
|
2019-12-05 07:09:25 +00:00
|
|
|
|
kwargs = dict(
|
|
|
|
|
every=detail['interval'],
|
|
|
|
|
period=IntervalSchedule.SECONDS,
|
2017-12-24 10:53:07 +00:00
|
|
|
|
)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
# 不能使用 get_or_create,因为可能会有多个
|
|
|
|
|
interval = IntervalSchedule.objects.filter(**kwargs).first()
|
|
|
|
|
if interval is None:
|
|
|
|
|
interval = IntervalSchedule.objects.create(**kwargs)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
last_run_at = local_now()
|
2017-12-24 10:53:07 +00:00
|
|
|
|
elif isinstance(detail.get("crontab"), str):
|
|
|
|
|
try:
|
|
|
|
|
minute, hour, day, month, week = detail["crontab"].split()
|
|
|
|
|
except ValueError:
|
2019-12-05 07:09:25 +00:00
|
|
|
|
logger.error("crontab is not valid")
|
|
|
|
|
return
|
2017-12-24 10:53:07 +00:00
|
|
|
|
kwargs = dict(
|
|
|
|
|
minute=minute, hour=hour, day_of_week=week,
|
2019-02-24 13:34:45 +00:00
|
|
|
|
day_of_month=day, month_of_year=month, timezone=get_current_timezone()
|
2017-12-24 10:53:07 +00:00
|
|
|
|
)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
crontab = CrontabSchedule.objects.filter(**kwargs).first()
|
|
|
|
|
if crontab is None:
|
2017-12-24 10:53:07 +00:00
|
|
|
|
crontab = CrontabSchedule.objects.create(**kwargs)
|
|
|
|
|
else:
|
2019-12-05 07:09:25 +00:00
|
|
|
|
logger.error("Schedule is not valid")
|
|
|
|
|
return
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
defaults = dict(
|
|
|
|
|
interval=interval,
|
|
|
|
|
crontab=crontab,
|
|
|
|
|
name=name,
|
|
|
|
|
task=detail['task'],
|
|
|
|
|
args=json.dumps(detail.get('args', [])),
|
|
|
|
|
kwargs=json.dumps(detail.get('kwargs', {})),
|
2021-07-01 02:42:49 +00:00
|
|
|
|
description=detail.get('description') or '',
|
|
|
|
|
last_run_at=last_run_at,
|
2017-12-24 10:53:07 +00:00
|
|
|
|
)
|
|
|
|
|
task = PeriodicTask.objects.update_or_create(
|
|
|
|
|
defaults=defaults, name=name,
|
|
|
|
|
)
|
2019-09-18 14:06:46 +00:00
|
|
|
|
PeriodicTasks.update_changed()
|
2017-12-24 10:53:07 +00:00
|
|
|
|
return task
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def disable_celery_periodic_task(task_name):
|
|
|
|
|
from django_celery_beat.models import PeriodicTask
|
|
|
|
|
PeriodicTask.objects.filter(name=task_name).update(enabled=False)
|
2019-09-18 14:06:46 +00:00
|
|
|
|
PeriodicTasks.update_changed()
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_celery_periodic_task(task_name):
|
|
|
|
|
from django_celery_beat.models import PeriodicTask
|
|
|
|
|
PeriodicTask.objects.filter(name=task_name).delete()
|
2019-09-18 14:06:46 +00:00
|
|
|
|
PeriodicTasks.update_changed()
|
2017-12-24 10:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
2020-07-21 07:33:33 +00:00
|
|
|
|
def get_celery_periodic_task(task_name):
|
|
|
|
|
from django_celery_beat.models import PeriodicTask
|
|
|
|
|
task = PeriodicTask.objects.filter(name=task_name).first()
|
|
|
|
|
return task
|
|
|
|
|
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
|
def get_celery_task_log_path(task_id):
|
2020-12-29 13:05:18 +00:00
|
|
|
|
from ops.utils import get_task_log_path
|
|
|
|
|
return get_task_log_path(settings.CELERY_LOG_DIR, task_id)
|
2020-12-09 08:27:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_celery_status():
|
|
|
|
|
from . import app
|
|
|
|
|
i = app.control.inspect()
|
|
|
|
|
ping_data = i.ping() or {}
|
|
|
|
|
active_nodes = [k for k, v in ping_data.items() if v.get('ok') == 'pong']
|
|
|
|
|
active_queue_worker = set([n.split('@')[0] for n in active_nodes if n])
|
2021-03-12 06:09:54 +00:00
|
|
|
|
# Celery Worker 数量: 2
|
|
|
|
|
if len(active_queue_worker) < 2:
|
2020-12-09 08:27:04 +00:00
|
|
|
|
print("Not all celery worker worked")
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_beat_status():
|
|
|
|
|
CONFIG = settings.CONFIG
|
|
|
|
|
r = redis.Redis(host=CONFIG.REDIS_HOST, port=CONFIG.REDIS_PORT, password=CONFIG.REDIS_PASSWORD)
|
|
|
|
|
lock = redis_lock.Lock(r, name="beat-distribute-start-lock")
|
|
|
|
|
try:
|
|
|
|
|
locked = lock.locked()
|
|
|
|
|
return locked
|
|
|
|
|
except redis.ConnectionError:
|
|
|
|
|
return False
|