zeebe_rs/job/
activated_job.rs

1use crate::{ClientError, proto};
2use serde::de::DeserializeOwned;
3
4/// Represents an activated job
5#[derive(Debug, Clone)]
6pub struct ActivatedJob {
7    key: i64,
8    job_type: String,
9    process_instance_key: i64,
10    bpmn_process_id: String,
11    process_definition_version: i32,
12    process_definition_key: i64,
13    element_id: String,
14    element_instance_key: i64,
15    custom_headers: String,
16    worker: String,
17    retries: i32,
18    deadline: i64,
19    variables: String,
20    tenant_id: String,
21}
22
23impl ActivatedJob {
24    /// The key, a unique identifier for the job.
25    ///
26    /// # Returns
27    ///
28    /// * `i64` - The unique key of the job.
29    pub fn key(&self) -> i64 {
30        self.key
31    }
32
33    /// The type of the job (should match what was requested).
34    ///
35    /// # Returns
36    ///
37    /// * `&str` - The type of the job.
38    pub fn job_type(&self) -> &str {
39        &self.job_type
40    }
41
42    /// The job's process instance key.
43    ///
44    /// # Returns
45    ///
46    /// * `i64` - The process instance key of the job.
47    pub fn process_instance_key(&self) -> i64 {
48        self.process_instance_key
49    }
50
51    /// The BPMN process ID of the job process definition.
52    ///
53    /// # Returns
54    ///
55    /// * `&str` - The BPMN process ID.
56    pub fn bpmn_process_id(&self) -> &str {
57        &self.bpmn_process_id
58    }
59
60    /// The version of the job process definition.
61    ///
62    /// # Returns
63    ///
64    /// * `i32` - The version of the process definition.
65    pub fn process_definition_version(&self) -> i32 {
66        self.process_definition_version
67    }
68
69    /// The key of the job process definition.
70    ///
71    /// # Returns
72    ///
73    /// * `i64` - The key of the process definition.
74    pub fn process_definition_key(&self) -> i64 {
75        self.process_definition_key
76    }
77
78    /// The associated task element ID.
79    ///
80    /// # Returns
81    ///
82    /// * `&str` - The element ID.
83    pub fn element_id(&self) -> &str {
84        &self.element_id
85    }
86
87    /// The unique key identifying the associated task, unique within the scope
88    /// of the process instance.
89    ///
90    /// # Returns
91    ///
92    /// * `i64` - The element instance key.
93    pub fn element_instance_key(&self) -> i64 {
94        self.element_instance_key
95    }
96
97    /// A set of custom headers defined during modelling; returned as a serialized
98    /// JSON document.
99    ///
100    /// # Returns
101    ///
102    /// * `&str` - The custom headers as a JSON document.
103    pub fn custom_headers(&self) -> &str {
104        &self.custom_headers
105    }
106
107    /// The name of the worker which activated this job.
108    ///
109    /// # Returns
110    ///
111    /// * `&str` - The worker name.
112    pub fn worker(&self) -> &str {
113        &self.worker
114    }
115
116    /// The amount of retries left to this job (should always be positive).
117    ///
118    /// # Returns
119    ///
120    /// * `i32` - The number of retries left.
121    pub fn retries(&self) -> i32 {
122        self.retries
123    }
124
125    /// When the job can be activated again, sent as a UNIX epoch timestamp.
126    ///
127    /// # Returns
128    ///
129    /// * `i64` - The deadline as a UNIX epoch timestamp.
130    pub fn deadline(&self) -> i64 {
131        self.deadline
132    }
133
134    /// JSON document, computed at activation time, consisting of all visible
135    /// variables to the task scope.
136    ///
137    /// # Returns
138    ///
139    /// * `&str` - The variables as a JSON document.
140    pub fn variables(&self) -> &str {
141        &self.variables
142    }
143
144    /// Deserializes the variables JSON document into a specified type.
145    ///
146    /// # Type Parameters
147    ///
148    /// * `T` - The type to deserialize into.
149    ///
150    /// # Returns
151    ///
152    /// * `Result<T, ClientError>` - The deserialized data or an error if deserialization fails.
153    pub fn data<T: DeserializeOwned>(&self) -> Result<T, ClientError> {
154        serde_json::from_str(&self.variables).map_err(|e| ClientError::DeserializationFailed {
155            value: self.variables.clone(),
156            source: e,
157        })
158    }
159
160    /// The id of the tenant that owns the job.
161    ///
162    /// # Returns
163    ///
164    /// * `&str` - The tenant ID.
165    pub fn tenant_id(&self) -> &str {
166        &self.tenant_id
167    }
168}
169
170impl From<proto::ActivatedJob> for ActivatedJob {
171    fn from(value: proto::ActivatedJob) -> Self {
172        ActivatedJob {
173            key: value.key,
174            job_type: value.r#type,
175            process_instance_key: value.process_instance_key,
176            bpmn_process_id: value.bpmn_process_id,
177            process_definition_version: value.process_definition_version,
178            process_definition_key: value.process_definition_key,
179            element_id: value.element_id,
180            element_instance_key: value.element_instance_key,
181            custom_headers: value.custom_headers,
182            worker: value.worker,
183            retries: value.retries,
184            deadline: value.deadline,
185            variables: value.variables,
186            tenant_id: value.tenant_id,
187        }
188    }
189}