zeebe_rs/job/
update_timeout.rs

1use std::time::Duration;
2
3use crate::{Client, ClientError, proto};
4
5pub struct Initial;
6pub struct WithKey;
7pub struct WithTimeout;
8
9pub trait UpdateJobTimeoutRequestState {}
10impl UpdateJobTimeoutRequestState for Initial {}
11impl UpdateJobTimeoutRequestState for WithKey {}
12impl UpdateJobTimeoutRequestState for WithTimeout {}
13
14/// Request to update the deadline of a job
15///
16/// Updates the deadline using the timeout (in ms) provided. This can be used
17/// for extending or shortening the job deadline.
18///
19/// # Examples
20/// ```ignore
21/// client
22///     .update_job_timeout()
23///     .with_job_key(123456)
24///     .with_timeout(Duration::from_secs(10))
25///     .send()
26///     .await?;
27/// ```
28#[derive(Debug, Clone)]
29pub struct UpdateJobTimeoutRequest<T: UpdateJobTimeoutRequestState> {
30    client: Client,
31    job_key: i64,
32    timeout: i64,
33    operation_reference: Option<u64>,
34    _state: std::marker::PhantomData<T>,
35}
36
37impl<T: UpdateJobTimeoutRequestState> UpdateJobTimeoutRequest<T> {
38    pub(crate) fn new(client: Client) -> UpdateJobTimeoutRequest<Initial> {
39        UpdateJobTimeoutRequest {
40            client,
41            job_key: 0,
42            timeout: 0,
43            operation_reference: None,
44            _state: std::marker::PhantomData,
45        }
46    }
47
48    fn transition<NewState: UpdateJobTimeoutRequestState>(
49        self,
50    ) -> UpdateJobTimeoutRequest<NewState> {
51        UpdateJobTimeoutRequest {
52            client: self.client,
53            job_key: self.job_key,
54            timeout: self.timeout,
55            operation_reference: self.operation_reference,
56            _state: std::marker::PhantomData,
57        }
58    }
59}
60
61impl UpdateJobTimeoutRequest<Initial> {
62    /// Sets the job key to identify which job to update
63    ///
64    /// # Arguments
65    /// - `job_key`: The unique key identifying the job
66    ///
67    /// # Returns
68    /// An `UpdateJobTimeoutRequest` in the `WithKey` state
69    pub fn with_job_key(mut self, job_key: i64) -> UpdateJobTimeoutRequest<WithKey> {
70        self.job_key = job_key;
71        self.transition()
72    }
73}
74
75impl UpdateJobTimeoutRequest<WithKey> {
76    /// Sets the new timeout duration
77    ///
78    /// # Arguments
79    /// - `timeout`: The new timeout duration
80    ///
81    /// # Returns
82    /// An `UpdateJobTimeoutRequest` in the `WithTimeout` state
83    pub fn with_timeout(mut self, timeout: Duration) -> UpdateJobTimeoutRequest<WithTimeout> {
84        self.timeout = timeout.as_millis() as i64;
85        self.transition()
86    }
87}
88
89impl UpdateJobTimeoutRequest<WithTimeout> {
90    /// Sends the job timeout update request to the Zeebe workflow engine
91    ///
92    /// # Errors
93    /// - `ClientError::NotFound`: No job exists with the given key
94    /// - `ClientError::InvalidState`: No deadline exists for the given job key
95    ///
96    /// # Returns
97    /// A `Result` containing the `UpdateJobTimeoutResponse` on success, or a `ClientError` on failure
98    pub async fn send(mut self) -> Result<UpdateJobTimeoutResponse, ClientError> {
99        let res = self
100            .client
101            .gateway_client
102            .update_job_timeout(proto::UpdateJobTimeoutRequest {
103                job_key: self.job_key,
104                timeout: self.timeout,
105                operation_reference: self.operation_reference,
106            })
107            .await?;
108
109        Ok(res.into_inner().into())
110    }
111
112    /// Sets a reference key for tracking this operation
113    ///
114    /// # Arguments
115    /// - `operation_reference`: A unique reference key for tracking the operation
116    ///
117    /// # Returns
118    /// The `UpdateJobTimeoutRequest` with the operation reference set
119    pub fn with_operation_reference(mut self, operation_reference: u64) -> Self {
120        self.operation_reference = Some(operation_reference);
121        self
122    }
123}
124
125/// Response from updating a job timeout
126#[derive(Debug, Clone)]
127pub struct UpdateJobTimeoutResponse {}
128
129impl From<proto::UpdateJobTimeoutResponse> for UpdateJobTimeoutResponse {
130    fn from(_value: proto::UpdateJobTimeoutResponse) -> UpdateJobTimeoutResponse {
131        UpdateJobTimeoutResponse {}
132    }
133}