Skip to content

Commit 0735967

Browse files
committed
Add the ability to list the current scheduled cron jobs #199
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 31a41c3 commit 0735967

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

dejacode/settings.py

+5
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ def get_fake_redis_connection(config, use_strict_redis):
565565
"propagate": False,
566566
"level": DEJACODE_LOG_LEVEL,
567567
},
568+
"rq_scheduler.scheduler": {
569+
"handlers": ["null"] if IS_TESTS else ["console"],
570+
"propagate": False,
571+
"level": "DEBUG" if DEBUG else DEJACODE_LOG_LEVEL,
572+
},
568573
},
569574
}
570575

dje/management/commands/setupcron.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,35 @@ class Command(BaseCommand):
2929
"Cron jobs are tasks that run automatically at specified intervals."
3030
)
3131

32-
def handle(self, *args, **kwargs):
32+
def add_arguments(self, parser):
33+
super().add_arguments(parser)
34+
parser.add_argument(
35+
"--list",
36+
action="store_true",
37+
help="List the current scheduled cron jobs.",
38+
)
39+
40+
def handle(self, *args, **options):
3341
if not settings.DEJACODE_ASYNC:
3442
self.stdout.write("SYNC mode detected, skipping cron job setup.")
3543
sys.exit(0)
3644

3745
scheduler = django_rq.get_scheduler("default")
3846

47+
if options["list"]:
48+
self.print_scheduled_jobs(scheduler)
49+
sys.exit(0)
50+
3951
# Cancel all existing cron jobs in the scheduler.
4052
# This ensures that the cron entries are always up-to-date in case their
4153
# configuration has changed. It also prevents any duplicate or orphaned jobs
4254
# from remaining in the scheduler, maintaining a clean and accurate schedule.
4355
cancel_all_scheduled_jobs(scheduler)
4456

45-
self.stdout.write("Schedule vulnerabilities update")
57+
self.stdout.write("Schedule vulnerabilities update:")
4658
forever = None
4759
scheduler.cron(
48-
cron_string=settings.DEJACODE_VULNERABILITIES_CRON, # 3am daily by default
60+
cron_string="*/2 * * * *",
4961
func=update_vulnerabilities,
5062
result_ttl=300,
5163
repeat=forever,
@@ -54,6 +66,9 @@ def handle(self, *args, **kwargs):
5466
)
5567

5668
self.stdout.write(self.style.SUCCESS("Successfully set up cron jobs."))
69+
self.print_scheduled_jobs(scheduler)
70+
71+
def print_scheduled_jobs(self, scheduler):
5772
self.stdout.write("Scheduled jobs next execution:")
5873
for job, scheduled_time in scheduler.get_jobs(with_times=True):
5974
msg = f" > {job.description} in {naturaltime(scheduled_time)} ({scheduled_time})"

0 commit comments

Comments
 (0)