Skip to content

Commit

Permalink
CLI mara_cron.schedule-job (#3)
Browse files Browse the repository at this point in the history
* typo

* add cli command schedule-job

* improve cli error messages
  • Loading branch information
leo-schick committed Jun 27, 2023
1 parent ef1b839 commit 84ab37c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions mara_cron/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import click
from mara_cron import config

from . import crontab
from . import crontab, job


@click.command()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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.')
Expand Down

0 comments on commit 84ab37c

Please sign in to comment.