cronoq-server 0.2.1

gRPC/HTTP server for the cronoq scheduler with remote workers
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
474
475
476
477
478
479
480
481
482
483
//! End-to-end behaviour of the server with remote workers.

mod common;

use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;

use common::*;
use cronoq_proto::v1 as pb;

#[tokio::test]
async fn grpc_submit_wait_with_remote_worker() {
    let server = TestServer::start("").await;
    let (_w, _j) = spawn_worker(&server, 2, Arc::default());
    server.wait_capacity(2).await;
    let mut c = server.tasks(CLIENT).await;

    let res = c
        .submit(submit(spec(b"hello"), true))
        .await
        .unwrap()
        .into_inner()
        .result
        .unwrap();
    assert_eq!(res.status, pb::TaskStatus::Succeeded as i32);
    assert_eq!(res.output.unwrap(), b"HELLO");
    assert!(res.output_available);

    let res = c
        .submit(submit(spec(b"fail"), true))
        .await
        .unwrap()
        .into_inner()
        .result
        .unwrap();
    assert_eq!(res.status, pb::TaskStatus::Failed as i32);
    assert!(res.message.contains("asked to fail"));

    // Batch with dependencies by key, then Wait on the last.
    let mut a = spec(b"a");
    a.key = "a".into();
    let mut b = spec(b"b");
    b.key = "b".into();
    b.deps.push(pb::Dep {
        target: by_key("a"),
        cond: pb::EdgeCond::OnSuccess as i32,
    });
    let r = c
        .submit_batch(pb::SubmitBatchRequest { tasks: vec![b, a] })
        .await
        .unwrap()
        .into_inner();
    assert_eq!(r.tasks.len(), 2);
    let res = c
        .wait(pb::WaitRequest {
            task: by_key("b"),
            timeout_s: Some(10.0),
        })
        .await
        .unwrap()
        .into_inner();
    assert_eq!(res.output.unwrap(), b"B");
    // Results stay available from the output cache after completion.
    let res = c
        .wait(pb::WaitRequest {
            task: by_key("a"),
            timeout_s: Some(1.0),
        })
        .await
        .unwrap()
        .into_inner();
    assert_eq!(res.status, pb::TaskStatus::Succeeded as i32);
    assert_eq!(res.output.unwrap(), b"A");

    // Rich task details.
    let info = c
        .get(pb::GetRequest { task: by_key("a") })
        .await
        .unwrap()
        .into_inner();
    assert_eq!(info.status, pb::TaskStatus::Succeeded as i32);
    assert_eq!(
        (info.pool.as_str(), info.key.as_str(), info.kind.as_str()),
        ("default", "a", "default")
    );
    assert_eq!((info.attempt, info.max_attempts), (1, 3));
    assert!(
        info.created_unix_ns.is_some()
            && info.started_unix_ns.is_some()
            && info.finished_unix_ns.is_some()
    );
    assert!(info.output_available);
    server.stop().await;
}

#[tokio::test]
async fn auth_is_enforced_per_role() {
    let server = TestServer::start("").await;
    let e = server
        .tasks(WORKER)
        .await
        .submit(submit(spec(b"x"), false))
        .await
        .unwrap_err();
    assert_eq!(e.code(), tonic::Code::PermissionDenied);
    let e = server
        .tasks("nope-nope-nope-nope-nope")
        .await
        .submit(submit(spec(b"x"), false))
        .await
        .unwrap_err();
    assert_eq!(e.code(), tonic::Code::Unauthenticated);
    assert!(
        server
            .tasks(ADMIN)
            .await
            .submit(submit(spec(b"x"), false))
            .await
            .is_ok()
    );
    // Snapshot needs the metrics role, which clients do not have.
    let e = server
        .admin(CLIENT)
        .await
        .snapshot(pb::Empty {})
        .await
        .unwrap_err();
    assert_eq!(e.code(), tonic::Code::PermissionDenied);
    assert!(
        server
            .admin(METRICS)
            .await
            .snapshot(pb::Empty {})
            .await
            .is_ok()
    );
    let e = server
        .admin(METRICS)
        .await
        .remove_recurring(pb::RemoveRecurringRequest { name: "x".into() })
        .await
        .unwrap_err();
    assert_eq!(e.code(), tonic::Code::PermissionDenied);

    let http = reqwest::Client::new();
    let r = http
        .post(server.http("/v1/tasks"))
        .json(&serde_json::json!({"payload": 1}))
        .send()
        .await
        .unwrap();
    assert_eq!(r.status(), 401);
    assert_eq!(r.headers()["www-authenticate"], "Bearer");
    let r = http.get(server.http("/readyz")).send().await.unwrap();
    assert_eq!(r.status(), 200);
    let r = http.get(server.http("/metrics")).send().await.unwrap();
    assert_eq!(r.status(), 401);
    let r = http
        .get(server.http("/metrics"))
        .bearer_auth(CLIENT)
        .send()
        .await
        .unwrap();
    assert_eq!(r.status(), 403);
    let r = http
        .get(server.http("/metrics"))
        .bearer_auth(METRICS)
        .send()
        .await
        .unwrap();
    assert_eq!(
        r.headers()["content-type"],
        "text/plain; version=0.0.4; charset=utf-8"
    );
    let body = r.text().await.unwrap();
    assert!(
        body.contains("cronoq_pool_workers{pool=\"default\"}"),
        "{body}"
    );
    server.stop().await;
}

#[tokio::test]
async fn http_json_api() {
    let server = TestServer::start("").await;
    let (_w, _j) = spawn_worker(&server, 1, Arc::default());
    server.wait_capacity(1).await;
    let http = reqwest::Client::new();
    let v: serde_json::Value = http
        .post(server.http("/v1/tasks"))
        .bearer_auth(CLIENT)
        .json(&serde_json::json!({"payload": "json-string", "priority": "high", "key": "k1", "wait": true}))
        .send()
        .await
        .unwrap()
        .json()
        .await
        .unwrap();
    assert_eq!(v["result"]["status"], "succeeded");
    assert!(v["id"].is_string(), "ids are strings: {v}");
    // The worker upper-cased the JSON text, which is still valid JSON.
    assert_eq!(v["result"]["output"], "JSON-STRING");
    let id = v["id"].as_str().unwrap().to_string();
    for path in [format!("/v1/tasks/{id}"), "/v1/tasks/by-key/k1".to_string()] {
        let v: serde_json::Value = http
            .get(server.http(&path))
            .bearer_auth(CLIENT)
            .send()
            .await
            .unwrap()
            .json()
            .await
            .unwrap();
        assert_eq!(v["status"], "succeeded", "{path}: {v}");
        assert_eq!(v["id"], id.as_str());
        assert_eq!(v["key"], "k1");
    }
    let r = http
        .post(server.http("/v1/tasks"))
        .bearer_auth(CLIENT)
        .json(&serde_json::json!({"payload": 1, "priority": "urgent"}))
        .send()
        .await
        .unwrap();
    assert_eq!(r.status(), 400);
    let r = http
        .post(server.http("/v1/tasks"))
        .bearer_auth(CLIENT)
        .json(&serde_json::json!({"payload": 1, "pool": "missing"}))
        .send()
        .await
        .unwrap();
    assert_eq!(r.status(), 404);
    server.stop().await;
}

#[tokio::test]
async fn disconnected_worker_leases_are_retried_elsewhere() {
    let server = TestServer::start("").await;
    let seen_a = Arc::new(AtomicU32::new(0));
    let (stop_a, join_a) = spawn_worker(&server, 1, seen_a.clone());
    server.wait_capacity(1).await;
    let mut c = server.tasks(CLIENT).await;
    // "slow" goes to worker A, which then stops mid-attempt.
    let id = c
        .submit(submit(spec(b"slow"), false))
        .await
        .unwrap()
        .into_inner()
        .id;
    for _ in 0..100 {
        if seen_a.load(Ordering::SeqCst) > 0 {
            break;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    let _ = stop_a.send(());
    // The drain waits for the slow handler; the worker gives up after its drain timeout, but
    // the server sees the disconnect when the task is aborted.
    join_a.abort();
    server.wait_capacity(0).await;
    let info = c
        .get(pb::GetRequest { task: by_id(id) })
        .await
        .unwrap()
        .into_inner();
    assert!(
        [
            pb::TaskStatus::Retrying as i32,
            pb::TaskStatus::Ready as i32
        ]
        .contains(&info.status),
        "status {}",
        info.status
    );
    server.stop().await;
}

#[tokio::test]
async fn timeouts_abort_remote_attempts() {
    let server = TestServer::start("").await;
    let seen = Arc::new(AtomicU32::new(0));
    let (_w, _j) = spawn_worker(&server, 1, seen.clone());
    server.wait_capacity(1).await;
    let mut c = server.tasks(CLIENT).await;
    let mut s = spec(b"slow");
    s.timeout_s = Some(0.2);
    s.retry = Some(pb::RetryPolicy {
        max_attempts: 2,
        initial_backoff_s: 0.01,
        ..Default::default()
    });
    let r = c.submit(submit(s, true)).await.unwrap().into_inner();
    assert_eq!(
        r.result.unwrap().status,
        pb::TaskStatus::DeadLettered as i32
    );
    assert_eq!(seen.load(Ordering::SeqCst), 2);
    // The slot was released by the aborts: new work still runs.
    let r = c
        .submit(submit(spec(b"ok"), true))
        .await
        .unwrap()
        .into_inner();
    assert_eq!(r.result.unwrap().output.unwrap(), b"OK");
    server.stop().await;
}

#[tokio::test]
async fn watch_streams_events_and_admin_snapshot() {
    let server = TestServer::start("").await;
    let (_w, _j) = spawn_worker(&server, 1, Arc::default());
    server.wait_capacity(1).await;
    let mut c = server.tasks(CLIENT).await;
    let mut events = c
        .watch(pb::WatchRequest::default())
        .await
        .unwrap()
        .into_inner();
    let mut all = c
        .watch(pb::WatchRequest {
            all_transitions: true,
            ..Default::default()
        })
        .await
        .unwrap()
        .into_inner();
    let id = c
        .submit(submit(spec(b"e"), true))
        .await
        .unwrap()
        .into_inner()
        .id;
    let ev = tokio::time::timeout(Duration::from_secs(5), events.message())
        .await
        .unwrap()
        .unwrap()
        .unwrap();
    assert_eq!(ev.id, id);
    assert_eq!(ev.status, pb::TaskStatus::Succeeded as i32);
    assert_eq!(ev.pool, "default");
    let mut statuses = Vec::new();
    while statuses.last() != Some(&(pb::TaskStatus::Succeeded as i32)) {
        let ev = tokio::time::timeout(Duration::from_secs(5), all.message())
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        statuses.push(ev.status);
    }
    assert!(
        statuses.contains(&(pb::TaskStatus::Running as i32)),
        "{statuses:?}"
    );

    let snap = server
        .admin(ADMIN)
        .await
        .snapshot(pb::Empty {})
        .await
        .unwrap()
        .into_inner();
    assert_eq!(snap.pools[0].succeeded, 1);
    assert_eq!(snap.workers_connected, 1);
    assert_eq!(snap.totals.unwrap().succeeded, 1);
    server.stop().await;
}

#[tokio::test]
async fn aborted_slot_is_reusable_immediately() {
    // One slot: when the slow task times out, the queued task is dispatched into the same
    // slot in the same engine step. It must not fail for lack of a free worker.
    let server = TestServer::start("").await;
    let (_w, _j) = spawn_worker(&server, 1, Arc::default());
    server.wait_capacity(1).await;
    let mut c = server.tasks(CLIENT).await;
    let no_retry = Some(pb::RetryPolicy {
        max_attempts: 1,
        ..Default::default()
    });
    let mut slow = spec(b"slow");
    slow.timeout_s = Some(0.3);
    slow.retry = no_retry;
    let mut next = spec(b"next");
    next.retry = no_retry;
    c.submit(submit(slow, false)).await.unwrap();
    let res = c
        .submit(submit(next, true))
        .await
        .unwrap()
        .into_inner()
        .result
        .unwrap();
    assert_eq!(
        res.status,
        pb::TaskStatus::Succeeded as i32,
        "{}",
        res.message
    );
    server.stop().await;
}

#[tokio::test]
async fn recurring_schedules_via_admin() {
    let server = TestServer::start("").await;
    let mut admin = server.admin(ADMIN).await;
    admin
        .add_recurring(pb::RecurringRequest {
            name: "nightly".into(),
            cron: "0 0 3 * * *".into(),
            tz: "UTC".into(),
            template: Some(spec(b"x")),
            overlap: pb::Overlap::Queue as i32,
        })
        .await
        .unwrap();
    // A relative deadline and a key prefix are per occurrence; absolute times and
    // dependencies describe a single task and are refused.
    let mut per_occurrence = spec(b"x");
    per_occurrence.key = "nightly".into();
    per_occurrence.deadline_in_s = Some(60.0);
    let mut absolute = spec(b"x");
    absolute.deadline_unix_ns = Some(1);
    let mut with_dep = spec(b"x");
    with_dep.deps.push(pb::Dep {
        target: by_key("other"),
        cond: pb::EdgeCond::OnSuccess as i32,
    });
    for (template, ok) in [(per_occurrence, true), (absolute, false), (with_dep, false)] {
        let r = admin
            .add_recurring(pb::RecurringRequest {
                name: "nightly".into(),
                cron: "0 0 3 * * *".into(),
                tz: "UTC".into(),
                template: Some(template),
                overlap: pb::Overlap::Skip as i32,
            })
            .await;
        assert_eq!(r.is_ok(), ok, "{r:?}");
    }
    let r = admin
        .remove_recurring(pb::RemoveRecurringRequest {
            name: "nightly".into(),
        })
        .await
        .unwrap();
    assert!(r.into_inner().removed);
    server.stop().await;
}

// Keys of the form `<prefix>@<UTC time>` belong to recurring occurrences; a client taking one in
// advance could stand in for a scheduled run.
#[tokio::test]
async fn occurrence_shaped_keys_are_reserved() {
    let server = TestServer::start("").await;
    let mut c = server.tasks(CLIENT).await;
    let mut squat = spec(b"x");
    squat.key = "nightly@2026-10-23T03:00:00Z".into();
    let e = c.submit(submit(squat, false)).await.unwrap_err();
    assert_eq!(e.code(), tonic::Code::InvalidArgument);
    let mut ok = spec(b"x");
    ok.key = "user@example.com".into();
    assert!(c.submit(submit(ok, false)).await.is_ok());
    server.stop().await;
}

#[tokio::test]
async fn a_pool_min_bounds_desired_capacity_without_inventing_workers() {
    let s = TestServer::start("[[pools]]\nname = \"default\"\nmin = 2\nmax = 8\n").await;
    let mut desired = None;
    for _ in 0..200 {
        let snap = s.snapshot().await;
        desired = snap["pools"][0]["desired"].as_u64();
        if desired == Some(2) {
            break;
        }
        tokio::time::sleep(Duration::from_millis(25)).await;
    }
    assert_eq!(desired, Some(2), "an idle pool asks for its minimum");
    assert_eq!(s.capacity().await, 0, "no worker is connected");
    s.stop().await;
}