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-06-19 11:25:21 +00:00
|
|
|
import datetime
|
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
|
2017-12-22 13:42:12 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger, get_object_or_none
|
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
|
|
|
|
)
|
|
|
|
from .celery.utils import create_or_update_celery_periodic_tasks
|
2019-01-08 03:15:09 +00:00
|
|
|
from .models import Task, CommandExecution, CeleryTask
|
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:
|
|
|
|
"""
|
2018-04-01 15:45:37 +00:00
|
|
|
task = get_object_or_none(Task, id=tid)
|
2017-12-22 13:42:12 +00:00
|
|
|
if task:
|
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
|
|
|
|
else:
|
|
|
|
logger.error("No task found")
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
execution = get_object_or_none(CommandExecution, id=cid)
|
2019-02-13 07:55:11 +00:00
|
|
|
if execution:
|
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")
|
2019-02-13 07:55:11 +00:00
|
|
|
else:
|
|
|
|
logger.error("Not found the execution id: {}".format(cid))
|
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
|
2018-12-20 03:05:36 +00:00
|
|
|
@register_as_period_task(interval=3600*24)
|
|
|
|
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:
|
|
|
|
ad.history.all().delete()
|
|
|
|
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-01-08 03:15:09 +00:00
|
|
|
@register_as_period_task(interval=3600*24)
|
|
|
|
def clean_celery_tasks_period():
|
2019-01-15 11:01:33 +00:00
|
|
|
expire_days = 30
|
2019-01-08 03:15:09 +00:00
|
|
|
logger.debug("Start clean celery task history")
|
2019-01-15 11:01:33 +00:00
|
|
|
one_month_ago = timezone.now() - timezone.timedelta(days=expire_days)
|
2019-01-08 03:15:09 +00:00
|
|
|
tasks = CeleryTask.objects.filter(date_start__lt=one_month_ago)
|
|
|
|
for task in tasks:
|
|
|
|
if os.path.isfile(task.full_log_path):
|
|
|
|
try:
|
|
|
|
os.remove(task.full_log_path)
|
|
|
|
except (FileNotFoundError, PermissionError):
|
|
|
|
pass
|
|
|
|
task.delete()
|
|
|
|
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
|
|
|
|
|
|
|
|
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-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
|
|
|
p = subprocess.Popen('ls /tmp', shell=True)
|
2019-06-19 11:25:21 +00:00
|
|
|
print("{} Hello world".format(datetime.datetime.now().strftime("%H:%M:%S")))
|
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
|