Skip to main content

scheduler/store/
in_memory.rs

1use super::StateStore;
2use crate::model::JobState;
3use std::collections::HashMap;
4use std::convert::Infallible;
5use tokio::sync::RwLock;
6
7#[derive(Debug, Default)]
8pub struct InMemoryStateStore {
9    states: RwLock<HashMap<String, JobState>>,
10}
11
12impl InMemoryStateStore {
13    pub fn new() -> Self {
14        Self::default()
15    }
16}
17
18impl StateStore for InMemoryStateStore {
19    type Error = Infallible;
20
21    async fn load(&self, job_id: &str) -> Result<Option<JobState>, Self::Error> {
22        Ok(self.states.read().await.get(job_id).cloned())
23    }
24
25    async fn save(&self, state: &JobState) -> Result<(), Self::Error> {
26        self.states
27            .write()
28            .await
29            .insert(state.job_id.clone(), state.clone());
30        Ok(())
31    }
32
33    async fn delete(&self, job_id: &str) -> Result<(), Self::Error> {
34        self.states.write().await.remove(job_id);
35        Ok(())
36    }
37}