jumpserver/apps/ops/tasks.py

62 lines
1.5 KiB
Python
Raw Normal View History

2017-03-09 06:55:33 +00:00
# coding: utf-8
2017-12-22 13:42:12 +00:00
from celery import shared_task, subtask
from common.utils import get_logger, get_object_or_none
2018-12-20 03:05:36 +00:00
from .celery.utils import register_as_period_task, after_app_shutdown_clean
from .models import Task, CommandExecution
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
@shared_task
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")
@shared_task
def run_command_execution(cid, **kwargs):
execution = get_object_or_none(CommandExecution, id=cid)
return execution.run()
2018-12-20 03:05:36 +00:00
@shared_task
@register_as_period_task(interval=3600*24)
@after_app_shutdown_clean
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()
2017-12-22 13:42:12 +00:00
@shared_task
def hello(name, callback=None):
print("Hello {}".format(name))
if callback is not None:
subtask(callback).delay("Guahongwei")
@shared_task
def hello_callback(result):
print(result)
print("Hello callback")