zeebe/worker/builder.rs
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
use crate::client::Client;
use crate::error::{Error, Result};
use crate::job::Job;
use crate::proto;
use crate::worker::{
auto_handler::{Extensions, FromJob, HandlerFactory, State},
job_dispatcher, JobPoller, PollMessage,
};
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use serde::Serialize;
use serde_json::json;
use std::fmt;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use tokio::{sync::mpsc, time::interval};
use tokio_stream::{
wrappers::{IntervalStream, ReceiverStream},
StreamExt,
};
use tracing_futures::Instrument;
static DEFAULT_JOB_TIMEOUT: Duration = Duration::from_secs(5 * 60);
static DEFAULT_JOB_TIMEOUT_IN_MS: i64 = DEFAULT_JOB_TIMEOUT.as_millis() as i64;
static DEFAULT_JOB_WORKER_MAX_JOB_ACTIVE: u32 = 32;
static DEFAULT_JOB_WORKER_CONCURRENCY: u32 = 4;
static DEFAULT_JOB_WORKER_POLL_INTERVAL: Duration = Duration::from_millis(100);
static DEFAULT_JOB_WORKER_POLL_THRESHOLD: f32 = 0.3;
static REQUEST_TIMEOUT_OFFSET: Duration = Duration::from_secs(10);
static DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Clone)]
pub(crate) struct JobHandler(Arc<dyn Fn(Client, Job) -> LocalBoxFuture<'static, ()>>);
impl JobHandler {
pub(crate) fn call(&self, client: Client, job: Job) -> LocalBoxFuture<'static, ()> {
self.0(client, job)
}
}
impl fmt::Debug for JobHandler {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("JobHandler")
}
}
/// Configuration for an asynchronous worker process.
#[derive(Debug)]
pub struct JobWorkerBuilder {
client: Client,
handler: Option<JobHandler>,
data: Extensions,
concurrency: u32,
poll_interval: Duration,
poll_threshold: f32,
request: proto::ActivateJobsRequest,
request_timeout: Duration,
}
impl JobWorkerBuilder {
/// Create a new job worker builder.
pub fn new(client: Client) -> Self {
JobWorkerBuilder {
client,
handler: None,
data: Extensions::new(),
concurrency: DEFAULT_JOB_WORKER_CONCURRENCY,
poll_interval: DEFAULT_JOB_WORKER_POLL_INTERVAL,
poll_threshold: DEFAULT_JOB_WORKER_POLL_THRESHOLD,
request: proto::ActivateJobsRequest {
r#type: String::new(),
worker: String::from("default"),
timeout: DEFAULT_JOB_TIMEOUT_IN_MS,
max_jobs_to_activate: DEFAULT_JOB_WORKER_MAX_JOB_ACTIVE as i32,
fetch_variable: Vec::new(),
request_timeout: DEFAULT_REQUEST_TIMEOUT.as_millis() as i64,
},
request_timeout: DEFAULT_REQUEST_TIMEOUT + REQUEST_TIMEOUT_OFFSET,
}
}
/// Set the job type of the worker.
pub fn with_job_type<T: Into<String>>(mut self, job_type: T) -> Self {
self.request.r#type = job_type.into();
self
}
/// Set the worker name (mostly used for logging)
pub fn with_worker_name<T: Into<String>>(mut self, worker: T) -> Self {
self.request.worker = worker.into();
self
}
/// Set the worker job timeout.
///
/// See [the requesting jobs docs] for more details.
///
/// [the requesting jobs docs]: https://docs.zeebe.io/basics/job-workers.html#requesting-jobs-from-the-broker
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.request.timeout = timeout.as_millis() as i64;
self
}
/// Set the worker request timeout.
///
/// See [the requesting jobs docs] for more details.
///
/// [the requesting jobs docs]: https://docs.zeebe.io/basics/job-workers.html#requesting-jobs-from-the-broker
pub fn with_request_timeout(mut self, request_timeout: Duration) -> Self {
self.request.request_timeout = request_timeout.as_millis() as i64;
self.request_timeout = request_timeout + REQUEST_TIMEOUT_OFFSET;
self
}
/// Set the maximum jobs to activate at a time by the worker.
pub fn with_max_jobs_active(mut self, max_jobs_active: u32) -> Self {
self.request.max_jobs_to_activate = max_jobs_active as i32;
self
}
/// Set the max number of jobs to run concurrently.
pub fn with_concurrency(self, concurrency: u32) -> Self {
JobWorkerBuilder {
concurrency,
..self
}
}
/// Set the handler function for the worker.
pub fn with_handler<T, R>(self, handler: T) -> Self
where
T: Fn(Client, Job) -> R + 'static,
R: Future<Output = ()> + 'static,
{
JobWorkerBuilder {
handler: Some(JobHandler(Arc::new(move |mut client, job| {
client.current_job_key = Some(job.key());
Box::pin(handler(client, job))
}))),
..self
}
}
/// Set a handler function that completes or fails the job based on the result
/// rather than having to explicitly use the client to report job status.
///
/// # Examples
///
/// ```no_run
/// use serde::{Deserialize, Serialize};
/// use thiserror::Error;
/// use zeebe::{Client, Data};
/// use futures::future;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new();
///
/// // Given an app-specific error
/// #[derive(Error, Debug)]
/// enum MyError {
/// #[error("unknown error occurred")]
/// Unknown,
/// }
///
/// // And app-specific job data
/// #[derive(Deserialize)]
/// struct MyJobData {
/// my_property: String,
/// my_other_property: String,
/// }
///
/// // And app-specific job result
/// #[derive(Serialize)]
/// struct MyJobResult {
/// result: u32,
/// }
///
/// // Async job handler function
/// async fn handle_job(client: Client, data: Data<MyJobData>) -> Result<MyJobResult, MyError> {
/// Ok(MyJobResult { result: 42 })
/// }
///
/// // Example use with async function
/// let job = client
/// .job_worker()
/// .with_job_type("my-job-type")
/// .with_auto_handler(handle_job)
/// .run()
/// .await?;
///
/// // Use with closure
/// let job = client
/// .job_worker()
/// .with_job_type("my-job-type")
/// .with_auto_handler(|client: Client, my_job_data: Data<MyJobData>| {
/// future::ok::<_, MyError>(MyJobResult { result: 42 })
/// })
/// .run()
/// .await?;
///
/// # Ok(())
/// # }
///
/// ```
pub fn with_auto_handler<F, T, R, O, E>(self, handler: F) -> Self
where
F: HandlerFactory<T, R, O, E>,
T: FromJob,
R: Future<Output = std::result::Result<O, E>> + 'static,
O: Serialize,
E: std::error::Error,
{
let job_type = self.request.r#type.clone();
self.with_handler(move |client, job| {
let span = tracing::info_span!(
"auto_handler",
otel.name = %job_type,
instance = job.process_instance_key(),
job = job.key(),
);
match T::from_job(&client, &job) {
Ok(params) => handler
.call(params)
.then(move |result| match result {
Ok(variables) => client
.complete_job()
.with_variables(json!(variables))
.send()
.map(|_| ())
.left_future(),
Err(err) => client
.fail_job()
.with_error_message(err.to_string())
.send()
.map(|_| ())
.right_future(),
})
.left_future()
.instrument(span),
Err(err) => {
span.in_scope(|| {
tracing::error!(%err, "variables do not deserialize to expected type");
});
client
.fail_job()
.with_error_message(format!(
"variables do not deserialize to expected type: {:?}",
err
))
.send()
.map(|_| ())
.right_future()
.instrument(span)
}
}
})
}
/// Set state to be persisted across job [`auto handler`] invocations.
///
/// [`auto handler`]: JobWorkerBuilder::with_auto_handler
///
/// # Examples
///
/// ```no_run
/// use futures::future;
/// use serde::Serialize;
/// use std::cell::Cell;
/// use thiserror::Error;
/// use zeebe::{Client, State};
///
/// #[derive(Error, Debug)]
/// enum MyError {}
///
/// #[derive(Serialize)]
/// struct MyJobResult {
/// result: u32,
/// }
///
/// struct MyJobState {
/// total: Cell<u32>,
/// }
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::default();
///
/// let job_state = MyJobState {
/// total: Cell::new(0),
/// };
///
/// let _job = client
/// .job_worker()
/// .with_job_type("my-job-type")
/// .with_auto_handler(|my_job_state: State<MyJobState>| {
/// future::ok::<_, MyError>(MyJobResult { result: 42 })
/// })
/// .with_state(job_state)
/// .run()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn with_state<T: 'static>(mut self, t: T) -> Self {
self.data.insert(State::new(t));
self
}
/// Set the list of variables to fetch as the job variables.
///
/// By default all visible variables at the time of activation for the scope of
/// the job will be returned.
pub fn with_fetch_variables(mut self, fetch_variables: Vec<String>) -> Self {
self.request.fetch_variable = fetch_variables;
self
}
/// Start the worker as a future. To stop the worker, simply drop the future.
pub async fn run(self) -> Result<()> {
if self.request.r#type.is_empty() || self.handler.is_none() {
return Err(Error::InvalidParameters(
"`job_type` and `handler` must be set",
));
}
let (job_queue, job_queue_rx) = mpsc::channel(self.request.max_jobs_to_activate as usize);
let (poll_queue, poll_rx) = mpsc::channel(32);
let poll_interval =
IntervalStream::new(interval(self.poll_interval)).map(|_| PollMessage::FetchJobs);
let worker_name = self.request.worker.clone();
let job_poller = JobPoller {
client: self.client.clone(),
request_timeout: self.request_timeout,
request_in_progress: false,
max_jobs_active: self.request.max_jobs_to_activate as u32,
job_queue,
message_sender: poll_queue.clone(),
messages: Box::pin(futures::stream::select(
ReceiverStream::new(poll_rx),
poll_interval,
)),
remaining: 0,
threshold: (self.request.max_jobs_to_activate as f32 * self.poll_threshold).floor()
as u32,
request: self.request,
};
// Process work
futures::join!(
job_poller,
job_dispatcher::run(
job_queue_rx,
poll_queue,
self.concurrency as usize,
self.handler.unwrap(),
self.client.clone(),
worker_name,
self.data,
)
);
Ok(())
}
}