From 84ab37c926777264094d921287677778ab542feb Mon Sep 17 00:00:00 2001 From: Leo Schick <67712864+leo-schick@users.noreply.github.com> Date: Tue, 27 Jun 2023 13:14:42 +0200 Subject: [PATCH] CLI mara_cron.schedule-job (#3) * typo * add cli command schedule-job * improve cli error messages --- README.md | 1 + mara_cron/cli.py | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a813533..df82dd2 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ This package contains the following cli commands: | -------------- | -------------- | `mara_cron.enable --job-id "my_job_id" [--module "module_name"]` | Enables a specific job regardless of the configuration. | `mara_cron.disable --job-id "my_job_id" [--module "module_name"]` | Disables a specific job. +| `mara_cron.schedule-job --job-id "my_job_id"` | Schedules a job to run in less than 1 minute. | `mara_cron.list-crontab` | Lists the current cron tab settings | `mara_cron.list-crontab --with-changes` | Lists the current cron tab including the changes not yet written | `mara_cron.write-crontab` | Writes all not published changes to the crontab diff --git a/mara_cron/cli.py b/mara_cron/cli.py index d3b5a4e..84b1719 100644 --- a/mara_cron/cli.py +++ b/mara_cron/cli.py @@ -2,7 +2,7 @@ import click from mara_cron import config -from . import crontab +from . import crontab, job @click.command() @@ -17,7 +17,7 @@ def disable(job_id: str, module_name: str): job = crontab.get_job(cron, job_id=job_id, module_name=module_name) if not job: - print('Could not find cronjob') + print(f'Could not find cronjob "{job_id}"', file=sys.stderr) sys.exit(1) job.enable(False) @@ -40,7 +40,7 @@ def enable(job_id: str, module_name: str): job = crontab.get_job(cron, job_id=job_id, module_name=module_name) if not job: - print('Could not find cronjob') + print(f'Could not find cronjob "{job_id}"', file=sys.stderr) sys.exit(1) job.enable(True) @@ -49,6 +49,22 @@ def enable(job_id: str, module_name: str): sys.exit(0) +@click.command() +@click.option('--job-id', required=True, + help='The id of of the cron job.') +def schedule_job(job_id: str): + """ Schedules a job to run. """ + cronjob = job.find_job(job_id) + if not cronjob: + print(f'Could not find job with id "{job_id}"', file=sys.stderr) + sys.exit(1) + + crontab.append_single_execution_job(cronjob) + + print(f'Job {job_id} is scheduled to run in less than 1 minute') + sys.exit(0) + + @click.command() @click.option('--with-changes', default=False, is_flag=True, help='Lists the current crontab including the not written changes.')