use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use strum::{AsRefStr, EnumString};
use crate::{
context::{HasJobContext, JobContext},
job::JobId,
};
#[derive(
EnumString, Serialize, Deserialize, Debug, Clone, AsRefStr, Hash, PartialEq, std::cmp::Eq,
)]
pub enum JobState {
#[serde(alias = "Latest")]
Pending,
Running,
Done,
Retry,
Failed,
Killed,
}
impl Default for JobState {
fn default() -> Self {
JobState::Pending
}
}
#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct JobRequest<T> {
pub(crate) job: T,
pub(crate) context: JobContext,
}
impl<T> JobRequest<T> {
pub fn new(job: T) -> Self {
let context = JobContext::new(JobId::new());
Self { job, context }
}
pub fn new_with_context(job: T, ctx: JobContext) -> Self {
Self { job, context: ctx }
}
pub fn inner(&self) -> &T {
&self.job
}
pub fn record_attempt(&mut self) {
self.context.set_attempts(self.context.attempts() + 1);
}
}
impl<T> std::ops::Deref for JobRequest<T> {
type Target = JobContext;
fn deref(&self) -> &Self::Target {
&self.context
}
}
impl<T> std::ops::DerefMut for JobRequest<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.context
}
}
impl<J> HasJobContext for JobRequest<J> {
fn context_mut(&mut self) -> &mut JobContext {
&mut self.context
}
fn context(&self) -> &JobContext {
&self.context
}
}