style: cargo fmt --all
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
use crate::job::Job;
|
||||
use async_trait::async_trait;
|
||||
use tracing::info;
|
||||
use crate::job::Job;
|
||||
|
||||
pub struct ExampleJob;
|
||||
|
||||
#[async_trait]
|
||||
impl Job for ExampleJob {
|
||||
fn name(&self) -> &str { "example" }
|
||||
fn name(&self) -> &str {
|
||||
"example"
|
||||
}
|
||||
async fn run(&self) -> anyhow::Result<()> {
|
||||
info!("example job ran — replace with real work");
|
||||
Ok(())
|
||||
|
||||
@@ -14,8 +14,7 @@ use runner::JobRunner;
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::from_default_env()
|
||||
.add_directive("worker=info".parse()?),
|
||||
tracing_subscriber::EnvFilter::from_default_env().add_directive("worker=info".parse()?),
|
||||
)
|
||||
.init();
|
||||
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
use crate::job::Job;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tracing::{error, info};
|
||||
use crate::job::Job;
|
||||
|
||||
pub struct JobRunner {
|
||||
jobs: Vec<(Arc<dyn Job>, Duration)>,
|
||||
}
|
||||
|
||||
impl JobRunner {
|
||||
pub fn new() -> Self { Self { jobs: vec![] } }
|
||||
pub fn new() -> Self {
|
||||
Self { jobs: vec![] }
|
||||
}
|
||||
|
||||
pub fn register(mut self, job: Arc<dyn Job>, interval: Duration) -> Self {
|
||||
self.jobs.push((job, interval));
|
||||
@@ -16,19 +18,29 @@ impl JobRunner {
|
||||
}
|
||||
|
||||
pub async fn run(self) {
|
||||
let handles: Vec<_> = self.jobs.into_iter().map(|(job, interval)| {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
info!(job = job.name(), "running job");
|
||||
if let Err(e) = job.run().await {
|
||||
error!(job = job.name(), error = %e, "job failed");
|
||||
let handles: Vec<_> = self
|
||||
.jobs
|
||||
.into_iter()
|
||||
.map(|(job, interval)| {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
info!(job = job.name(), "running job");
|
||||
if let Err(e) = job.run().await {
|
||||
error!(job = job.name(), error = %e, "job failed");
|
||||
}
|
||||
tokio::time::sleep(interval).await;
|
||||
}
|
||||
tokio::time::sleep(interval).await;
|
||||
}
|
||||
})
|
||||
})
|
||||
}).collect();
|
||||
for handle in handles { let _ = handle.await; }
|
||||
.collect();
|
||||
for handle in handles {
|
||||
let _ = handle.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for JobRunner { fn default() -> Self { Self::new() } }
|
||||
impl Default for JobRunner {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user