1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use serde::{Deserialize, Serialize};

use std::fmt::Debug;
use strum::{AsRefStr, EnumString};

use crate::{
    context::{HasJobContext, JobContext},
    job::JobId,
};

/// Represents the state of a [JobRequest]
#[derive(
    EnumString, Serialize, Deserialize, Debug, Clone, AsRefStr, Hash, PartialEq, std::cmp::Eq,
)]
pub enum JobState {
    /// Job is pending
    #[serde(alias = "Latest")]
    Pending,
    /// Job is running
    Running,
    /// Job was done successfully
    Done,
    /// Retry Job
    Retry,
    /// Job has failed. Check `last_error`
    Failed,
    /// Job has been killed
    Killed,
}

impl Default for JobState {
    fn default() -> Self {
        JobState::Pending
    }
}

/// Represents a job which can be serialized and executed

#[derive(Serialize, Debug, Deserialize, Clone)]
pub struct JobRequest<T> {
    pub(crate) job: T,
    pub(crate) context: JobContext,
}

impl<T> JobRequest<T> {
    /// Creates a new [JobRequest]
    pub fn new(job: T) -> Self {
        let context = JobContext::new(JobId::new());
        Self { job, context }
    }

    /// Creates a Job request with context provided
    pub fn new_with_context(job: T, ctx: JobContext) -> Self {
        Self { job, context: ctx }
    }

    /// Get the underlying reference of the job
    pub fn inner(&self) -> &T {
        &self.job
    }

    /// Records a job attempt
    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> {
    /// Gets a mutable reference to the job context.
    fn context_mut(&mut self) -> &mut JobContext {
        &mut self.context
    }

    /// Gets a reference to the job context.
    fn context(&self) -> &JobContext {
        &self.context
    }
}