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-09-18 14:06:46 +00:00
|
|
|
import time
|
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
|
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
|
2019-12-05 07:09:25 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-12-22 13:42:12 +00:00
|
|
|
|
2020-10-26 06:13:20 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none, get_disk_usage, 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
|
|
|
|
)
|
2019-01-08 03:15:09 +00:00
|
|
|
from .models import Task, CommandExecution, CeleryTask
|
2019-12-05 07:09:25 +00:00
|
|
|
from .utils import send_server_performance_mail
|
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
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
@shared_task(queue="ansible")
|
2018-04-01 15:45:37 +00:00
|
|
|
def run_ansible_task(tid, callback=None, **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():
|
|
|
|
task = get_object_or_none(Task, 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):
|
2017-12-24 10:53:07 +00:00
|
|
|
result = task.run()
|
2017-12-22 13:42:12 +00:00
|
|
|
if callback is not None:
|
2017-12-24 10:53:07 +00:00
|
|
|
subtask(callback).delay(result, task_name=task.name)
|
2017-12-22 13:42:12 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2019-09-12 03:17:15 +00:00
|
|
|
@shared_task(soft_time_limit=60, queue="ansible")
|
2018-12-10 02:11:54 +00:00
|
|
|
def run_command_execution(cid, **kwargs):
|
2020-03-16 11:24:48 +00:00
|
|
|
with tmp_to_root_org():
|
|
|
|
execution = get_object_or_none(CommandExecution, id=cid)
|
2020-03-18 06:02:36 +00:00
|
|
|
if not execution:
|
|
|
|
logger.error("Not found the execution id: {}".format(cid))
|
|
|
|
return
|
|
|
|
with tmp_to_org(execution.run_as.org):
|
2019-03-27 03:12:34 +00:00
|
|
|
try:
|
2019-08-21 12:27:21 +00:00
|
|
|
os.environ.update({
|
|
|
|
"TERM_ROWS": kwargs.get("rows", ""),
|
|
|
|
"TERM_COLS": kwargs.get("cols", ""),
|
|
|
|
})
|
2019-03-27 03:12:34 +00:00
|
|
|
execution.run()
|
|
|
|
except SoftTimeLimitExceeded:
|
2019-07-04 13:17:39 +00:00
|
|
|
logger.error("Run time out")
|
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
|
2019-12-05 07:09:25 +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
|
2019-12-05 07:09:25 +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)
|
|
|
|
tasks = CeleryTask.objects.filter(date_start__lt=days_ago)
|
2020-03-12 08:24:38 +00:00
|
|
|
tasks.delete()
|
2019-01-08 03:15:09 +00:00
|
|
|
tasks = CeleryTask.objects.filter(date_start__isnull=True)
|
|
|
|
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-02-18 03:13:55 +00:00
|
|
|
if not settings.DISK_CHECK_ENABLED:
|
|
|
|
return
|
2019-12-05 07:09:25 +00:00
|
|
|
usages = get_disk_usage()
|
2020-03-12 08:24:38 +00:00
|
|
|
uncheck_paths = ['/etc', '/boot']
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
for path, usage in usages.items():
|
2020-03-12 08:24:38 +00:00
|
|
|
need_check = True
|
|
|
|
for uncheck_path in uncheck_paths:
|
|
|
|
if path.startswith(uncheck_path):
|
|
|
|
need_check = False
|
|
|
|
if need_check and usage.percent > 80:
|
2019-12-05 07:09:25 +00:00
|
|
|
send_server_performance_mail(path, usage, usages)
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
@shared_task(queue="ansible")
|
2017-12-22 13:42:12 +00:00
|
|
|
def hello(name, callback=None):
|
2019-01-15 02:23:30 +00:00
|
|
|
import time
|
|
|
|
time.sleep(10)
|
2017-12-22 13:42:12 +00:00
|
|
|
print("Hello {}".format(name))
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
@shared_task
|
2019-06-19 11:25:21 +00:00
|
|
|
# @after_app_shutdown_clean_periodic
|
|
|
|
# @register_as_period_task(interval=30)
|
2019-06-19 03:31:38 +00:00
|
|
|
def hello123():
|
2019-08-21 12:27:21 +00:00
|
|
|
return None
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
@shared_task
|
|
|
|
def hello_callback(result):
|
|
|
|
print(result)
|
|
|
|
print("Hello callback")
|
2019-09-18 14:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def add(a, b):
|
|
|
|
time.sleep(5)
|
2019-09-19 13:21:05 +00:00
|
|
|
return a + b
|
2019-09-18 14:06:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def add_m(x):
|
|
|
|
from celery import chain
|
|
|
|
a = range(x)
|
|
|
|
b = [a[i:i + 10] for i in range(0, len(a), 10)]
|
|
|
|
s = list()
|
|
|
|
s.append(add.s(b[0], b[1]))
|
|
|
|
for i in b[1:]:
|
|
|
|
s.append(add.s(i))
|
|
|
|
res = chain(*tuple(s))()
|
|
|
|
return res
|