custom_auto_handler/
custom_auto_handler.rs

1use std::{ops::Deref, time::Duration};
2use thiserror::Error;
3use zeebe_rs::{ActivatedJob, Client, WorkerOutputHandler};
4
5// Custom error type we want to use for auto handler implementation
6#[derive(Debug, Error)]
7pub enum MyErrorType {
8    #[error("")]
9    Fail,
10}
11
12// We want to use Result<T, E> but can't implement trait WorkerOutputHandler
13// because of the orphan rule.
14struct MyResult<T>(pub Result<T, MyErrorType>);
15impl<T> Deref for MyResult<T> {
16    type Target = Result<T, MyErrorType>;
17
18    fn deref(&self) -> &Self::Target {
19        &self.0
20    }
21}
22
23// Implement the WorkerOutputHandler for our custom result type
24impl<T> WorkerOutputHandler for MyResult<T>
25where
26    T: Send + 'static,
27{
28    async fn handle_result(self, client: Client, job: ActivatedJob) {
29        match *self {
30            Ok(_) => unreachable!("This will always fail!"),
31            Err(_) => {
32                let _ = client.fail_job().with_job_key(job.key()).send().await;
33            }
34        }
35    }
36}
37
38async fn always_fail(_client: Client, _job: ActivatedJob) -> MyResult<()> {
39    MyResult(Err(MyErrorType::Fail))
40}
41
42#[tokio::main]
43async fn main() -> Result<(), Box<dyn std::error::Error>> {
44    unsafe { std::env::set_var("RUST_BACKTRACE", "1") };
45
46    // Default configuration using Camunda's docker compose
47    // https://github.com/camunda/camunda-platform/blob/5dc74fe71667e18fbb5c8d4694068d662d83ad00/README.md
48    let client = Client::builder()
49        .with_address("http://localhost", 26500)
50        .with_oauth(
51            String::from("zeebe"),
52            String::from("zecret"),
53            String::from(
54                "http://localhost:18080/auth/realms/camunda-platform/protocol/openid-connect/token",
55            ),
56            String::from("zeebe-api"),
57            Duration::from_secs(30),
58            None,
59        )
60        .build()
61        .await?;
62
63    // Wait until first OAuth token has been retrieved
64    client.auth_initialized().await;
65
66    let worker = client
67        .worker()
68        .with_request_timeout(Duration::from_secs(1))
69        .with_job_timeout(Duration::from_secs(1))
70        .with_max_jobs_to_activate(1)
71        .with_concurrency_limit(1)
72        .with_job_type(String::from("placeholder"))
73        .with_handler(always_fail)
74        .build();
75
76    tokio::join!(worker.run());
77
78    Ok(())
79}