use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use crate::{BackgroundJobServerPublisher, JobId};
pub trait JobParameter
where
Self: Serialize + DeserializeOwned,
{
fn to_bytes(&self) -> anyhow::Result<Vec<u8>>;
fn from_bytes(payload: &[u8]) -> Self;
fn try_from_bytes(payload: &[u8]) -> anyhow::Result<Self> {
Ok(Self::from_bytes(payload))
}
fn get_ptype(&self) -> String;
}
#[async_trait]
pub trait BgJobHandler<C> {
fn get_ctx(&self) -> &C;
fn get_publisher(&self) -> &BackgroundJobServerPublisher;
async fn dispatch(&self, ptype: String, payload: &[u8], job_id: JobId) -> anyhow::Result<()>;
async fn retry_policy(
&self,
_ptype: &str,
_payload: &[u8],
) -> anyhow::Result<Option<crate::retry::RetryPolicy>> {
Ok(None)
}
}