Skip to main content

faktory/proto/batch/
status.rs

1#[cfg(doc)]
2use super::Batch;
3
4use super::BatchHandle;
5use crate::error::Error;
6use crate::proto::{BatchId, Client};
7use chrono::{DateTime, Utc};
8
9// Not documented, but existing de fakto and also mentioned in the official client
10// https://github.com/contribsys/faktory/blob/main/client/batch.go#L17-L19
11/// State of a `callback` job of a [`Batch`].
12#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq)]
13#[non_exhaustive]
14pub enum CallbackState {
15    /// Not enqueued yet.
16    #[serde(rename = "")]
17    Pending,
18    /// Enqueued by the server, because the jobs belonging to this batch have finished executing.
19    /// If a callback has been consumed, it's status is still `Enqueued`.
20    /// If a callback has finished with failure, it's status remains `Enqueued`.
21    #[serde(rename = "1")]
22    Enqueued,
23    /// The enqueued callback job has been consumed and successfully executed.
24    #[serde(rename = "2")]
25    FinishedOk,
26}
27
28impl std::fmt::Display for CallbackState {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        use CallbackState::*;
31        let s = match self {
32            Pending => "Pending",
33            Enqueued => "Enqueued",
34            FinishedOk => "FinishedOk",
35        };
36        write!(f, "{}", s)
37    }
38}
39
40/// Batch status retrieved from Faktory server.
41#[derive(Deserialize, Debug)]
42pub struct BatchStatus {
43    // Fields "bid", "created_at", "description", "total", "pending", and "failed"
44    // are described in the docs: https://github.com/contribsys/faktory/wiki/Ent-Batches#status
45    /// Id of this batch.
46    pub bid: BatchId,
47
48    /// Batch creation date and time.
49    pub created_at: DateTime<Utc>,
50
51    /// Batch description, if any.
52    pub description: Option<String>,
53
54    /// Number of jobs in this batch.
55    pub total: usize,
56
57    /// Number of pending jobs.
58    pub pending: usize,
59
60    /// Number of failed jobs.
61    pub failed: usize,
62
63    // The official golang client also mentions "parent_bid', "complete_st", and "success_st":
64    // https://github.com/contribsys/faktory/blob/main/client/batch.go#L8-L22
65    /// Id of the parent batch, provided this batch is a child ("nested") batch.
66    pub parent_bid: Option<BatchId>,
67
68    /// State of the `complete` callback.
69    ///
70    /// See [with_complete_callback](struct.BatchBuilder.html#method.with_complete_callback).
71    #[serde(rename = "complete_st")]
72    pub complete_callback_state: CallbackState,
73
74    /// State of the `success` callback.
75    ///
76    /// See [with_success_callback](struct.BatchBuilder.html#method.with_success_callback).
77    #[serde(rename = "success_st")]
78    pub success_callback_state: CallbackState,
79}
80
81impl<'a> BatchStatus {
82    /// Open the batch for which this `BatchStatus` has been retrieved.
83    ///
84    /// See [`open_batch`](Client::open_batch).
85    pub async fn open(&self, prod: &'a mut Client) -> Result<Option<BatchHandle<'a>>, Error> {
86        prod.open_batch(&self.bid).await
87    }
88}