celery/beat/
backend.rs

1/// This module contains the definition of application-provided scheduler backends.
2use super::scheduled_task::ScheduledTask;
3use crate::error::BeatError;
4use std::collections::BinaryHeap;
5
6/// A `SchedulerBackend` is in charge of keeping track of the internal state of the scheduler
7/// according to some source of truth, such as a database.
8///
9/// The default scheduler backend, [`LocalSchedulerBackend`](struct.LocalSchedulerBackend.html),
10/// doesn't do any external synchronization, so the source of truth is just the locally defined
11/// schedules.
12pub trait SchedulerBackend {
13    /// Check whether the internal state of the scheduler should be synchronized.
14    /// If this method returns `true`, then `sync` will be called as soon as possible.
15    fn should_sync(&self) -> bool;
16
17    /// Synchronize the internal state of the scheduler.
18    ///
19    /// This method is called in the pauses between scheduled tasks. Synchronization should
20    /// be as quick as possible, as it may otherwise delay the execution of due tasks.
21    /// If synchronization is slow, it should be done incrementally (i.e., it should span
22    /// multiple calls to `sync`).
23    ///
24    /// This method will not be called if `should_sync` returns `false`.
25    fn sync(&mut self, scheduled_tasks: &mut BinaryHeap<ScheduledTask>) -> Result<(), BeatError>;
26
27    // Maybe we should consider some methods to inform the backend that a task has been executed.
28    // Not sure about what Python does, but at least it keeps a counter with the number of executed tasks,
29    // and the backend has access to that.
30}
31
32/// The default [`SchedulerBackend`](trait.SchedulerBackend.html).
33pub struct LocalSchedulerBackend {}
34
35#[allow(clippy::new_without_default)]
36impl LocalSchedulerBackend {
37    pub fn new() -> Self {
38        Self {}
39    }
40}
41
42impl SchedulerBackend for LocalSchedulerBackend {
43    fn should_sync(&self) -> bool {
44        false
45    }
46
47    #[allow(unused_variables)]
48    fn sync(&mut self, scheduled_tasks: &mut BinaryHeap<ScheduledTask>) -> Result<(), BeatError> {
49        unimplemented!()
50    }
51}