Skip to main content

systemprompt_models/services/
scheduler.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct JobConfig {
5    #[serde(default)]
6    pub extension: Option<String>,
7    pub name: String,
8    #[serde(default = "default_true")]
9    pub enabled: bool,
10    #[serde(default)]
11    pub schedule: Option<String>,
12}
13
14const fn default_true() -> bool {
15    true
16}
17
18impl JobConfig {
19    #[must_use]
20    pub fn new(name: impl Into<String>) -> Self {
21        Self {
22            extension: None,
23            name: name.into(),
24            enabled: true,
25            schedule: None,
26        }
27    }
28
29    #[must_use]
30    pub fn with_extension(mut self, extension: impl Into<String>) -> Self {
31        self.extension = Some(extension.into());
32        self
33    }
34
35    #[must_use]
36    pub fn with_schedule(mut self, schedule: impl Into<String>) -> Self {
37        self.schedule = Some(schedule.into());
38        self
39    }
40
41    #[must_use]
42    pub const fn disabled(mut self) -> Self {
43        self.enabled = false;
44        self
45    }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct SchedulerConfig {
50    #[serde(default = "default_true")]
51    pub enabled: bool,
52    #[serde(default)]
53    pub jobs: Vec<JobConfig>,
54    #[serde(default = "default_bootstrap_jobs")]
55    pub bootstrap_jobs: Vec<String>,
56}
57
58fn default_bootstrap_jobs() -> Vec<String> {
59    vec![
60        "database_cleanup".to_string(),
61        "cleanup_inactive_sessions".to_string(),
62    ]
63}
64
65impl Default for SchedulerConfig {
66    fn default() -> Self {
67        Self {
68            enabled: true,
69            jobs: vec![
70                JobConfig::new("cleanup_anonymous_users")
71                    .with_extension("core")
72                    .with_schedule("0 0 3 * * *"),
73                JobConfig::new("cleanup_empty_contexts")
74                    .with_extension("core")
75                    .with_schedule("0 0 * * * *"),
76                JobConfig::new("cleanup_inactive_sessions")
77                    .with_extension("core")
78                    .with_schedule("0 0 * * * *"),
79                JobConfig::new("database_cleanup")
80                    .with_extension("core")
81                    .with_schedule("0 0 4 * * *"),
82                JobConfig::new("publish_content")
83                    .with_extension("content")
84                    .with_schedule("0 */30 * * * *"),
85            ],
86            bootstrap_jobs: default_bootstrap_jobs(),
87        }
88    }
89}