ora_scheduler/store/
task.rs

1//! Backend store types for managing tasks.
2use async_trait::async_trait;
3use futures::Stream;
4use ora_common::{timeout::TimeoutPolicy, UnixNanos};
5use uuid::Uuid;
6
7/// A backend store for task management.
8#[async_trait]
9pub trait SchedulerTaskStore {
10    /// An error type returned by operations.
11    type Error: std::error::Error + Send + Sync + 'static;
12
13    /// An event stream that can be used to watch for changes.
14    type Events: Stream<Item = Result<SchedulerTaskStoreEvent, Self::Error>>;
15
16    /// Subscribe for new events.
17    async fn events(&self) -> Result<Self::Events, Self::Error>;
18
19    /// Return all tasks that should be scheduled.
20    async fn pending_tasks(&self) -> Result<Vec<PendingTask>, Self::Error>;
21
22    /// Return active tasks that are not pending and are not
23    /// yet finished.
24    ///
25    /// The scheduler needs to know about these tasks
26    /// in order to schedule timeouts for all existing tasks,
27    /// not just newly added ones.
28    async fn active_tasks(&self) -> Result<Vec<ActiveTask>, Self::Error>;
29
30    /// Update the task status as ready.
31    async fn task_ready(&self, task_id: Uuid) -> Result<(), Self::Error>;
32
33    /// The task timed out.
34    ///
35    /// This is always called for tasks that have a timeout policy set,
36    /// it should be ignored if the task already finished before this
37    /// function is called.
38    async fn task_timed_out(&self, task_id: Uuid) -> Result<(), Self::Error>;
39}
40
41/// A task that was not yet marked as ready.
42#[derive(Debug, Clone, Copy)]
43pub struct PendingTask {
44    /// The task's ID.
45    pub id: Uuid,
46    /// The task's target timestamp.
47    pub target: UnixNanos,
48    /// The task's timeout policy.
49    pub timeout: TimeoutPolicy,
50}
51
52/// A task that is not finished and not pending anymore.
53///
54/// It is used to keep track of timeouts for tasks
55/// that are already running when the scheduler starts.
56#[derive(Debug, Clone, Copy)]
57pub struct ActiveTask {
58    /// The task's ID.
59    pub id: Uuid,
60    /// The task's target timestamp.
61    pub target: UnixNanos,
62    /// The task's timeout policy.
63    pub timeout: TimeoutPolicy,
64}
65
66impl From<PendingTask> for ActiveTask {
67    fn from(value: PendingTask) -> Self {
68        Self {
69            id: value.id,
70            target: value.target,
71            timeout: value.timeout,
72        }
73    }
74}
75
76/// A scheduler store event.
77#[derive(Debug, Clone, Copy)]
78pub enum SchedulerTaskStoreEvent {
79    /// A new task was added.
80    TaskAdded(PendingTask),
81    /// A task was cancelled.
82    TaskCancelled(Uuid),
83}