-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathJobController.py
98 lines (90 loc) · 3.96 KB
/
JobController.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
86
87
88
89
90
91
92
93
94
95
96
97
98
import time
import subprocess
import db_controller as db
import MatterController as mc
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
def Get_myJobList():
sql = """SELECT id,command,commander,token FROM t_job_list WHERE status=%s;"""
joblist = db.get_AllValues(sql, "waiting")
if len(joblist) != 0:
Run_myjob(joblist)
def Set_myjob(command, commander):
sql = """INSERT INTO t_job_list(command,commander,status,timestamp) VALUES(%s,%s,%s,%s) RETURNING id;"""
status = "waiting"
time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
arg = (command, commander, status, time)
job_id = db.insert_db(sql, arg)
attachment = {
"mrkdwn_in": ["text"],
"title": "Added order",
"pretext": "I added your order",
"text": f"@{commander} Your order is accepted.\n" + " JobID:{}\n Command:{}".format(job_id, command)
}
mc.botbot_information(attachment)
def Run_command(job):
job_id, command, commander, token = job
sql = """UPDATE t_job_list SET status=%s, timestamp=%s WHERE id= %s;"""
time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
args = ("running", time, job_id)
db.update_db(sql, args)
p = subprocess.Popen(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return out, err, job_id, command, commander, token
def Run_myjob(joblist):
executer = ThreadPoolExecutor(max_workers=3)
futures = []
for job in joblist:
job_id, command, commander, token = job
# chat message send
attachment = {
"mrkdwn_in": ["text"],
"fallback": f"@{commander} Processing start. JobID:{job_id}",
"title": "Processing start",
"pretext": "Processing began as ordered.",
"text": f"@{commander} Ordered processing has been started.\n" + " JobID:{}\n Command:{}".format(job_id, command)
}
mc.botbot_information(attachment)
time.sleep(1)
future = executer.submit(Run_command, job)
futures.append(future)
while True:
if len(futures) == 0:
break
else:
for future in futures:
if future.done():
out, err, job_id, command, commander, token = future.result()
if len(out) != 0:
attachment = {
"mrkdwn_in": ["text"],
"fallback": f"@{commander} Done. JobID:{job_id}",
"title": f"Done. JobID:{job_id}",
"pretext": "Process is finished.",
"text": f"@{commander} The process was successfully completed.\n" + \
" JobID:{}\n Command:{}".format(job_id, command) + \
"\n\n" + out.decode("utf-8")
}
elif len(err) != 0:
attachment = {
"mrkdwn_in": ["text"],
"fallback": f"@{commander} Error. JobID:{job_id}",
"title": f"Error. JobID:{job_id}",
"pretext": "Processing has failed.",
"text": f"@{commander} The process was failed.\n" + \
" JobID:{}\n Command:{}".format(job_id, command) + \
"\n\n" + err.decode("utf-8")
}
mc.botbot_information(attachment)
sql = """UPDATE t_job_list SET status=%s, timestamp=%s WHERE id= %s;"""
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
args = ("done", timestamp, job_id)
db.update_db(sql, args)
futures.remove(future)
time.sleep(25)
executer.shutdown()
if __name__ == "__main__":
while True:
Get_myJobList()
time.sleep(1) # sleep 1 sec short?