Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,25 @@ def schedule(self):
repr(entry) for entry in self._schedule.values()),
)
return self._schedule


class DryRunDatabaseScheduler(DatabaseScheduler):
"""
DatabaseScheduler in dry-run mode.

The Scheduler reads Periodic Tasks from the database but does not execute them,
only logging when they would have been triggered.
Useful in environments where tasks should not actually run, but the scheduler
must remain operational.
"""

def apply_entry(self, entry, producer=None):
"""
Overwritten method to log the triggered tasks instead of actually running them.
"""
debug(
'Dry-run mode: Skipping task %s %s %s',
entry.task,
entry.args,
entry.kwargs
)
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dry-run scheduler may unintentionally update the database with task execution metadata. In dry-run mode, it would be more appropriate to also override the sync method to prevent updating last_run_at and total_run_count fields in the database, or override the reserve method to avoid marking entries as dirty. Currently, while tasks won't execute, the scheduler will still write execution metadata to the database, which may not be the intended behavior for a dry-run mode.

Suggested change
)
)
def sync(self):
"""
Override sync to avoid persisting execution metadata in dry-run mode.
In DatabaseScheduler, sync() saves dirty entries (including updated
last_run_at and total_run_count) back to the database. For a dry-run
scheduler this would be misleading, since tasks are never actually run.
By overriding sync as a no-op, we ensure that dry-run operation does not
modify PeriodicTask records in the database while still allowing the
scheduler machinery to operate normally.
"""
debug('Dry-run mode: Skipping database sync of scheduled tasks.')

Copilot uses AI. Check for mistakes.
Loading