WP-02: contracts and failing tests for settings, mail, jobs and scheduler
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
73
backend/crates/application/src/jobs.rs
Normal file
73
backend/crates/application/src/jobs.rs
Normal file
@ -0,0 +1,73 @@
|
||||
//! Job runner: starts a handler for a job kind, persists its log and result.
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use domain::jobs::{JobKind, JobRun};
|
||||
use domain::ports::JobRunRepository;
|
||||
use domain::DomainError;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Sink for log lines of a running job.
|
||||
#[async_trait]
|
||||
pub trait JobLog: Send + Sync {
|
||||
async fn line(&self, text: &str);
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait JobHandler: Send + Sync {
|
||||
async fn run(&self, params: Option<String>, log: &dyn JobLog) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub struct JobRunner {
|
||||
runs: Arc<dyn JobRunRepository>,
|
||||
handlers: HashMap<JobKind, Arc<dyn JobHandler>>,
|
||||
}
|
||||
|
||||
impl JobRunner {
|
||||
pub fn new(runs: Arc<dyn JobRunRepository>) -> Self {
|
||||
Self {
|
||||
runs,
|
||||
handlers: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(mut self, kind: JobKind, handler: Arc<dyn JobHandler>) -> Self {
|
||||
self.handlers.insert(kind, handler);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn kinds(&self) -> Vec<JobKind> {
|
||||
let mut k: Vec<_> = self.handlers.keys().copied().collect();
|
||||
k.sort_by_key(|k| k.as_str());
|
||||
k
|
||||
}
|
||||
|
||||
/// Start a job in the background. Fails with `Conflict` if the kind is already running.
|
||||
pub async fn start(
|
||||
&self,
|
||||
_kind: JobKind,
|
||||
_params: Option<String>,
|
||||
_triggered_by: &str,
|
||||
) -> Result<JobRun, DomainError> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Run a job and wait for it to finish (used by tests and the scheduler).
|
||||
pub async fn run_and_wait(
|
||||
&self,
|
||||
_kind: JobKind,
|
||||
_params: Option<String>,
|
||||
_triggered_by: &str,
|
||||
) -> Result<JobRun, DomainError> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn get(&self, _id: Uuid) -> Result<JobRun, DomainError> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub async fn list(&self, _limit: u32) -> Result<Vec<JobRun>, DomainError> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user