later 0.0.27

Distributed Background jobs manager and runner for Rust
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
#![cfg(feature = "sqlite")]

use async_std::sync::{Arc, Mutex};
use later::{backend::SqliteBackend, storage::Sqlite, BackgroundJobServer, Config};
use serde::{Deserialize, Serialize};
use std::{
    sync::atomic::{AtomicBool, AtomicUsize, Ordering},
    time::{Duration, Instant},
};

#[derive(Debug, Clone)]
struct Event {
    label: &'static str,
    start: Instant,
    end: Instant,
}

#[derive(Default)]
struct Concurrency {
    current: AtomicUsize,
    peak: AtomicUsize,
}

impl Concurrency {
    fn enter(&self) {
        let now = self.current.fetch_add(1, Ordering::SeqCst) + 1;
        self.peak.fetch_max(now, Ordering::SeqCst);
    }

    fn exit(&self) {
        self.current.fetch_sub(1, Ordering::SeqCst);
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct SequentialTick;

#[derive(Serialize, Deserialize, Debug, Clone)]
struct SequentialContinuation;

#[derive(Serialize, Deserialize, Debug, Clone)]
struct NormalTick;

struct AppContext {
    events: Arc<Mutex<Vec<Event>>>,
    tick_sleep: Duration,
    chain_continuation_once: Arc<AtomicBool>,
    concurrency: Arc<Concurrency>,
}

later::background_job! {
    struct Jobs {
        sequential_tick: SequentialTick,
        sequential_continuation: SequentialContinuation,
        normal_tick: NormalTick,
    }
}

async fn handle_sequential_tick(
    ctx: JobsContext<AppContext>,
    _payload: SequentialTick,
) -> anyhow::Result<()> {
    if ctx
        .app
        .chain_continuation_once
        .swap(false, Ordering::SeqCst)
    {
        ctx.enqueue_recurring_continue(ctx.job_id().clone(), SequentialContinuation)
            .await?;
    }

    let start = Instant::now();
    tokio::time::sleep(ctx.app.tick_sleep).await;
    let end = Instant::now();
    ctx.app.events.lock().await.push(Event {
        label: "tick",
        start,
        end,
    });
    Ok(())
}

async fn handle_sequential_continuation(
    ctx: JobsContext<AppContext>,
    _payload: SequentialContinuation,
) -> anyhow::Result<()> {
    let now = Instant::now();
    ctx.app.events.lock().await.push(Event {
        label: "continuation",
        start: now,
        end: now,
    });
    Ok(())
}

async fn handle_normal_tick(
    ctx: JobsContext<AppContext>,
    _payload: NormalTick,
) -> anyhow::Result<()> {
    ctx.app.concurrency.enter();
    tokio::time::sleep(ctx.app.tick_sleep).await;
    ctx.app.concurrency.exit();
    Ok(())
}

async fn start_server(
    namespace: &str,
    pool: sqlx::SqlitePool,
    events: Arc<Mutex<Vec<Event>>>,
    concurrency: Arc<Concurrency>,
    tick_sleep: Duration,
) -> BackgroundJobServer<AppContext, Jobs<AppContext>> {
    let backend = SqliteBackend::from_pool(namespace, pool)
        .await
        .expect("create sqlite backend");
    let ctx = AppContext {
        events,
        tick_sleep,
        chain_continuation_once: Arc::new(AtomicBool::new(true)),
        concurrency,
    };
    JobsBuilder::new(
        Config::builder()
            .context(ctx)
            .backend(Box::new(backend))
            .worker_count(4)
            .default_retry_policy(later::retry::RetryPolicy::immediate(6))
            .recurring_sequential_partitions(4)
            .build(),
    )
    .with_sequential_tick_handler(handle_sequential_tick)
    .with_sequential_continuation_handler(handle_sequential_continuation)
    .with_normal_tick_handler(handle_normal_tick)
    .build()
    .await
    .expect("start job server")
}

async fn wait_until_events(events: &Arc<Mutex<Vec<Event>>>, count: usize, timeout: Duration) {
    let start = Instant::now();
    while start.elapsed() < timeout {
        if events.lock().await.len() >= count {
            return;
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

async fn wait_until_peak(concurrency: &Arc<Concurrency>, peak: usize, timeout: Duration) {
    let start = Instant::now();
    while start.elapsed() < timeout {
        if concurrency.peak.load(Ordering::SeqCst) >= peak {
            return;
        }
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

/// Every second's occurrence sleeps well past the next tick. The recurring
/// scheduler must never let two occurrences run concurrently, and must not
/// drop the ticks that pile up behind a slow one.
#[tokio::test]
async fn sequential_recurring_jobs_never_overlap_and_queue_a_backlog() -> anyhow::Result<()> {
    let pool = Sqlite::new("sqlite::memory:").await?.pool().clone();
    let events = Arc::new(Mutex::new(Vec::new()));
    let concurrency = Arc::new(Concurrency::default());
    let server = start_server(
        "recurring-sequential-overlap",
        pool,
        events.clone(),
        concurrency,
        Duration::from_millis(2500),
    )
    .await;

    server
        .enqueue_recurring_sequential(
            "overlap-test".to_string(),
            SequentialTick,
            "* * * * * *".to_string(),
        )
        .await?;

    wait_until_events(&events, 3, Duration::from_secs(30)).await;

    let recorded = events.lock().await.clone();
    assert!(
        recorded.len() >= 3,
        "expected at least 3 occurrences to run despite a 2.5s handler on a 1s schedule, got {}",
        recorded.len()
    );
    for pair in recorded.windows(2) {
        let [a, b] = pair else { unreachable!() };
        assert!(
            a.end <= b.start,
            "occurrences overlapped: {:?} finished after {:?} started",
            a,
            b
        );
    }

    let _ = server.shutdown(Duration::from_secs(5)).await;
    Ok(())
}

/// A continuation chained off a sequential occurrence's own job id (from
/// inside its handler) must run as part of that occurrence's chain: after
/// its parent, and before the next scheduled occurrence.
#[tokio::test]
async fn sequential_recurring_continuation_runs_as_part_of_the_chain() -> anyhow::Result<()> {
    let pool = Sqlite::new("sqlite::memory:").await?.pool().clone();
    let events = Arc::new(Mutex::new(Vec::new()));
    let concurrency = Arc::new(Concurrency::default());
    let server = start_server(
        "recurring-sequential-continuation",
        pool,
        events.clone(),
        concurrency,
        Duration::from_millis(50),
    )
    .await;

    server
        .enqueue_recurring_sequential(
            "continuation-test".to_string(),
            SequentialTick,
            "*/3 * * * * *".to_string(),
        )
        .await?;

    wait_until_events(&events, 3, Duration::from_secs(20)).await;

    let recorded = events.lock().await.clone();
    assert!(
        recorded.len() >= 3,
        "expected at least [tick, continuation, tick], got {:?}",
        recorded
    );
    assert_eq!(recorded[0].label, "tick");
    assert_eq!(recorded[1].label, "continuation");
    assert_eq!(
        recorded[2].label, "tick",
        "the continuation must run before the next scheduled occurrence"
    );

    let _ = server.shutdown(Duration::from_secs(2)).await;
    Ok(())
}

/// Normal-mode recurring jobs run on the plain unordered path, unlike
/// sequential ones: nothing about being a recurring occurrence should make a
/// job serialize against other concurrent work. A far-future cron keeps the
/// proactive poller from creating a second occurrence during the test, so
/// the overlap is forced deterministically with a sibling job due at
/// roughly the same time as the recurring job's first occurrence, rather
/// than by racing the (comparatively slow and jittery) recurring-job poller
/// cadence.
#[tokio::test]
async fn normal_recurring_jobs_may_overlap() -> anyhow::Result<()> {
    let pool = Sqlite::new("sqlite::memory:").await?.pool().clone();
    let events = Arc::new(Mutex::new(Vec::new()));
    let concurrency = Arc::new(Concurrency::default());
    let server = start_server(
        "recurring-normal-overlap",
        pool,
        events,
        concurrency.clone(),
        Duration::from_millis(3000),
    )
    .await;

    server
        .enqueue_recurring(
            "normal-overlap-test".to_string(),
            NormalTick,
            "* * * * * *".to_string(),
        )
        .await?;
    server
        .enqueue_delayed(NormalTick, Duration::from_millis(500))
        .await?;

    wait_until_peak(&concurrency, 2, Duration::from_secs(20)).await;
    assert!(
        concurrency.peak.load(Ordering::SeqCst) >= 2,
        "expected the recurring occurrence to overlap with a concurrently-due sibling job"
    );

    let _ = server.shutdown(Duration::from_secs(5)).await;
    Ok(())
}

/// Re-registering the same identifier must update the definition in place,
/// not add a second entry to the index the recurring-job poller scans, and
/// - the actual production bug this regression-tests - must not enqueue a
/// second occurrence either. Every process restart calls
/// `enqueue_recurring_sequential` again to recreate its schedule; if that
/// unconditionally enqueued a fresh occurrence, a long-running deployment
/// that restarts often would permanently queue one more job per restart,
/// forever, each blocking every other job sharing its reserved-topic
/// partition until its turn came - exactly what happened in production
/// (a daily job's restart-accumulated occurrences blocked a once-a-minute
/// job behind them for hours).
#[tokio::test]
async fn reregistering_a_sequential_recurring_job_does_not_duplicate_its_schedule_entry(
) -> anyhow::Result<()> {
    let pool = Sqlite::new("sqlite::memory:").await?.pool().clone();
    let events = Arc::new(Mutex::new(Vec::new()));
    let concurrency = Arc::new(Concurrency::default());
    let server = start_server(
        "recurring-upsert",
        pool.clone(),
        events,
        concurrency,
        Duration::from_millis(50),
    )
    .await;

    let first = server
        .enqueue_recurring_sequential(
            "upsert-test".to_string(),
            SequentialTick,
            "*/30 * * * * *".to_string(),
        )
        .await?;
    assert!(
        first.is_some(),
        "a brand-new identifier must enqueue its first occurrence"
    );

    let second = server
        .enqueue_recurring_sequential(
            "upsert-test".to_string(),
            SequentialTick,
            "*/30 * * * * *".to_string(),
        )
        .await?;
    assert!(
        second.is_none(),
        "re-registering the same identifier on the same schedule must not enqueue another occurrence"
    );

    let count: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM later_storage_range WHERE range_key = 'later-recurring-upsert-all-recurring-jobs'",
    )
    .fetch_one(&pool)
    .await?;
    assert_eq!(
        count, 1,
        "upserting the same identifier must not add a duplicate schedule entry"
    );

    // At most 1, not exactly 1: the single legitimate occurrence may have
    // already been claimed and run (this cron is due within 30s and the
    // handler is fast) by the time this check runs - that race is fine.
    // What must never happen is a *second* occurrence queued behind it,
    // which is what `queued == 2` would mean.
    let queued: i64 = sqlx::query_scalar(
        "SELECT COUNT(*) FROM later_partition_job WHERE namespace = 'later-recurring-upsert' AND topic = '__later_recurring__'",
    )
    .fetch_one(&pool)
    .await?;
    assert!(
        queued <= 1,
        "re-registering the same identifier must not leave a second occurrence queued behind the first, got {queued}"
    );

    let _ = server.shutdown(Duration::from_secs(2)).await;
    Ok(())
}

/// Re-registering with a *different* cron schedule is treated as a real
/// change, not a no-op upsert - it must still enqueue a fresh occurrence
/// under the new schedule, unlike re-registering with the same schedule.
#[tokio::test]
async fn reregistering_with_a_changed_cron_enqueues_a_fresh_occurrence() -> anyhow::Result<()> {
    let pool = Sqlite::new("sqlite::memory:").await?.pool().clone();
    let events = Arc::new(Mutex::new(Vec::new()));
    let concurrency = Arc::new(Concurrency::default());
    let server = start_server(
        "recurring-cron-change",
        pool,
        events,
        concurrency,
        Duration::from_millis(50),
    )
    .await;

    let first = server
        .enqueue_recurring_sequential(
            "cron-change-test".to_string(),
            SequentialTick,
            "*/30 * * * * *".to_string(),
        )
        .await?;
    assert!(first.is_some());

    let second = server
        .enqueue_recurring_sequential(
            "cron-change-test".to_string(),
            SequentialTick,
            "0 * * * * *".to_string(),
        )
        .await?;
    assert!(
        second.is_some(),
        "a changed cron schedule must enqueue a fresh occurrence, not be treated as a no-op"
    );

    let _ = server.shutdown(Duration::from_secs(2)).await;
    Ok(())
}

/// Two servers sharing one database and namespace - the normal shape of a
/// horizontally-scaled deployment - registering the exact same brand-new
/// recurring identifier at the same moment must not both decide "I'm
/// first": exactly one enqueues the first occurrence, and the other
/// defers to it.
#[tokio::test]
async fn simultaneous_first_registration_from_two_servers_enqueues_only_once() -> anyhow::Result<()>
{
    let pool = Sqlite::new("sqlite::memory:").await?.pool().clone();
    let namespace = "recurring-simultaneous-registration";
    let first_server = start_server(
        namespace,
        pool.clone(),
        Arc::new(Mutex::new(Vec::new())),
        Arc::new(Concurrency::default()),
        Duration::from_millis(50),
    )
    .await;
    let second_server = start_server(
        namespace,
        pool,
        Arc::new(Mutex::new(Vec::new())),
        Arc::new(Concurrency::default()),
        Duration::from_millis(50),
    )
    .await;

    let (first, second) = tokio::join!(
        first_server.enqueue_recurring_sequential(
            "cold-start-race".to_string(),
            SequentialTick,
            "*/30 * * * * *".to_string(),
        ),
        second_server.enqueue_recurring_sequential(
            "cold-start-race".to_string(),
            SequentialTick,
            "*/30 * * * * *".to_string(),
        ),
    );
    let outcomes = [first?.is_some(), second?.is_some()];
    assert_eq!(
        outcomes.iter().filter(|enqueued| **enqueued).count(),
        1,
        "exactly one of the two simultaneous first registrations must enqueue the first \
         occurrence, got {outcomes:?}"
    );

    let _ = first_server.shutdown(Duration::from_secs(2)).await;
    let _ = second_server.shutdown(Duration::from_secs(2)).await;
    Ok(())
}