runledger-runtime 0.5.0

Async worker, scheduler, and reaper runtime for the Runledger job system
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use std::collections::HashMap;
use std::panic::AssertUnwindSafe;

use futures_util::FutureExt;
use runledger_core::jobs::{JobContext, JobDeadLetterInfo, JobDeadLetterReason};
use runledger_postgres::jobs::{ReapedLeaseDisposition, ReapedLeaseRecord};
use tokio::sync::watch;
use tokio::task::{Id, JoinSet};
use tokio::time::{Duration, timeout};
use tracing::{Instrument, info, info_span, warn};

use crate::registry::JobRegistry;
use crate::shutdown;

const REAPER_WORKER_ID: &str = "reaper";
const REAPER_TERMINAL_HOOK_MAX_CONCURRENCY: usize = 8;
#[cfg(test)]
const TERMINAL_HOOK_TIMEOUT: Duration = Duration::from_millis(100);
#[cfg(not(test))]
const TERMINAL_HOOK_TIMEOUT: Duration = Duration::from_secs(10);
#[cfg(test)]
const TERMINAL_HOOK_SHUTDOWN_DRAIN_TIMEOUT: Duration = Duration::from_millis(150);
#[cfg(not(test))]
const TERMINAL_HOOK_SHUTDOWN_DRAIN_TIMEOUT: Duration = Duration::from_secs(10);
#[cfg(test)]
const TERMINAL_HOOK_ABORT_DRAIN_TIMEOUT: Duration = Duration::from_millis(50);
#[cfg(not(test))]
const TERMINAL_HOOK_ABORT_DRAIN_TIMEOUT: Duration = Duration::from_millis(250);

pub(super) async fn notify_handlers_of_terminal_lease_expirations(
    registry: &JobRegistry,
    jobs: &[ReapedLeaseRecord],
    shutdown: &mut watch::Receiver<bool>,
) -> TerminalHookFanoutResult {
    notify_handlers_of_terminal_lease_expirations_inner(registry, jobs, shutdown, || {}).await
}

#[cfg(test)]
pub(super) async fn notify_handlers_of_terminal_lease_expirations_with_before_first_hook_admission<
    F,
>(
    registry: &JobRegistry,
    jobs: &[ReapedLeaseRecord],
    shutdown: &mut watch::Receiver<bool>,
    before_first_hook_admission: F,
) -> TerminalHookFanoutResult
where
    F: FnOnce(),
{
    notify_handlers_of_terminal_lease_expirations_inner(
        registry,
        jobs,
        shutdown,
        before_first_hook_admission,
    )
    .await
}

async fn notify_handlers_of_terminal_lease_expirations_inner<F>(
    registry: &JobRegistry,
    jobs: &[ReapedLeaseRecord],
    shutdown: &mut watch::Receiver<bool>,
    before_first_hook_admission: F,
) -> TerminalHookFanoutResult
where
    F: FnOnce(),
{
    let mut in_flight = JoinSet::new();
    let mut metadata: HashMap<Id, HookMetadata> = HashMap::new();
    let mut started_hook_count = 0;
    let mut skipped_hook_count = 0;
    let mut shutdown_observed = shutdown::is_requested_or_closed(shutdown);
    let mut post_shutdown_admission_budget = if shutdown_observed {
        // If shutdown was already requested after the reaper committed a
        // terminal batch, attempt one bounded concurrency window instead of
        // silently skipping every committed hook.
        REAPER_TERMINAL_HOOK_MAX_CONCURRENCY
    } else {
        0
    };
    let mut before_first_hook_admission = Some(before_first_hook_admission);

    for job in jobs {
        let ReapedLeaseDisposition::DeadLetteredTerminal { payload } = &job.disposition else {
            continue;
        };

        let Some(handler) = registry.get(job.job_type.as_borrowed()) else {
            continue;
        };

        if let Some(before_first_hook_admission) = before_first_hook_admission.take() {
            before_first_hook_admission();
        }

        if !shutdown_observed {
            while in_flight.len() >= REAPER_TERMINAL_HOOK_MAX_CONCURRENCY {
                if wait_for_hook_capacity_or_shutdown(&mut in_flight, &mut metadata, shutdown).await
                    == HookWaitOutcome::ShutdownRequested
                {
                    observe_shutdown_and_grant_admission_budget(
                        &mut shutdown_observed,
                        &mut post_shutdown_admission_budget,
                    );
                    break;
                }
            }

            if !shutdown_observed && shutdown::is_requested_or_closed(shutdown) {
                observe_shutdown_and_grant_admission_budget(
                    &mut shutdown_observed,
                    &mut post_shutdown_admission_budget,
                );
            }
        }

        if shutdown_observed {
            if post_shutdown_admission_budget == 0
                || in_flight.len() >= REAPER_TERMINAL_HOOK_MAX_CONCURRENCY
            {
                skipped_hook_count += 1;
                continue;
            }
            post_shutdown_admission_budget -= 1;
        }

        let context = JobContext {
            job_id: job.job_id,
            run_number: job.run_number,
            attempt: job.attempt,
            organization_id: job.organization_id,
            worker_id: REAPER_WORKER_ID.to_string(),
        };
        let payload = payload.clone();
        let dead_letter = JobDeadLetterInfo::new(
            job.failure.clone(),
            JobDeadLetterReason::LeaseExpired,
            Some(job.max_attempts),
        );
        let hook_meta = HookMetadata {
            job_id: job.job_id.to_string(),
            job_type: job.job_type.to_string(),
            run_number: job.run_number,
            attempt: job.attempt,
        };
        let hook_span = info_span!(
            "reaper_terminal_hook",
            sentry.name = %job.job_type,
            sentry.op = "runledger.reaper.terminal_hook",
            job_id = %job.job_id,
            job_type = %job.job_type,
            run_number = job.run_number,
            attempt = job.attempt,
        );

        let abort_handle = in_flight.spawn(
            async move {
                match tokio::time::timeout(
                    TERMINAL_HOOK_TIMEOUT,
                    AssertUnwindSafe(handler.on_dead_letter(context, payload, dead_letter))
                        .catch_unwind(),
                )
                .await
                {
                    Ok(Ok(())) => HookOutcome::Completed,
                    Ok(Err(panic_payload)) => {
                        HookOutcome::Panicked(panic_payload_message(&*panic_payload))
                    }
                    Err(_) => HookOutcome::TimedOut,
                }
            }
            .instrument(hook_span),
        );
        metadata.insert(abort_handle.id(), hook_meta);
        started_hook_count += 1;
    }

    if !shutdown_observed {
        shutdown_observed =
            drain_hook_results_until_complete_or_shutdown(&mut in_flight, &mut metadata, shutdown)
                .await;
    }

    if shutdown_observed {
        if started_hook_count > 0 || skipped_hook_count > 0 {
            warn!(
                started_terminal_hooks = started_hook_count,
                skipped_terminal_hooks_due_to_shutdown = skipped_hook_count,
                in_flight_terminal_hooks = in_flight.len(),
                shutdown_drain_timeout_ms = TERMINAL_HOOK_SHUTDOWN_DRAIN_TIMEOUT.as_millis(),
                "reaper terminal hook fanout interrupted by shutdown; draining in-flight hooks with bounded budget"
            );
        }
        drain_terminal_hooks_for_shutdown(&mut in_flight, &mut metadata).await;
        clear_stale_hook_metadata(&mut metadata);
        return TerminalHookFanoutResult::InterruptedByShutdown {
            started: started_hook_count,
            skipped: skipped_hook_count,
        };
    }

    clear_stale_hook_metadata(&mut metadata);
    TerminalHookFanoutResult::Completed {
        started: started_hook_count,
    }
}

fn observe_shutdown_and_grant_admission_budget(
    shutdown_observed: &mut bool,
    post_shutdown_admission_budget: &mut usize,
) {
    if *shutdown_observed {
        return;
    }

    *shutdown_observed = true;
    *post_shutdown_admission_budget = REAPER_TERMINAL_HOOK_MAX_CONCURRENCY;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum TerminalHookFanoutResult {
    Completed { started: usize },
    InterruptedByShutdown { started: usize, skipped: usize },
}

impl TerminalHookFanoutResult {
    pub(super) fn interrupted_by_shutdown(self) -> bool {
        matches!(self, Self::InterruptedByShutdown { .. })
    }
}

#[derive(Debug)]
enum HookOutcome {
    Completed,
    TimedOut,
    Panicked(String),
}

#[derive(Debug)]
struct HookMetadata {
    job_id: String,
    job_type: String,
    run_number: i32,
    attempt: i32,
}

type HookJoinResult = std::result::Result<(Id, HookOutcome), tokio::task::JoinError>;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HookWaitOutcome {
    HookCompleted,
    ShutdownRequested,
}

async fn wait_for_hook_capacity_or_shutdown(
    in_flight: &mut JoinSet<HookOutcome>,
    metadata: &mut HashMap<Id, HookMetadata>,
    shutdown: &mut watch::Receiver<bool>,
) -> HookWaitOutcome {
    while in_flight.len() >= REAPER_TERMINAL_HOOK_MAX_CONCURRENCY {
        tokio::select! {
            result = in_flight.join_next_with_id() => {
                if let Some(result) = result {
                    handle_next_hook_result(result, metadata);
                    return HookWaitOutcome::HookCompleted;
                }
            }
            changed = shutdown.changed() => {
                if changed.is_err() || *shutdown.borrow() {
                    return HookWaitOutcome::ShutdownRequested;
                }
            }
        }
    }

    HookWaitOutcome::HookCompleted
}

async fn drain_hook_results_until_complete_or_shutdown(
    in_flight: &mut JoinSet<HookOutcome>,
    metadata: &mut HashMap<Id, HookMetadata>,
    shutdown: &mut watch::Receiver<bool>,
) -> bool {
    while !in_flight.is_empty() {
        tokio::select! {
            result = in_flight.join_next_with_id() => {
                if let Some(result) = result {
                    handle_next_hook_result(result, metadata);
                }
            }
            changed = shutdown.changed() => {
                if changed.is_err() || *shutdown.borrow() {
                    return true;
                }
            }
        }
    }

    false
}

async fn drain_terminal_hooks_for_shutdown(
    in_flight: &mut JoinSet<HookOutcome>,
    metadata: &mut HashMap<Id, HookMetadata>,
) {
    if in_flight.is_empty() {
        return;
    }

    info!(
        in_flight_terminal_hooks = in_flight.len(),
        timeout_ms = TERMINAL_HOOK_SHUTDOWN_DRAIN_TIMEOUT.as_millis(),
        "shutdown requested; draining in-flight reaper terminal hooks"
    );

    match timeout(
        TERMINAL_HOOK_SHUTDOWN_DRAIN_TIMEOUT,
        drain_hook_results_to_completion(in_flight, metadata),
    )
    .await
    {
        Ok(()) => {}
        Err(_) => {
            warn!(
                remaining_terminal_hooks = in_flight.len(),
                timeout_ms = TERMINAL_HOOK_SHUTDOWN_DRAIN_TIMEOUT.as_millis(),
                "reaper terminal hooks did not finish before shutdown drain deadline; aborting"
            );
            in_flight.abort_all();

            match timeout(
                TERMINAL_HOOK_ABORT_DRAIN_TIMEOUT,
                drain_hook_results_to_completion(in_flight, metadata),
            )
            .await
            {
                Ok(()) => {}
                Err(_) => {
                    warn!(
                        remaining_terminal_hooks = in_flight.len(),
                        timeout_ms = TERMINAL_HOOK_ABORT_DRAIN_TIMEOUT.as_millis(),
                        "reaper terminal hook abort drain timed out during shutdown; dropping unresolved tasks"
                    );
                }
            }
        }
    }
}

async fn drain_hook_results_to_completion(
    in_flight: &mut JoinSet<HookOutcome>,
    metadata: &mut HashMap<Id, HookMetadata>,
) {
    while let Some(result) = in_flight.join_next_with_id().await {
        handle_next_hook_result(result, metadata);
    }
}

fn clear_stale_hook_metadata(metadata: &mut HashMap<Id, HookMetadata>) {
    if metadata.is_empty() {
        return;
    }

    warn!(
        stale_hook_metadata_entries = metadata.len(),
        "reaper hook metadata diverged from in-flight task set; clearing stale metadata"
    );
    metadata.clear();
}

fn handle_next_hook_result(result: HookJoinResult, metadata: &mut HashMap<Id, HookMetadata>) {
    match result {
        Ok((id, HookOutcome::Completed)) => {
            if metadata.remove(&id).is_none() {
                warn!("terminal failure hook completed; metadata missing in reaper loop");
            }
        }
        Ok((id, HookOutcome::TimedOut)) => {
            if let Some(meta) = metadata.remove(&id) {
                warn!(
                    job_id = meta.job_id,
                    job_type = meta.job_type,
                    run_number = meta.run_number,
                    attempt = meta.attempt,
                    timeout_ms = TERMINAL_HOOK_TIMEOUT.as_millis(),
                    "terminal failure hook timed out; continuing reaper loop"
                );
            } else {
                warn!(
                    timeout_ms = TERMINAL_HOOK_TIMEOUT.as_millis(),
                    "terminal failure hook timed out; metadata missing in reaper loop"
                );
            }
        }
        Ok((id, HookOutcome::Panicked(panic_message))) => {
            if let Some(meta) = metadata.remove(&id) {
                warn!(
                    job_id = meta.job_id,
                    job_type = meta.job_type,
                    run_number = meta.run_number,
                    attempt = meta.attempt,
                    panic = %panic_message,
                    "terminal failure hook panicked; continuing reaper loop"
                );
            } else {
                warn!(
                    panic = %panic_message,
                    "terminal failure hook panicked; metadata missing in reaper loop"
                );
            }
        }
        Err(error) => {
            let id = error.id();
            if let Some(meta) = metadata.remove(&id) {
                if error.is_panic() {
                    warn!(
                        job_id = meta.job_id,
                        job_type = meta.job_type,
                        run_number = meta.run_number,
                        attempt = meta.attempt,
                        error = %error,
                        "terminal failure hook panicked; continuing reaper loop"
                    );
                } else if error.is_cancelled() {
                    warn!(
                        job_id = meta.job_id,
                        job_type = meta.job_type,
                        run_number = meta.run_number,
                        attempt = meta.attempt,
                        error = %error,
                        "terminal failure hook was cancelled; continuing reaper loop"
                    );
                } else {
                    warn!(
                        job_id = meta.job_id,
                        job_type = meta.job_type,
                        run_number = meta.run_number,
                        attempt = meta.attempt,
                        error = %error,
                        "terminal failure hook join failed; continuing reaper loop"
                    );
                }
            } else if error.is_panic() {
                warn!(
                    error = %error,
                    "terminal failure hook panicked; metadata missing in reaper loop"
                );
            } else if error.is_cancelled() {
                warn!(
                    error = %error,
                    "terminal failure hook was cancelled; metadata missing in reaper loop"
                );
            } else {
                warn!(
                    error = %error,
                    "terminal failure hook join failed; metadata missing in reaper loop"
                );
            }
        }
    }
}

fn panic_payload_message(panic_payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(message) = panic_payload.downcast_ref::<String>() {
        return message.clone();
    }

    if let Some(message) = panic_payload.downcast_ref::<&'static str>() {
        return (*message).to_string();
    }

    "non-string panic payload".to_string()
}