parent
496d720219
commit
0cb37e1268
@ -0,0 +1,58 @@
|
|||||||
|
use std::sync::atomic::{AtomicU8, Ordering};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
||||||
|
pub enum JobExecutionState {
|
||||||
|
Scheduled,
|
||||||
|
Running,
|
||||||
|
Finished,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ExecutionStateSynchronizer {
|
||||||
|
state: Arc<AtomicU8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ExecutionStateSynchronizer {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
state: Arc::new(AtomicU8::new(0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExecutionStateSynchronizer {
|
||||||
|
pub fn set_scheduled(&self) {
|
||||||
|
self.state.store(0, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn set_running(&self) -> RunningHandle {
|
||||||
|
self.state.store(1, Ordering::Relaxed);
|
||||||
|
RunningHandle {
|
||||||
|
state: Arc::clone(&self.state),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_finished(&self) {
|
||||||
|
self.state.store(2, Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn state(&self) -> JobExecutionState {
|
||||||
|
match self.state.load(Ordering::SeqCst) {
|
||||||
|
0 => JobExecutionState::Scheduled,
|
||||||
|
1 => JobExecutionState::Running,
|
||||||
|
2 => JobExecutionState::Scheduled,
|
||||||
|
_ => JobExecutionState::Finished,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RunningHandle {
|
||||||
|
state: Arc<AtomicU8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for RunningHandle {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.state.store(2, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
use crate::execution_state::{ExecutionStateSynchronizer, JobExecutionState};
|
||||||
|
use crate::jobs::ScheduledJob;
|
||||||
|
use crate::progress::{JobProgressUpdate, ProgressSender};
|
||||||
|
use crate::state_data::StateData;
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use mediarepo_core::error::RepoResult;
|
||||||
|
use mediarepo_logic::dao::repo::Repo;
|
||||||
|
use mediarepo_logic::dao::DaoProvider;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::mpsc::Sender;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
#[derive(Default, Clone)]
|
||||||
|
pub struct VacuumJob;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl ScheduledJob for VacuumJob {
|
||||||
|
async fn set_state(&self, _: StateData) -> RepoResult<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, sender: &ProgressSender, repo: Repo) -> RepoResult<()> {
|
||||||
|
sender.send_progress_percent(0.0);
|
||||||
|
repo.job().vacuum().await?;
|
||||||
|
sender.send_progress_percent(1.0);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue