feat: add abstraction around task
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
45
crates/churn/src/agent/task.rs
Normal file
45
crates/churn/src/agent/task.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait Task {
|
||||
fn id(&self) -> String;
|
||||
async fn should_run(&self) -> anyhow::Result<bool> {
|
||||
Ok(true)
|
||||
}
|
||||
async fn execute(&self) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
pub trait IntoTask {
|
||||
fn into_task(self) -> ConcreteTask;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConcreteTask {
|
||||
inner: Arc<dyn Task + Sync + Send + 'static>,
|
||||
}
|
||||
|
||||
impl ConcreteTask {
|
||||
pub fn new<T: Task + Sync + Send + 'static>(t: T) -> Self {
|
||||
Self { inner: Arc::new(t) }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Deref for ConcreteTask {
|
||||
type Target = Arc<dyn Task + Sync + Send + 'static>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoTask for ConcreteTask {
|
||||
fn into_task(self) -> ConcreteTask {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Task + Sync + Send + 'static> IntoTask for T {
|
||||
fn into_task(self) -> ConcreteTask {
|
||||
ConcreteTask::new(self)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user