mas_storage/queue/
schedule.rs

1// Copyright 2024 New Vector Ltd.
2//
3// SPDX-License-Identifier: AGPL-3.0-only
4// Please see LICENSE in the repository root for full details.
5
6//! Repository to interact with recurrent scheduled jobs in the job queue
7
8use async_trait::async_trait;
9use chrono::{DateTime, Utc};
10
11use crate::repository_impl;
12
13/// [`QueueScheduleRepository::list`] returns a list of [`ScheduleStatus`],
14/// which has the name of the schedule and infos about its last run
15pub struct ScheduleStatus {
16    /// Name of the schedule, uniquely identifying it
17    pub schedule_name: String,
18    /// When the schedule was last run
19    pub last_scheduled_at: Option<DateTime<Utc>>,
20    /// Did the last job on this schedule finish? (successfully or not)
21    pub last_scheduled_job_completed: Option<bool>,
22}
23
24/// A [`QueueScheduleRepository`] is used to interact with recurrent scheduled
25/// jobs in the job queue.
26#[async_trait]
27pub trait QueueScheduleRepository: Send + Sync {
28    /// The error type returned by the repository.
29    type Error;
30
31    /// Setup the list of schedules in the repository
32    ///
33    /// # Parameters
34    ///
35    /// * `schedules` - The list of schedules to setup
36    ///
37    /// # Errors
38    ///
39    /// Returns an error if the underlying repository fails.
40    async fn setup(&mut self, schedules: &[&'static str]) -> Result<(), Self::Error>;
41
42    /// List the schedules in the repository, with the last time they were run
43    ///
44    /// # Errors
45    ///
46    /// Returns an error if the underlying repository fails.
47    async fn list(&mut self) -> Result<Vec<ScheduleStatus>, Self::Error>;
48}
49
50repository_impl!(QueueScheduleRepository:
51    async fn setup(
52        &mut self,
53        schedules: &[&'static str],
54    ) -> Result<(), Self::Error>;
55
56    async fn list(&mut self) -> Result<Vec<ScheduleStatus>, Self::Error>;
57);