lilqueue 0.1.0

A small storage-agnostic async job queue runner
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
use super::*;
use async_trait::async_trait;
use std::{
    fs,
    sync::{Arc, Mutex},
    time::{Duration, SystemTime, UNIX_EPOCH},
};
use tempfile::tempdir;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct WriteFileJob {
    output_path: String,
    line: String,
}

#[async_trait]
impl Job for WriteFileJob {
    async fn process(&self) -> Result<(), JobError> {
        use std::io::Write;

        let mut file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.output_path)
            .map_err(|e| JobError::permanent(e.to_string()))?;
        writeln!(file, "{}", self.line).map_err(|e| JobError::permanent(e.to_string()))?;
        Ok(())
    }
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct FlakyJob {
    state_path: String,
    succeed_on_attempt: u32,
}

#[async_trait]
impl Job for FlakyJob {
    async fn process(&self) -> Result<(), JobError> {
        let attempt = fs::read_to_string(&self.state_path)
            .ok()
            .and_then(|v| v.trim().parse::<u32>().ok())
            .unwrap_or(0)
            .saturating_add(1);

        fs::write(&self.state_path, attempt.to_string())
            .map_err(|e| JobError::permanent(e.to_string()))?;

        if attempt < self.succeed_on_attempt {
            return Err(JobError::retryable("transient failure"));
        }

        Ok(())
    }
}

#[tokio::test]
async fn processes_successful_job() {
    let dir = tempdir().unwrap();
    let queue = MemoryQueue::default();
    let processor = JobProcessor::<WriteFileJob, _>::new(queue.clone(), test_options());

    let output_path = dir.path().join("output.log");
    let job = WriteFileJob {
        output_path: output_path.to_string_lossy().to_string(),
        line: "hello".to_string(),
    };

    let id = processor.enqueue(&job).await.unwrap();
    let outcome = processor.run_once().await.unwrap();

    assert_eq!(
        outcome,
        RunOutcome::Completed {
            job_id: id,
            attempts: 1,
        }
    );

    let file_contents = fs::read_to_string(output_path).unwrap();
    assert!(file_contents.contains("hello"));
    assert_eq!(queue.job(id).status, MemoryStatus::Completed);

    let idle = processor.run_once().await.unwrap();
    assert_eq!(idle, RunOutcome::Idle);
}

#[tokio::test]
async fn retries_and_then_completes() {
    let dir = tempdir().unwrap();
    let queue = MemoryQueue::default();

    let mut options = test_options();
    options.max_attempts = 5;
    options.backoff = BackoffStrategy::Fixed(Duration::ZERO);

    let processor = JobProcessor::<FlakyJob, _>::new(queue.clone(), options);

    let state_path = dir.path().join("attempt.txt");
    let job = FlakyJob {
        state_path: state_path.to_string_lossy().to_string(),
        succeed_on_attempt: 2,
    };

    let job_id = processor.enqueue(&job).await.unwrap();

    let first = processor.run_once().await.unwrap();
    assert!(matches!(first, RunOutcome::Retried { attempts: 1, .. }));
    assert_eq!(queue.job(job_id).status, MemoryStatus::Queued);
    assert_eq!(
        queue.job(job_id).last_error.as_deref(),
        Some("transient failure")
    );

    let second = processor.run_once().await.unwrap();
    assert!(matches!(second, RunOutcome::Completed { attempts: 2, .. }));
    assert_eq!(queue.job(job_id).status, MemoryStatus::Completed);

    let attempts_file = fs::read_to_string(state_path).unwrap();
    assert_eq!(attempts_file.trim(), "2");
}

#[tokio::test]
async fn fails_permanent_job_without_retry() {
    #[derive(Debug, serde::Serialize, serde::Deserialize)]
    struct PermanentFailJob;

    #[async_trait]
    impl Job for PermanentFailJob {
        async fn process(&self) -> Result<(), JobError> {
            Err(JobError::permanent("bad payload"))
        }
    }

    let queue = MemoryQueue::default();
    let processor = JobProcessor::<PermanentFailJob, _>::new(queue.clone(), test_options());

    let job_id = processor.enqueue(&PermanentFailJob).await.unwrap();
    let outcome = processor.run_once().await.unwrap();

    assert_eq!(
        outcome,
        RunOutcome::Failed {
            job_id,
            attempts: 1,
            error: "bad payload".to_string(),
        }
    );
    assert_eq!(queue.job(job_id).status, MemoryStatus::Failed);
}

#[tokio::test]
async fn delayed_job_is_not_claimed_until_available() {
    let dir = tempdir().unwrap();
    let queue = MemoryQueue::default();
    let processor = JobProcessor::<WriteFileJob, _>::new(queue.clone(), test_options());

    let output_path = dir.path().join("delayed.log");
    processor
        .enqueue_with_delay(
            &WriteFileJob {
                output_path: output_path.to_string_lossy().to_string(),
                line: "delayed".to_string(),
            },
            Duration::from_secs(60),
        )
        .await
        .unwrap();

    let outcome = processor.run_once().await.unwrap();
    assert_eq!(outcome, RunOutcome::Idle);
    assert!(!output_path.exists());
}

#[tokio::test]
async fn run_until_notified_wakes_when_job_is_enqueued() {
    let dir = tempdir().unwrap();
    let queue = MemoryQueue::default();

    let mut options = test_options();
    options.poll_interval = Duration::from_secs(60);

    let processor = JobProcessor::<WriteFileJob, _>::new(queue, options);

    let output_path = dir.path().join("notify.log");
    let output_path_str = output_path.to_string_lossy().to_string();

    let shutdown = Arc::new(tokio::sync::Notify::new());
    let worker_processor = processor.clone();
    let worker_shutdown = Arc::clone(&shutdown);
    let worker = tokio::spawn(async move {
        worker_processor
            .run_until_notified(worker_shutdown.as_ref())
            .await
            .unwrap();
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    processor
        .enqueue(&WriteFileJob {
            output_path: output_path_str,
            line: "wake".to_string(),
        })
        .await
        .unwrap();

    tokio::time::timeout(Duration::from_secs(2), async {
        loop {
            if fs::read_to_string(&output_path)
                .map(|contents| contents.contains("wake"))
                .unwrap_or(false)
            {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .unwrap();

    shutdown.notify_waiters();
    tokio::time::timeout(Duration::from_secs(2), worker)
        .await
        .unwrap()
        .unwrap();
}

#[tokio::test]
async fn spawn_worker_processes_job_and_can_shutdown() {
    let dir = tempdir().unwrap();
    let queue = MemoryQueue::default();

    let mut options = test_options();
    options.poll_interval = Duration::from_secs(60);

    let processor = JobProcessor::<WriteFileJob, _>::new(queue, options);
    let worker = processor.spawn_worker();

    let output_path = dir.path().join("spawn.log");
    processor
        .enqueue(&WriteFileJob {
            output_path: output_path.to_string_lossy().to_string(),
            line: "spawned".to_string(),
        })
        .await
        .unwrap();

    tokio::time::timeout(Duration::from_secs(2), async {
        loop {
            if fs::read_to_string(&output_path)
                .map(|contents| contents.contains("spawned"))
                .unwrap_or(false)
            {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .unwrap();

    tokio::time::timeout(Duration::from_secs(2), worker.shutdown_and_wait())
        .await
        .unwrap();
}

fn test_options() -> ProcessorOptions {
    ProcessorOptions {
        max_attempts: 3,
        poll_interval: Duration::from_millis(1),
        backoff: BackoffStrategy::Fixed(Duration::ZERO),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MemoryStatus {
    Queued,
    Processing,
    Completed,
    Failed,
}

#[derive(Debug, Clone)]
struct MemoryJob {
    id: i64,
    job_type: String,
    payload: String,
    status: MemoryStatus,
    attempts: u32,
    max_attempts: u32,
    available_at: i64,
    last_error: Option<String>,
}

#[derive(Debug, Clone)]
struct MemoryClaim {
    id: i64,
}

#[derive(Debug, Default)]
struct MemoryState {
    next_id: i64,
    jobs: Vec<MemoryJob>,
}

#[derive(Debug, Clone, Default)]
struct MemoryQueue {
    inner: Arc<Mutex<MemoryState>>,
}

impl MemoryQueue {
    fn job(&self, id: i64) -> MemoryJob {
        self.inner
            .lock()
            .unwrap()
            .jobs
            .iter()
            .find(|job| job.id == id)
            .unwrap()
            .clone()
    }
}

#[async_trait]
impl JobQueue for MemoryQueue {
    async fn enqueue(&self, job: NewJob) -> QueueResult<i64> {
        let mut state = self.inner.lock().unwrap();
        state.next_id = state.next_id.saturating_add(1);
        let id = state.next_id;
        state.jobs.push(MemoryJob {
            id,
            job_type: job.job_type,
            payload: job.payload,
            status: MemoryStatus::Queued,
            attempts: 0,
            max_attempts: job.max_attempts,
            available_at: job.available_at,
            last_error: None,
        });
        Ok(id)
    }

    async fn next_wakeup_at(&self, job_type: &str) -> QueueResult<Option<i64>> {
        let state = self.inner.lock().unwrap();
        Ok(state
            .jobs
            .iter()
            .filter(|job| job.job_type == job_type && job.status == MemoryStatus::Queued)
            .map(|job| job.available_at)
            .min())
    }
}

#[async_trait]
impl LockableQueue for MemoryQueue {
    type Claim = MemoryClaim;

    async fn claim(&self, job_type: &str) -> QueueResult<Option<ClaimedJob<Self::Claim>>> {
        let now = current_epoch_seconds();
        let mut state = self.inner.lock().unwrap();
        let Some(job) = state
            .jobs
            .iter_mut()
            .filter(|job| {
                job.job_type == job_type
                    && job.status == MemoryStatus::Queued
                    && job.available_at <= now
            })
            .min_by_key(|job| (job.available_at, job.id))
        else {
            return Ok(None);
        };

        job.status = MemoryStatus::Processing;
        job.attempts = job.attempts.saturating_add(1);

        Ok(Some(ClaimedJob {
            id: job.id,
            payload: job.payload.clone(),
            attempts: job.attempts,
            max_attempts: job.max_attempts,
            claim: MemoryClaim { id: job.id },
        }))
    }

    async fn complete(&self, job: ClaimedJob<Self::Claim>) -> QueueResult<()> {
        let mut state = self.inner.lock().unwrap();
        let Some(stored) = state
            .jobs
            .iter_mut()
            .find(|stored| stored.id == job.claim.id)
        else {
            return Err(std::io::Error::other("claimed job not found").into());
        };
        stored.status = MemoryStatus::Completed;
        stored.last_error = None;
        Ok(())
    }
}

#[async_trait]
impl RetryableQueue for MemoryQueue {
    async fn retry(
        &self,
        job: ClaimedJob<Self::Claim>,
        next_run_at: i64,
        error: String,
    ) -> QueueResult<()> {
        let mut state = self.inner.lock().unwrap();
        let Some(stored) = state
            .jobs
            .iter_mut()
            .find(|stored| stored.id == job.claim.id)
        else {
            return Err(std::io::Error::other("claimed job not found").into());
        };
        stored.status = MemoryStatus::Queued;
        stored.available_at = next_run_at;
        stored.last_error = Some(error);
        Ok(())
    }

    async fn fail(&self, job: ClaimedJob<Self::Claim>, error: String) -> QueueResult<()> {
        let mut state = self.inner.lock().unwrap();
        let Some(stored) = state
            .jobs
            .iter_mut()
            .find(|stored| stored.id == job.claim.id)
        else {
            return Err(std::io::Error::other("claimed job not found").into());
        };
        stored.status = MemoryStatus::Failed;
        stored.last_error = Some(error);
        Ok(())
    }
}

fn current_epoch_seconds() -> i64 {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    i64::try_from(secs).unwrap_or(i64::MAX)
}