-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtasks.py
85 lines (66 loc) · 2.74 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os, time, subprocess
import datetime
from os import listdir
from os.path import isfile, join
from .celery import app
from celery.contrib.abortable import AbortableTask
from django_celery_results.models import TaskResult
from django.contrib.auth.models import User
from django.conf import settings
from celery.exceptions import Ignore, TaskError
def get_scripts():
"""
Returns all scripts from 'ROOT_DIR/celery_scripts'
"""
raw_scripts = []
scripts = []
ignored_ext = ['db', 'txt']
try:
raw_scripts = [f for f in listdir(settings.CELERY_SCRIPTS_DIR) if isfile(join(settings.CELERY_SCRIPTS_DIR, f))]
except Exception as e:
return None, 'Error CELERY_SCRIPTS_DIR: ' + str( e )
for filename in raw_scripts:
ext = filename.split(".")[-1]
if ext not in ignored_ext:
scripts.append( filename )
return scripts, None
def write_to_log_file(logs, script_name):
"""
Writes logs to a log file with formatted name in the CELERY_LOGS_DIR directory.
"""
script_base_name = os.path.splitext(script_name)[0] # Remove the .py extension
current_time = datetime.datetime.now().strftime("%y%m%d-%H%M%S")
log_file_name = f"{script_base_name}-{current_time}.log"
log_file_path = os.path.join(settings.CELERY_LOGS_DIR, log_file_name)
with open(log_file_path, 'w') as log_file:
log_file.write(logs)
return log_file_path
@app.task(bind=True, base=AbortableTask)
def execute_script(self, data: dict):
"""
This task executes scripts found in settings.CELERY_SCRIPTS_DIR and logs are later generated and stored in settings.CELERY_LOGS_DIR
:param data dict: contains data needed for task execution. Example `input` which is the script to be executed.
:rtype: None
"""
script = data.get("script")
args = data.get("args")
print( '> EXEC [' + script + '] -> ('+args+')' )
scripts, ErrInfo = get_scripts()
if script and script in scripts:
# Executing related script
script_path = os.path.join(settings.CELERY_SCRIPTS_DIR, script)
process = subprocess.Popen(
f"python {script_path} {args}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
time.sleep(8)
exit_code = process.wait()
error = False
status = "STARTED"
if exit_code == 0: # If script execution successfull
logs = process.stdout.read().decode()
status = "SUCCESS"
else:
logs = process.stderr.read().decode()
error = True
status = "FAILURE"
log_file = write_to_log_file(logs, script)
return {"logs": logs, "input": script, "error": error, "output": "", "status": status, "log_file": log_file}