arcature 0.1.2

Arcature: an opinionated full-stack Rust web framework. One package, batteries included.
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! The worker: claims and runs jobs from the shared pool.
//!
//! The run loop claims a batch of pending jobs, spawns each as a dispatch
//! task bounded by a concurrency semaphore, and reaps finished tasks. A
//! sweep of expired leases runs on its own cadence regardless of queue
//! busy-ness (crash recovery must not starve on a busy queue). Graceful
//! shutdown releases unclaimed leases and waits for in-flight tasks to
//! commit their outcome (no abort).

use std::sync::Arc;
use std::time::Instant;

use tokio::sync::Semaphore;
use tokio::task::JoinSet;
use tokio_util::sync::CancellationToken;
use uuid::Uuid;

use super::claim::{self, ClaimedJob};
use super::complete::{self, ClaimTransition, DispatchOutcome, ErrorKind, JobState, Outcome};
use super::config::{RetryPolicy, WorkerConfig};
use super::dialect::{JobPool, sql};
use super::error::WorkerError;
use super::observe::{Event, FailReason, Observer};
use super::registry::{self, Registry};

/// The fixed message stored when a handler panics. The panic payload is never
/// stored or emitted (a handler may panic with a secret in the message).
const PANIC_MESSAGE: &str = "handler panicked";

/// A worker that claims and runs jobs from the shared pool.
#[derive(Clone)]
pub struct Worker {
    pool: JobPool,
    registry: Registry,
    config: WorkerConfig,
    retry: RetryPolicy,
    worker_id: String,
    observer: Option<Arc<dyn Observer>>,
}

impl Worker {
    /// Create a worker with default config and retry policy.
    pub fn new(pool: JobPool, registry: Registry) -> Self {
        Self::builder(pool, registry).build()
    }

    /// Create a worker builder.
    pub fn builder(pool: JobPool, registry: Registry) -> WorkerBuilder {
        WorkerBuilder {
            inner: Worker {
                pool,
                registry,
                config: WorkerConfig::default(),
                retry: RetryPolicy::default(),
                worker_id: format!("worker-{}", Uuid::new_v4().simple()),
                observer: None,
            },
        }
    }

    /// Run the worker until `shutdown` is cancelled.
    pub async fn run(self, shutdown: CancellationToken) -> Result<(), WorkerError> {
        run_loop(
            self.pool,
            self.registry,
            self.config,
            self.retry,
            self.worker_id,
            self.observer,
            shutdown,
        )
        .await
    }
}

/// A builder for [`Worker`].
pub struct WorkerBuilder {
    inner: Worker,
}

impl WorkerBuilder {
    /// Set the worker id. Must be non-empty, at most 128 bytes, ASCII
    /// graphic only; otherwise the default `worker-<uuid>` is kept.
    pub fn worker_id(mut self, id: impl Into<String>) -> Self {
        let id = id.into();
        if !id.is_empty() && id.len() <= 128 && id.bytes().all(|b| b.is_ascii_graphic()) {
            self.inner.worker_id = id;
        }
        self
    }

    /// Set the worker configuration.
    pub fn config(mut self, config: WorkerConfig) -> Self {
        self.inner.config = config;
        self
    }

    /// Set the retry policy.
    pub fn retry_policy(mut self, policy: RetryPolicy) -> Self {
        self.inner.retry = policy;
        self
    }

    /// Set the observer.
    pub fn observer(mut self, observer: impl Observer) -> Self {
        self.inner.observer = Some(Arc::new(observer));
        self
    }

    /// Build the worker.
    pub fn build(self) -> Worker {
        self.inner
    }
}

// ---------------------------------------------------------------------------
// The run loop
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
async fn run_loop(
    pool: JobPool,
    registry: Registry,
    config: WorkerConfig,
    retry: RetryPolicy,
    worker_id: String,
    observer: Option<Arc<dyn Observer>>,
    shutdown: CancellationToken,
) -> Result<(), WorkerError> {
    // Validate the relational invariants before the loop starts. A config
    // with job_timeout > lease would guarantee duplicate delivery; a
    // heartbeat not below the lease would be futile; a retry policy with a
    // non-finite or negative multiplier would panic on the first retry.
    config.validate()?;
    retry.validate()?;

    let semaphore = Arc::new(Semaphore::new(config.get_concurrency()));
    let mut join_set: JoinSet<()> = JoinSet::new();
    let mut next_sweep = tokio::time::Instant::now() + config.get_sweep_interval();

    loop {
        // Reap finished tasks so the JoinSet does not grow unbounded.
        while join_set.try_join_next().is_some() {}

        if shutdown.is_cancelled() {
            break;
        }

        // Run the lease sweep when it is due, regardless of queue busy-ness.
        let now = tokio::time::Instant::now();
        if now >= next_sweep {
            let _ = complete::sweep_expired_leases(&pool, config.get_sweep_batch()).await;
            next_sweep = now + config.get_sweep_interval();
        }

        // Claim a batch of pending jobs.
        let claimed = claim::claim_jobs(
            &pool,
            &worker_id,
            config.get_lease(),
            config.get_poll_batch(),
        )
        .await?;

        if claimed.is_empty() {
            // No jobs available: sleep before the next poll (interruptible).
            tokio::select! {
                _ = shutdown.cancelled() => break,
                _ = tokio::time::sleep(config.get_poll_interval()) => {}
            }
            continue;
        }

        for job in claimed {
            // On shutdown, release the just-claimed row back to pending so a
            // sweep redelivers it. We do not start a job we will not finish.
            if shutdown.is_cancelled() {
                release_claim(&pool, job.id, job.claim_token).await?;
                continue;
            }
            // Acquire a concurrency permit before spawning. If the semaphore
            // is exhausted, wait here (interruptible by shutdown).
            let permit = {
                let sem = semaphore.clone();
                tokio::select! {
                    res = sem.acquire_owned() => match res {
                        Ok(permit) => permit,
                        Err(_) => {
                            release_claim(&pool, job.id, job.claim_token).await?;
                            continue;
                        }
                    },
                    _ = shutdown.cancelled() => {
                        release_claim(&pool, job.id, job.claim_token).await?;
                        continue;
                    }
                }
            };

            let pool_clone = pool.clone();
            let registry_clone = registry.clone();
            let observer_clone = observer.clone();
            let retry_clone = retry;
            let config_clone = config;
            join_set.spawn(async move {
                let _permit = permit;
                let result = dispatch_one(
                    &pool_clone,
                    &registry_clone,
                    retry_clone,
                    config_clone,
                    observer_clone.as_deref(),
                    job,
                )
                .await;
                if let Err(e) = result {
                    eprintln!("job dispatch error: {e}");
                }
            });
        }
    }

    // Graceful shutdown: wait for every in-flight task to finish. Each is
    // bounded by the per-job timeout, so this join cannot hang.
    while let Some(res) = join_set.join_next().await {
        if let Err(e) = res {
            eprintln!("job dispatch task panicked: {e}");
        }
    }

    Ok(())
}

/// Release a claim back to pending (fenced on the claim token).
async fn release_claim(pool: &JobPool, job_id: Uuid, claim_token: Uuid) -> Result<(), WorkerError> {
    let _ = sqlx::query(sql::RELEASE_CLAIM)
        .bind(job_id)
        .bind(claim_token)
        .execute(pool)
        .await?;
    Ok(())
}

fn emit(observer: Option<&dyn Observer>, event: Event) {
    if let Some(o) = observer {
        o.observe(&event);
    }
}

// ---------------------------------------------------------------------------
// dispatch_one — the per-job execute path
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
async fn dispatch_one(
    pool: &JobPool,
    registry: &Registry,
    retry: RetryPolicy,
    config: WorkerConfig,
    observer: Option<&dyn Observer>,
    job: ClaimedJob,
) -> Result<(), WorkerError> {
    let job_timeout = config.get_job_timeout();
    let lease = config.get_lease();
    let heartbeat_interval = config.get_heartbeat_interval();

    let started = Instant::now();
    let job_id = job.id;
    let claim_token = job.claim_token;
    let kind = job.kind.clone();
    let version = job.version;
    let attempt = job.attempts;

    // Look up the handler. An unknown job is a poison job: mark it dead so it
    // is not retried forever.
    let handler = match registry.get(&job.kind, job.version) {
        Some(h) => h,
        None => {
            let transition = complete::mark_dead(
                pool,
                job_id,
                claim_token,
                ErrorKind::Unknown,
                format!("no handler registered for kind {kind:?} version {version}"),
            )
            .await?;
            if transition == ClaimTransition::Updated {
                emit(
                    observer,
                    Event::Failed {
                        job_id,
                        attempt,
                        duration: started.elapsed(),
                        message: format!(
                            "no handler registered for kind {kind:?} version {version}"
                        ),
                        reason: FailReason::Unknown,
                    },
                );
            }
            return Ok(());
        }
    };

    emit(
        observer,
        Event::Started {
            job_id,
            kind: kind.clone(),
            version,
            attempt,
        },
    );

    // The claim already incremented attempts, so the completion decision has
    // the exact attempt count without a second round-trip.
    let state = JobState {
        attempts: job.attempts,
        max_attempts: job.max_attempts,
    };

    // Run the handler in a spawned task so a panic is caught (it does not
    // crash the worker) and a timeout can cancel it.
    let payload = job.payload.clone();
    let mut handler_task = {
        let handler = handler.clone();
        tokio::spawn(async move { handler.handle(&payload, job_id).await })
    };

    let handler_start = tokio::time::Instant::now();
    let timeout_deadline = handler_start + job_timeout;

    let dispatch = loop {
        tokio::select! {
            res = &mut handler_task => {
                break match res {
                    Ok(handler_result) => match handler_result {
                        Ok(()) => DispatchOutcome::Succeeded,
                        Err(registry::HandlerError::Malformed) => DispatchOutcome::Malformed,
                        Err(registry::HandlerError::Job(e)) => DispatchOutcome::HandlerError(e),
                    },
                    Err(join_err) => {
                        // A panic is a permanent failure. The panic payload
                        // is NOT persisted or emitted (secret safety).
                        let transition = complete::mark_dead(
                            pool,
                            job_id,
                            claim_token,
                            ErrorKind::Panic,
                            PANIC_MESSAGE.to_string(),
                        )
                        .await?;
                        if transition == ClaimTransition::Updated {
                            emit(
                                observer,
                                Event::Failed {
                                    job_id,
                                    attempt,
                                    duration: started.elapsed(),
                                    message: PANIC_MESSAGE.to_string(),
                                    reason: FailReason::Panic,
                                },
                            );
                        }
                        let _ = join_err;
                        return Ok(());
                    }
                };
            }
            _ = tokio::time::sleep_until(timeout_deadline) => {
                handler_task.abort();
                break DispatchOutcome::Timeout;
            }
            _ = tokio::time::sleep(heartbeat_interval) => {
                // Refresh the lease. On false (claim lost) or error, continue;
                // the completion will fence out, or the lease may still hold.
                let _ = complete::heartbeat(pool, job_id, claim_token, lease).await;
            }
        }
    };

    let duration = started.elapsed();

    // Persist the outcome. Every transition fences on the claim token; a
    // lost claim affects zero rows and the stale event is suppressed.
    let now = chrono::Utc::now();
    let outcome = complete::decide(&state, &dispatch, &retry, now);
    match outcome {
        Outcome::Succeeded => {
            let transition = complete::mark_succeeded(pool, job_id, claim_token).await?;
            if transition == ClaimTransition::Updated {
                emit(
                    observer,
                    Event::Succeeded {
                        job_id,
                        attempt,
                        duration,
                    },
                );
            }
        }
        Outcome::Retry { available_at } => {
            let message = match &dispatch {
                DispatchOutcome::HandlerError(err) => err.stored_message(),
                DispatchOutcome::Timeout => {
                    format!(
                        "job {job_id} exceeded its {}s timeout",
                        job_timeout.as_secs()
                    )
                }
                _ => String::new(),
            };
            let transition =
                complete::mark_retry(pool, job_id, claim_token, available_at, message.clone())
                    .await?;
            if transition == ClaimTransition::Updated {
                emit(
                    observer,
                    Event::Retried {
                        job_id,
                        attempt,
                        duration,
                        message,
                        next_run_at: available_at,
                    },
                );
            }
        }
        Outcome::Dead {
            error_kind,
            message,
        } => {
            let reason = match error_kind {
                ErrorKind::Permanent => FailReason::Permanent,
                ErrorKind::Exhausted => FailReason::Exhausted,
                ErrorKind::Malformed => FailReason::Malformed,
                ErrorKind::Unknown => FailReason::Unknown,
                ErrorKind::Panic => FailReason::Panic,
                ErrorKind::Timeout => FailReason::Timeout,
            };
            let transition =
                complete::mark_dead(pool, job_id, claim_token, error_kind, message.clone()).await?;
            if transition == ClaimTransition::Updated {
                emit(
                    observer,
                    Event::Failed {
                        job_id,
                        attempt,
                        duration,
                        message,
                        reason,
                    },
                );
            }
        }
    }
    Ok(())
}