Conversation
There was a problem hiding this comment.
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
DryRunDatabaseSchedulerclass that inherits fromDatabaseScheduler - Overrides
apply_entrymethod 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 | ||
| ) |
There was a problem hiding this comment.
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.
| ) | |
| ) | |
| 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.') |
| 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 | ||
| ) |
There was a problem hiding this comment.
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.
| 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 | ||
| ) |
There was a problem hiding this comment.
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.
auvipy
left a comment
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Description
Introduces the
DryRunDatabaseSchedulerto 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
DatabaseSchedulerand overwrites theapply_entrymethod to log the triggered tasks instead of actually running them.It fixes the issue #942