Skip to content

Commit

Permalink
Fix some tasks are not added to scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Jan 26, 2024
1 parent ea2e004 commit 96e90a9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
38 changes: 20 additions & 18 deletions dramatiq_crontab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Cron style scheduler for asynchronous Dramatiq tasks in Django."""
from unittest.mock import Mock

from apscheduler.schedulers.base import STATE_STOPPED
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from django.utils import timezone
Expand All @@ -16,14 +18,20 @@

__all__ = ["cron", "scheduler"]

scheduler = BlockingScheduler()

_jobs = []
class LazyBlockingScheduler(BlockingScheduler):
"""Avoid annoying info logs for pending jobs."""

def add_job(self, *args, **kwargs):
logger = self._logger
if self.state == STATE_STOPPED:
# We don't want to schedule jobs before the scheduler is started.
self._logger = Mock()
super().add_job(*args, **kwargs)
self._logger = logger

def _schedule_jobs():
for args, kwargs in _jobs:
scheduler.add_job(*args, **kwargs)

scheduler = LazyBlockingScheduler()


def cron(schedule):
Expand Down Expand Up @@ -59,19 +67,13 @@ def decorator(actor):
if monitor is not None:
actor.fn = monitor(actor.actor_name)(actor.fn)

_jobs.append(
(
(
actor.send,
CronTrigger.from_crontab(
schedule,
timezone=timezone.get_default_timezone(),
),
),
{
"name": actor.actor_name,
},
)
scheduler.add_job(
actor.send,
CronTrigger.from_crontab(
schedule,
timezone=timezone.get_default_timezone(),
),
name=actor.actor_name,
)
# We don't add the Sentry monitor on the actor itself, because we only want to
# monitor the cron job, not the actor itself, or it's direct invocations.
Expand Down
3 changes: 1 addition & 2 deletions dramatiq_crontab/management/commands/crontab.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
except ImportError:
capture_exception = lambda e: None

from ... import _schedule_jobs, scheduler
from ... import scheduler


def kill_softly(signum, frame):
Expand Down Expand Up @@ -67,7 +67,6 @@ def load_tasks(self, options):
If they are not imported, they will not have registered
their tasks with the scheduler.
"""
_schedule_jobs()
for app in apps.get_app_configs():
if app.name == "dramatiq_crontab":
continue
Expand Down
6 changes: 1 addition & 5 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from dramatiq_crontab import _schedule_jobs, scheduler, tasks
from dramatiq_crontab import scheduler, tasks


def test_heartbeat(caplog):
Expand All @@ -20,7 +20,6 @@ def test_cron__stars():
assert not scheduler.remove_all_jobs()
assert tasks.cron("* * * * *")(tasks.heartbeat)
init = datetime.datetime(2021, 1, 1, 0, 0, 0)
_schedule_jobs()
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
Expand All @@ -32,7 +31,6 @@ def test_cron__day_of_week():
assert not scheduler.remove_all_jobs()
assert tasks.cron("* * * * Mon")(tasks.heartbeat)
init = datetime.datetime(2021, 1, 1, 0, 0, 0) # Friday
_schedule_jobs()
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
Expand All @@ -51,7 +49,6 @@ def test_cron_day_range(schedule):
assert not scheduler.remove_all_jobs()
assert tasks.cron(schedule)(tasks.heartbeat)
init = datetime.datetime(2021, 1, 1, 0, 0, 0) # Friday
_schedule_jobs()
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
Expand All @@ -69,7 +66,6 @@ def test_cron__every_15_minutes():
assert not scheduler.remove_all_jobs()
assert tasks.cron("*/15 * * * *")(tasks.heartbeat)
init = datetime.datetime(2021, 1, 1, 0, 0, 0)
_schedule_jobs()
assert scheduler.get_jobs()[0].trigger.get_next_fire_time(
init, init
) == datetime.datetime(
Expand Down

0 comments on commit 96e90a9

Please sign in to comment.