File indexing completed on 2026-04-19 08:00:03
0001 import json
0002 import os.path
0003
0004 import requests
0005 from pandaharvester.harvestercore import core_utils
0006 from pandaharvester.harvestercore.plugin_base import PluginBase
0007 from pandaharvester.harvestercore.work_spec import WorkSpec
0008 from pandaharvester.harvestermisc.gitlab_utils import get_job_params
0009
0010
0011 baseLogger = core_utils.setup_logger("gitlab_monitor")
0012
0013
0014
0015 class GitlabMonitor(PluginBase):
0016
0017 def __init__(self, **kwarg):
0018 self.timeout = 180
0019 PluginBase.__init__(self, **kwarg)
0020
0021
0022 def check_workers(self, workspec_list):
0023 retList = []
0024 for workSpec in workspec_list:
0025
0026 tmpLog = self.make_logger(baseLogger, f"workerID={workSpec.workerID}", method_name="check_workers")
0027 try:
0028 params = get_job_params(workSpec)
0029 url = f"{params['project_api']}/{params['project_id']}/pipelines/{workSpec.batchID.split()[0]}"
0030 try:
0031 tmpLog.debug(f"check pipeline at {url}")
0032 r = requests.get(url, headers={"PRIVATE-TOKEN": params["secrets"][params["access_token"]]}, timeout=self.timeout)
0033 response = r.json()
0034 tmpLog.debug(f"got {str(response)}")
0035 except Exception:
0036 err_str = core_utils.dump_error_message(tmpLog)
0037 retList.append((WorkSpec.ST_idle, err_str))
0038 continue
0039 newMsg = ""
0040 if "status" not in response:
0041 newStatus = WorkSpec.ST_idle
0042 if "message" in response:
0043 newMsg = response["message"]
0044 else:
0045 newMsg = "failed to check due to unknown reason"
0046 else:
0047 if response["status"] == "success":
0048 newStatus = WorkSpec.ST_finished
0049 elif response["status"] == "failed":
0050 newStatus = WorkSpec.ST_failed
0051 elif response["status"] == "created":
0052 newStatus = WorkSpec.ST_submitted
0053 elif response["status"] == "pending":
0054 newStatus = WorkSpec.ST_pending
0055 else:
0056 newStatus = WorkSpec.ST_running
0057 tmpLog.debug(f"newStatus={newStatus}")
0058 retList.append((newStatus, newMsg))
0059 except Exception:
0060 err_str = core_utils.dump_error_message(tmpLog)
0061 retList.append((WorkSpec.ST_idle, err_str))
0062 return True, retList