2017-03-09 06:55:33 +00:00
|
|
|
# coding: utf-8
|
2019-01-08 03:15:09 +00:00
|
|
|
import os
|
2022-10-27 11:23:15 +00:00
|
|
|
import random
|
2019-01-15 11:01:33 +00:00
|
|
|
import subprocess
|
2019-01-08 03:15:09 +00:00
|
|
|
|
2019-01-15 11:01:33 +00:00
|
|
|
from django.conf import settings
|
2017-12-22 13:42:12 +00:00
|
|
|
from celery import shared_task, subtask
|
2022-09-29 12:44:45 +00:00
|
|
|
from celery import signals
|
2022-01-13 06:18:32 +00:00
|
|
|
|
2019-03-27 03:12:34 +00:00
|
|
|
from celery.exceptions import SoftTimeLimitExceeded
|
2019-01-08 03:15:09 +00:00
|
|
|
from django.utils import timezone
|
2022-01-13 06:18:32 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _, gettext
|
2017-12-22 13:42:12 +00:00
|
|
|
|
2021-07-30 07:42:06 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none, get_log_keep_day
|
2020-03-18 06:02:36 +00:00
|
|
|
from orgs.utils import tmp_to_root_org, tmp_to_org
|
2019-01-15 02:23:30 +00:00
|
|
|
from .celery.decorator import (
|
|
|
|
register_as_period_task, after_app_shutdown_clean_periodic,
|
|
|
|
after_app_ready_start
|
|
|
|
)
|
2020-07-21 07:33:33 +00:00
|
|
|
from .celery.utils import (
|
|
|
|
create_or_update_celery_periodic_tasks, get_celery_periodic_task,
|
|
|
|
disable_celery_periodic_task, delete_celery_periodic_task
|
|
|
|
)
|
2022-10-24 12:14:18 +00:00
|
|
|
from .models import CeleryTaskExecution, AdHoc, Playbook
|
2021-07-30 07:42:06 +00:00
|
|
|
from .notifications import ServerPerformanceCheckUtil
|
2017-12-22 13:42:12 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
def rerun_task():
|
|
|
|
pass
|
2017-03-09 06:55:33 +00:00
|
|
|
|
|
|
|
|
2022-10-08 11:12:04 +00:00
|
|
|
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task"))
|
|
|
|
def run_adhoc(tid, **kwargs):
|
2017-12-22 13:42:12 +00:00
|
|
|
"""
|
2018-04-01 15:45:37 +00:00
|
|
|
:param tid: is the tasks serialized data
|
2017-12-22 13:42:12 +00:00
|
|
|
:param callback: callback function name
|
|
|
|
:return:
|
|
|
|
"""
|
2020-03-18 05:45:49 +00:00
|
|
|
with tmp_to_root_org():
|
2022-10-08 11:12:04 +00:00
|
|
|
task = get_object_or_none(AdHoc, id=tid)
|
2020-03-18 06:02:36 +00:00
|
|
|
if not task:
|
|
|
|
logger.error("No task found")
|
|
|
|
return
|
|
|
|
with tmp_to_org(task.org):
|
2022-10-08 11:12:04 +00:00
|
|
|
execution = task.create_execution()
|
|
|
|
try:
|
|
|
|
execution.start(**kwargs)
|
|
|
|
except SoftTimeLimitExceeded:
|
|
|
|
execution.set_error('Run timeout')
|
|
|
|
logger.error("Run adhoc timeout")
|
|
|
|
except Exception as e:
|
|
|
|
execution.set_error(e)
|
|
|
|
logger.error("Start adhoc execution error: {}".format(e))
|
2017-12-22 13:42:12 +00:00
|
|
|
|
|
|
|
|
2022-09-29 12:44:45 +00:00
|
|
|
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible command"))
|
2022-10-08 11:12:04 +00:00
|
|
|
def run_playbook(pid, **kwargs):
|
2020-03-16 11:24:48 +00:00
|
|
|
with tmp_to_root_org():
|
2022-10-08 11:12:04 +00:00
|
|
|
task = get_object_or_none(Playbook, id=pid)
|
|
|
|
if not task:
|
|
|
|
logger.error("No task found")
|
2020-03-18 06:02:36 +00:00
|
|
|
return
|
2022-10-08 11:12:04 +00:00
|
|
|
|
|
|
|
with tmp_to_org(task.org):
|
|
|
|
execution = task.create_execution()
|
2019-03-27 03:12:34 +00:00
|
|
|
try:
|
2022-10-08 11:12:04 +00:00
|
|
|
execution.start(**kwargs)
|
2019-03-27 03:12:34 +00:00
|
|
|
except SoftTimeLimitExceeded:
|
2022-10-08 11:12:04 +00:00
|
|
|
execution.set_error('Run timeout')
|
|
|
|
logger.error("Run playbook timeout")
|
|
|
|
except Exception as e:
|
|
|
|
execution.set_error(e)
|
|
|
|
logger.error("Run playbook execution error: {}".format(e))
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
|
2018-12-20 03:05:36 +00:00
|
|
|
@shared_task
|
2019-01-15 02:23:30 +00:00
|
|
|
@after_app_shutdown_clean_periodic
|
2022-10-27 11:23:15 +00:00
|
|
|
@register_as_period_task(interval=3600 * 24, description=_("Clean task history period"))
|
2018-12-20 03:05:36 +00:00
|
|
|
def clean_tasks_adhoc_period():
|
|
|
|
logger.debug("Start clean task adhoc and run history")
|
|
|
|
tasks = Task.objects.all()
|
|
|
|
for task in tasks:
|
|
|
|
adhoc = task.adhoc.all().order_by('-date_created')[5:]
|
|
|
|
for ad in adhoc:
|
2020-03-12 08:24:38 +00:00
|
|
|
ad.execution.all().delete()
|
2018-12-20 03:05:36 +00:00
|
|
|
ad.delete()
|
|
|
|
|
|
|
|
|
2019-01-08 03:15:09 +00:00
|
|
|
@shared_task
|
2019-01-15 02:23:30 +00:00
|
|
|
@after_app_shutdown_clean_periodic
|
2022-10-27 11:23:15 +00:00
|
|
|
@register_as_period_task(interval=3600 * 24, description=_("Clean celery log period"))
|
2019-01-08 03:15:09 +00:00
|
|
|
def clean_celery_tasks_period():
|
|
|
|
logger.debug("Start clean celery task history")
|
2020-10-26 06:13:20 +00:00
|
|
|
expire_days = get_log_keep_day('TASK_LOG_KEEP_DAYS')
|
|
|
|
days_ago = timezone.now() - timezone.timedelta(days=expire_days)
|
2022-10-24 12:14:18 +00:00
|
|
|
tasks = CeleryTaskExecution.objects.filter(date_start__lt=days_ago)
|
2020-03-12 08:24:38 +00:00
|
|
|
tasks.delete()
|
2022-10-24 12:14:18 +00:00
|
|
|
tasks = CeleryTaskExecution.objects.filter(date_start__isnull=True)
|
2019-01-08 03:15:09 +00:00
|
|
|
tasks.delete()
|
2019-01-15 11:01:33 +00:00
|
|
|
command = "find %s -mtime +%s -name '*.log' -type f -exec rm -f {} \\;" % (
|
|
|
|
settings.CELERY_LOG_DIR, expire_days
|
|
|
|
)
|
|
|
|
subprocess.call(command, shell=True)
|
2019-03-18 03:34:13 +00:00
|
|
|
command = "echo > {}".format(os.path.join(settings.LOG_DIR, 'celery.log'))
|
|
|
|
subprocess.call(command, shell=True)
|
2019-01-08 03:15:09 +00:00
|
|
|
|
|
|
|
|
2020-07-21 07:33:33 +00:00
|
|
|
@shared_task
|
|
|
|
@after_app_ready_start
|
|
|
|
def clean_celery_periodic_tasks():
|
|
|
|
"""清除celery定时任务"""
|
|
|
|
need_cleaned_tasks = [
|
|
|
|
'handle_be_interrupted_change_auth_task_periodic',
|
|
|
|
]
|
|
|
|
logger.info('Start clean celery periodic tasks: {}'.format(need_cleaned_tasks))
|
|
|
|
for task_name in need_cleaned_tasks:
|
|
|
|
logger.info('Start clean task: {}'.format(task_name))
|
|
|
|
task = get_celery_periodic_task(task_name)
|
|
|
|
if task is None:
|
|
|
|
logger.info('Task does not exist: {}'.format(task_name))
|
|
|
|
continue
|
|
|
|
disable_celery_periodic_task(task_name)
|
|
|
|
delete_celery_periodic_task(task_name)
|
|
|
|
task = get_celery_periodic_task(task_name)
|
|
|
|
if task is None:
|
|
|
|
logger.info('Clean task success: {}'.format(task_name))
|
|
|
|
else:
|
|
|
|
logger.info('Clean task failure: {}'.format(task))
|
|
|
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
@shared_task
|
|
|
|
@after_app_ready_start
|
|
|
|
def create_or_update_registered_periodic_tasks():
|
|
|
|
from .celery.decorator import get_register_period_tasks
|
|
|
|
for task in get_register_period_tasks():
|
|
|
|
create_or_update_celery_periodic_tasks(task)
|
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
@shared_task
|
|
|
|
@register_as_period_task(interval=3600)
|
|
|
|
def check_server_performance_period():
|
2021-07-30 07:42:06 +00:00
|
|
|
ServerPerformanceCheckUtil().check_and_publish()
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
2022-10-27 11:23:15 +00:00
|
|
|
@shared_task(queue="ansible", verbose_name=_("Hello"), comment="an test shared task")
|
2017-12-22 13:42:12 +00:00
|
|
|
def hello(name, callback=None):
|
2021-11-18 10:47:01 +00:00
|
|
|
from users.models import User
|
2019-01-15 02:23:30 +00:00
|
|
|
import time
|
2021-11-18 10:47:01 +00:00
|
|
|
|
|
|
|
count = User.objects.count()
|
2022-01-13 06:18:32 +00:00
|
|
|
print(gettext("Hello") + ': ' + name)
|
2021-11-18 10:47:01 +00:00
|
|
|
print("Count: ", count)
|
|
|
|
time.sleep(1)
|
2022-01-13 06:18:32 +00:00
|
|
|
return gettext("Hello")
|
2017-12-22 13:42:12 +00:00
|
|
|
|
|
|
|
|
2022-10-27 11:23:15 +00:00
|
|
|
@shared_task(verbose_name="Hello Error", comment="an test shared task error")
|
|
|
|
def hello_error():
|
|
|
|
raise Exception("must be error")
|
|
|
|
|
|
|
|
|
|
|
|
@shared_task(verbose_name="Hello Random", comment="some time error and some time success")
|
|
|
|
def hello_random():
|
|
|
|
i = random.randint(0, 1)
|
|
|
|
if i == 1:
|
|
|
|
raise Exception("must be error")
|
|
|
|
|
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
@shared_task
|
|
|
|
def hello_callback(result):
|
|
|
|
print(result)
|
|
|
|
print("Hello callback")
|