From d2ff2d1b35fdaa473a3fee7cbb7c97424affa1b1 Mon Sep 17 00:00:00 2001 From: jmbardallo Date: Tue, 21 Jul 2026 12:20:17 +0200 Subject: [PATCH] feat: notify ogcore when script execution finishes natively --- src/opengnsys/jobmgr.py | 2 +- .../modules/server/OpenGnSys/__init__.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/opengnsys/jobmgr.py b/src/opengnsys/jobmgr.py index fe5c093..e7f77e3 100644 --- a/src/opengnsys/jobmgr.py +++ b/src/opengnsys/jobmgr.py @@ -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() diff --git a/src/opengnsys/modules/server/OpenGnSys/__init__.py b/src/opengnsys/modules/server/OpenGnSys/__init__.py index 1203eb2..d730694 100644 --- a/src/opengnsys/modules/server/OpenGnSys/__init__.py +++ b/src/opengnsys/modules/server/OpenGnSys/__init__.py @@ -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 = {