feat: notify ogcore when script execution finishes natively
parent
158cc9ef20
commit
d2ff2d1b35
|
|
@ -25,7 +25,7 @@ class JobMgr():
|
|||
ts = now.strftime ('%Y-%m-%d %H:%M:%S.%f%z') ## '%s' doesn't work on windows
|
||||
job_id = hashlib.sha256 (now.isoformat().encode('UTF-8') + script.encode ('UTF-8')).hexdigest()[0:12]
|
||||
p = subprocess.Popen (args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
self.jobs[job_id] = { 'p': p, 'pid': p.pid, 'starttime': ts, 'script': script, 'client': is_client, 'status': 'running', 'stdout': '', 'stderr': '' }
|
||||
self.jobs[job_id] = { 'p': p, 'pid': p.pid, 'starttime': ts, 'script': script, 'client': is_client, 'status': 'running', 'stdout': '', 'stderr': '', 'notified': False }
|
||||
self.jobs[job_id]['t1'] = threading.Thread (target=job_readstdout, args=(self.jobs[job_id],))
|
||||
self.jobs[job_id]['t2'] = threading.Thread (target=job_readstderr, args=(self.jobs[job_id],))
|
||||
self.jobs[job_id]['t1'].start()
|
||||
|
|
|
|||
|
|
@ -126,6 +126,28 @@ class OpenGnSysWorker(ServerWorker):
|
|||
while True:
|
||||
time.sleep(1)
|
||||
n += 1
|
||||
|
||||
# Check for finished jobs to send webhook
|
||||
for k, elem in list(self.jobmgr.jobs.items()):
|
||||
if elem.get('p') and elem['p'].poll() is not None:
|
||||
if not elem.get('notified', False):
|
||||
if not elem.get('t1').is_alive() and not elem.get('t2').is_alive():
|
||||
elem['notified'] = True
|
||||
elem['status'] = 'finished'
|
||||
elem['rc'] = elem['p'].returncode
|
||||
|
||||
webhook_body = {
|
||||
"nfn": "RUN_SCRIPT",
|
||||
"job_id": k,
|
||||
"res": 1 if elem['rc'] == 0 else 0,
|
||||
"der": elem.get('stderr', '')
|
||||
}
|
||||
logger.info(f"Job {k} finished with return code {elem['rc']}. Sending webhook to ogcore.")
|
||||
try:
|
||||
self.REST.sendMessage('clients/status/webhook', webhook_body)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to send webhook for job {k}: {e}")
|
||||
|
||||
if not n % 10:
|
||||
_st_map = {'linux': 'LNX', 'macos': 'OSX', 'windows': 'WIN'}
|
||||
body = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue