2017-03-09 06:55:33 +00:00
|
|
|
# coding: utf-8
|
2019-01-08 03:15:09 +00:00
|
|
|
import os
|
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
|
2022-11-15 08:29:40 +00:00
|
|
|
from celery import shared_task
|
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-11-15 08:29:40 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
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
|
2022-11-23 16:50:37 +00:00
|
|
|
from orgs.utils import 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-11-15 08:29:40 +00:00
|
|
|
from .models import CeleryTaskExecution, Job, JobExecution
|
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
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task"))
|
2022-11-18 10:46:48 +00:00
|
|
|
def run_ops_job(job_id):
|
2022-11-11 11:20:17 +00:00
|
|
|
job = get_object_or_none(Job, id=job_id)
|
2022-12-06 09:30:54 +00:00
|
|
|
execution = job.create_execution()
|
|
|
|
run_ops_job_execution(execution)
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task(soft_time_limit=60, queue="ansible", verbose_name=_("Run ansible task execution"))
|
2022-11-29 11:44:12 +00:00
|
|
|
def run_ops_job_execution(execution_id, **kwargs):
|
2022-11-11 11:20:17 +00:00
|
|
|
execution = get_object_or_none(JobExecution, id=execution_id)
|
2022-12-06 09:30:54 +00:00
|
|
|
try:
|
2022-12-15 09:25:21 +00:00
|
|
|
with tmp_to_org(execution.org):
|
|
|
|
execution.start()
|
2022-12-06 09:30:54 +00:00
|
|
|
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))
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
@shared_task(verbose_name=_('Periodic clear celery tasks'))
|
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
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
@shared_task(verbose_name=_('Clear celery periodic tasks'))
|
2020-07-21 07:33:33 +00:00
|
|
|
@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))
|
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
@shared_task(verbose_name=_('Create or update periodic tasks'))
|
2019-01-15 02:23:30 +00:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
@shared_task(verbose_name=_("Periodic check service performance"))
|
2019-12-05 07:09:25 +00:00
|
|
|
@register_as_period_task(interval=3600)
|
|
|
|
def check_server_performance_period():
|
2021-07-30 07:42:06 +00:00
|
|
|
ServerPerformanceCheckUtil().check_and_publish()
|