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:
parent
974e1ee0d6
commit
d6fdda0e4e
@ -5,6 +5,7 @@ use refresh::AgentRefresh;
|
||||
pub use config::setup_config;
|
||||
|
||||
pub mod models;
|
||||
pub(crate) mod task;
|
||||
|
||||
mod agent_state;
|
||||
mod config;
|
||||
|
@ -1,32 +1,33 @@
|
||||
use anyhow::Context;
|
||||
|
||||
use super::task::{IntoTask, Task};
|
||||
|
||||
pub struct Plan {}
|
||||
impl Plan {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub async fn tasks(&self) -> anyhow::Result<Vec<Task>> {
|
||||
Ok(vec![Task::new()])
|
||||
pub async fn tasks(&self) -> anyhow::Result<Vec<impl IntoTask>> {
|
||||
Ok(vec![AptTask::new()])
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Task {}
|
||||
pub struct AptTask {}
|
||||
|
||||
impl Task {
|
||||
impl AptTask {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> String {
|
||||
#[async_trait::async_trait]
|
||||
impl Task for AptTask {
|
||||
fn id(&self) -> String {
|
||||
"apt".into()
|
||||
}
|
||||
|
||||
pub async fn should_run(&self) -> anyhow::Result<bool> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub async fn execute(&self) -> anyhow::Result<()> {
|
||||
async fn execute(&self) -> anyhow::Result<()> {
|
||||
let mut cmd = tokio::process::Command::new("apt-get");
|
||||
cmd.args(["update", "-q"]);
|
||||
let output = cmd.output().await.context("failed to run apt update")?;
|
||||
|
@ -1,6 +1,9 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use crate::agent::actions::Plan;
|
||||
use crate::agent::{
|
||||
actions::Plan,
|
||||
task::{ConcreteTask, IntoTask},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ScheduledTasks {}
|
||||
@ -17,7 +20,12 @@ impl ScheduledTasks {
|
||||
tracing::info!("scheduling: {}", task);
|
||||
|
||||
let plan = Plan::new();
|
||||
let tasks = plan.tasks().await?;
|
||||
let tasks: Vec<ConcreteTask> = plan
|
||||
.tasks()
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|i| i.into_task())
|
||||
.collect();
|
||||
|
||||
for task in tasks {
|
||||
if !task.should_run().await? {
|
||||
|
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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user