use crate::error::Result;
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
#[derive(Debug, Clone)]
pub struct JobContext {
pub job_id: String,
pub attempt: u32,
pub created_at: chrono::DateTime<chrono::Utc>,
}
#[async_trait]
pub trait Job: Send + Sync + 'static {
const NAME: &'static str;
type Data: Serialize + DeserializeOwned + Send + Sync + Debug;
async fn execute(&self, ctx: JobContext, data: Self::Data) -> Result<()>;
}
#[async_trait]
pub trait JobHandler: Send + Sync {
async fn handle(&self, ctx: JobContext, data: serde_json::Value) -> Result<()>;
}
#[async_trait]
impl<J: Job> JobHandler for J {
async fn handle(&self, ctx: JobContext, data: serde_json::Value) -> Result<()> {
let data: J::Data = serde_json::from_value(data)?;
self.execute(ctx, data).await
}
}