Skip to content

Dry-run Database Scheduler#968

Open
andreangelucci wants to merge 2 commits intocelery:mainfrom
andreangelucci:dry-run-scheduler
Open

Dry-run Database Scheduler#968
andreangelucci wants to merge 2 commits intocelery:mainfrom
andreangelucci:dry-run-scheduler

Conversation

@andreangelucci
Copy link

@andreangelucci andreangelucci commented Oct 29, 2025

Description

Introduces the DryRunDatabaseScheduler to read Periodic Tasks from the database while operating in dry-run mode.

The dry-run mode is useful for setting up projects with Periodic Tasks stored in the Django database while skipping their execution in development environments lower than production, such as local environments.

The Scheduler class inherits from DatabaseScheduler and overwrites the apply_entry method to log the triggered tasks instead of actually running them.


It fixes the issue #942

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a DryRunDatabaseScheduler class that reads periodic tasks from the database while operating in dry-run mode, preventing task execution in development environments. This addresses issue #942 by allowing developers to test scheduler configuration without triggering actual task execution.

Changes:

  • Added new DryRunDatabaseScheduler class that inherits from DatabaseScheduler
  • Overrides apply_entry method to log scheduled tasks instead of executing them

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.
Comment on lines 528 to 547
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 it.
"""
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 new DryRunDatabaseScheduler class lacks test coverage. Given that the repository has comprehensive test coverage for the DatabaseScheduler (in t/unit/test_schedulers.py), tests should be added to verify the dry-run behavior, including confirming that tasks are logged but not executed, and that the scheduler functions correctly without affecting task execution.

Copilot uses AI. Check for mistakes.
Comment on lines 528 to 547
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 it.
"""
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.

Documentation is missing for the new DryRunDatabaseScheduler. Consider adding usage examples to the documentation (such as in docs/includes/introduction.txt) showing how to configure and use the dry-run scheduler, similar to how the DatabaseScheduler is documented. Users should know how to enable this scheduler in their development environments.

Copilot uses AI. Check for mistakes.
Copy link
Member

@auvipy auvipy left a comment

Choose a reason for hiding this comment

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

Interesting stuff! but we will need tests and some times to decided before merging this

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Jan 18, 2026

Codecov Report

❌ Patch coverage is 61.53846% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.11%. Comparing base (0c5b652) to head (6609cb8).
⚠️ Report is 43 commits behind head on main.

Files with missing lines Patch % Lines
django_celery_beat/schedulers.py 61.53% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #968      +/-   ##
==========================================
+ Coverage   88.07%   88.11%   +0.04%     
==========================================
  Files          32       32              
  Lines        1006     1010       +4     
  Branches      104      105       +1     
==========================================
+ Hits          886      890       +4     
- Misses        101      102       +1     
+ Partials       19       18       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants