Awight has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/345022 )
Change subject: Show job status in --list-jobs ...................................................................... Show job status in --list-jobs Change-Id: Ie99758202547130b711769a24186c30b67cb9729 --- M bin/run-job M process-control.example.yaml M processcontrol/job_wrapper.py M tests/data/global_defaults.yaml 4 files changed, 34 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/process-control refs/changes/22/345022/1 diff --git a/bin/run-job b/bin/run-job index 746d288..2ea0512 100755 --- a/bin/run-job +++ b/bin/run-job @@ -20,3 +20,6 @@ for job_name in job_wrapper.list(): job = job_wrapper.load(job_name) print("{job} - {human_name}".format(job=job_name, human_name=job.name)) + status = job.status() + if status is not None: + print(status) diff --git a/process-control.example.yaml b/process-control.example.yaml index ce742db..15b41ea 100644 --- a/process-control.example.yaml +++ b/process-control.example.yaml @@ -33,3 +33,11 @@ # /var/log/process-control/jobname/jobname-20170401-235959.log # output_directory: /var/log/process-control + +# Path for working files such as locks. +# +# TODO: The deb install should create this directory and do something about +# permissions. +#run_dir: /var/run/process-control +# +run_dir: /tmp diff --git a/processcontrol/job_wrapper.py b/processcontrol/job_wrapper.py index ff8f11f..2731bc2 100644 --- a/processcontrol/job_wrapper.py +++ b/processcontrol/job_wrapper.py @@ -15,7 +15,7 @@ def load(job_name): job_directory = config.GlobalConfiguration().get("job_directory") job_path = "{job_dir}/{job_name}.yaml".format(job_dir=job_directory, job_name=job_name) - return JobWrapper(config_path=job_path) + return JobWrapper(config_path=job_path, slug=job_name) def list(): @@ -28,12 +28,13 @@ class JobWrapper(object): - def __init__(self, config_path=None): + def __init__(self, config_path=None, slug=None): self.global_config = config.GlobalConfiguration() self.config_path = config_path self.config = config.JobConfiguration(self.global_config, self.config_path) self.name = self.config.get("name") + self.slug = slug self.start_time = datetime.datetime.utcnow() self.mailer = mailer.Mailer(self.config) self.timeout = self.config.get("timeout") @@ -130,3 +131,21 @@ print(header, file=out) out.write(stderr_data.decode("utf-8")) + + def status(self): + """Check for any running instances of this job, in this process or another. + + Returns a crappy dict, or None if no process is found. + + Do not use this function to gate the workflow, explicitly assert the + lock instead.""" + + # FIXME: DRY + lock_path = "/tmp/{name}.lock".format(name=self.slug) + if os.path.exists(lock_path): + with open(lock_path, "r") as f: + pid = f.read().strip() + # TODO: encapsulate + return { "status": "running", "pid": pid } + + return None diff --git a/tests/data/global_defaults.yaml b/tests/data/global_defaults.yaml index 4330a53..079dfae 100644 --- a/tests/data/global_defaults.yaml +++ b/tests/data/global_defaults.yaml @@ -28,3 +28,5 @@ # /var/log/process-control/jobname-20170401-235959.log # output_directory: /tmp + +run_dir: /tmp -- To view, visit https://gerrit.wikimedia.org/r/345022 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie99758202547130b711769a24186c30b67cb9729 Gerrit-PatchSet: 1 Gerrit-Project: wikimedia/fundraising/process-control Gerrit-Branch: master Gerrit-Owner: Awight <awi...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits