laterite-core 0.9.23

Laterite CMF application kernel: config, errors, database, migrations, modules
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! The scheduler: entries that run on a cron expression, evaluated every
//! minute in their own timezone, each on one process at a time.
//!
//! An entry runs a closure or pushes a job. A module registers its entries;
//! the ticker in the application process (or `schedule run` alone) checks
//! them each minute, takes a lock per entry and minute so one deployment
//! runs an entry once however many processes it has, skips an entry still
//! running from last time, and records each run for the screen.
//!
//! ```ignore
//! use laterite_core::schedule::{ScheduleEntry, ScheduleRegistry};
//!
//! fn register(&self, registry: &mut Registry) {
//!     registry.add_schedule(
//!         ScheduleEntry::new("acme.blog::digest", "0 7 * * *")
//!             .timezone("Europe/Berlin")
//!             .job(SendDigest { user_id: 0 }),
//!     );
//!     registry.add_schedule(
//!         ScheduleEntry::new("acme.blog::sweep", "*/5 * * * *")
//!             .run(|cx| async move { sweep(cx.db()).await }),
//!     );
//! }
//! ```

use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use crate::cache::Cache;
use crate::config::ScheduleConfig;
use crate::error::{CoreError, CoreResult};
use crate::events::Events;
use crate::parameters::Parameters;
use crate::queue::{Job, Queue};
use crate::registry::Registry;
use crate::{Db, Module, ModuleId};
use chrono::{DateTime, Timelike, Utc};
use chrono_tz::Tz;
use croner::Cron;

pub mod migrations;
mod store;

pub use store::{EntryState, RunStatus};

/// The module that owns the `schedule_runs` table, listed among an
/// application's built-ins.
pub struct ScheduleModule;

impl Module for ScheduleModule {
    fn id(&self) -> ModuleId {
        ModuleId::new(migrations::MODULE_ID)
    }
    fn migrations(&self) -> crate::MigrationSet {
        migrations::migrations()
    }
}

/// What a running entry sees.
#[derive(Clone)]
pub struct ScheduleCx {
    db: Db,
    events: Events,
    cache: Cache,
    parameters: Parameters,
    queue: Queue,
}

impl ScheduleCx {
    pub fn db(&self) -> &Db {
        &self.db
    }
    pub fn events(&self) -> &Events {
        &self.events
    }
    pub fn cache(&self) -> &Cache {
        &self.cache
    }
    pub fn parameters(&self) -> &Parameters {
        &self.parameters
    }
    pub fn queue(&self) -> &Queue {
        &self.queue
    }
}

/// What happens when an entry is due while its last run is still going.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Overlap {
    /// Let this minute's run go: the entry runs once at a time.
    #[default]
    Skip,
    /// Run it beside the one still going.
    Allow,
}

type RunFn =
    dyn Fn(ScheduleCx) -> Pin<Box<dyn Future<Output = CoreResult<()>> + Send>> + Send + Sync;

#[derive(Clone)]
enum Action {
    Run(Arc<RunFn>),
    Job {
        name: &'static str,
        payload: serde_json::Value,
        queue: String,
    },
    /// Declared with neither `.run` nor `.job`; refused at boot.
    None,
}

/// One scheduled entry: a name, a cron expression, a timezone and what to do.
#[derive(Clone)]
pub struct ScheduleEntry {
    name: String,
    expression: String,
    timezone: String,
    description: String,
    action: Action,
    overlap: Overlap,
    one_server: bool,
}

impl std::fmt::Debug for ScheduleEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ScheduleEntry")
            .field("name", &self.name)
            .field("expression", &self.expression)
            .field("timezone", &self.timezone)
            .finish_non_exhaustive()
    }
}

impl ScheduleEntry {
    /// An entry named `name` (`acme.blog::digest`) on `expression`: five
    /// fields (`0 7 * * *`), six with seconds, or a preset (`@hourly`,
    /// `@daily`, `@weekly`, `@monthly`, `@yearly`). A bad expression is
    /// refused when the application boots.
    pub fn new(name: impl Into<String>, expression: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            expression: expression.into(),
            timezone: "UTC".to_string(),
            description: String::new(),
            action: Action::None,
            overlap: Overlap::Skip,
            one_server: true,
        }
    }

    /// Every `n` minutes.
    pub fn every_minutes(name: impl Into<String>, n: u32) -> Self {
        Self::new(name, format!("*/{} * * * *", n.max(1)))
    }

    /// Every hour, on the hour.
    pub fn hourly(name: impl Into<String>) -> Self {
        Self::new(name, "0 * * * *")
    }

    /// Every day at `HH:MM` in the entry's timezone.
    pub fn daily_at(name: impl Into<String>, time: &str) -> Self {
        let (hour, minute) = time.split_once(':').unwrap_or((time, "0"));
        let hour = hour.trim().parse::<u32>().unwrap_or(0);
        let minute = minute.trim().parse::<u32>().unwrap_or(0);
        Self::new(name, format!("{minute} {hour} * * *"))
    }

    /// The timezone the expression is read in; `UTC` unless set.
    pub fn timezone(mut self, tz: impl Into<String>) -> Self {
        self.timezone = tz.into();
        self
    }

    /// A line for the screen.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = description.into();
        self
    }

    /// Runs `f` when due.
    pub fn run<F, Fut>(mut self, f: F) -> Self
    where
        F: Fn(ScheduleCx) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = CoreResult<()>> + Send + 'static,
    {
        self.action = Action::Run(Arc::new(move |cx| Box::pin(f(cx))));
        self
    }

    /// Pushes `job` when due.
    pub fn job<T: Job>(mut self, job: T) -> Self {
        let payload = serde_json::to_value(&job).unwrap_or(serde_json::Value::Null);
        self.action = Action::Job {
            name: T::NAME,
            payload,
            queue: job.queue(),
        };
        self
    }

    pub fn overlap(mut self, overlap: Overlap) -> Self {
        self.overlap = overlap;
        self
    }

    /// Whether one process of the deployment runs it per minute (`true`
    /// unless set) or every process does.
    pub fn one_server(mut self, one: bool) -> Self {
        self.one_server = one;
        self
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn expression(&self) -> &str {
        &self.expression
    }
}

/// `registry.add_schedule(entry)`: the typed way to contribute an entry.
pub trait ScheduleRegistry {
    fn add_schedule(&mut self, entry: ScheduleEntry);
}

impl ScheduleRegistry for Registry {
    fn add_schedule(&mut self, entry: ScheduleEntry) {
        self.add(entry);
    }
}

/// An entry parsed and ready.
struct Prepared {
    entry: ScheduleEntry,
    cron: Cron,
    tz: Tz,
}

impl Prepared {
    fn new(entry: ScheduleEntry) -> CoreResult<Self> {
        let cron: Cron = entry.expression.parse().map_err(|e| {
            CoreError::Config(format!(
                "schedule entry `{}`: expression `{}` does not parse: {e}",
                entry.name, entry.expression
            ))
        })?;
        let tz: Tz = entry.timezone.parse().map_err(|_| {
            CoreError::Config(format!(
                "schedule entry `{}`: timezone `{}` is unknown",
                entry.name, entry.timezone
            ))
        })?;
        if matches!(entry.action, Action::None) {
            return Err(CoreError::Config(format!(
                "schedule entry `{}` has nothing to do: give it `.run(..)` or `.job(..)`",
                entry.name
            )));
        }
        Ok(Self { entry, cron, tz })
    }

    fn is_due(&self, minute: DateTime<Utc>) -> bool {
        let local = minute.with_timezone(&self.tz);
        self.cron.is_time_matching(&local).unwrap_or(false)
    }

    fn next_after(&self, now: DateTime<Utc>) -> Option<DateTime<Utc>> {
        let local = now.with_timezone(&self.tz);
        self.cron
            .find_next_occurrence(&local, false)
            .ok()
            .map(|t| t.with_timezone(&Utc))
    }
}

struct Inner {
    entries: Vec<Prepared>,
    by_name: HashMap<String, usize>,
    db: Db,
    cx: ScheduleCx,
}

/// The schedule handle. Cheap to clone: every clone is the same schedule.
#[derive(Clone)]
pub struct Schedule {
    inner: Arc<Inner>,
}

impl std::fmt::Debug for Schedule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Schedule")
            .field("entries", &self.inner.entries.len())
            .finish_non_exhaustive()
    }
}

/// The services an entry runs against.
#[derive(Clone)]
pub struct Services {
    pub db: Db,
    pub events: Events,
    pub cache: Cache,
    pub parameters: Parameters,
    pub queue: Queue,
}

/// How long the one-server lock on a minute is kept.
const MINUTE_LOCK: Duration = Duration::from_secs(3600);
/// How long an entry's overlap lock may be held by a run that never came back.
const OVERLAP_LOCK: Duration = Duration::from_secs(24 * 3600);

/// How one tick went: what ran, what was skipped.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Tick {
    pub ran: Vec<String>,
    pub skipped: Vec<String>,
}

impl Schedule {
    /// A schedule over `entries`; a bad expression, timezone or duplicate
    /// name is an error.
    pub fn new(
        entries: impl IntoIterator<Item = ScheduleEntry>,
        services: Services,
    ) -> CoreResult<Self> {
        let mut prepared = Vec::new();
        let mut by_name = HashMap::new();
        for entry in entries {
            if by_name.contains_key(&entry.name) {
                return Err(CoreError::Config(format!(
                    "schedule entry `{}` is registered twice",
                    entry.name
                )));
            }
            by_name.insert(entry.name.clone(), prepared.len());
            prepared.push(Prepared::new(entry)?);
        }
        Ok(Self {
            inner: Arc::new(Inner {
                entries: prepared,
                by_name,
                db: services.db.clone(),
                cx: ScheduleCx {
                    db: services.db,
                    events: services.events,
                    cache: services.cache,
                    parameters: services.parameters,
                    queue: services.queue,
                },
            }),
        })
    }

    /// The entries' names, in registration order.
    pub fn names(&self) -> Vec<&str> {
        self.inner
            .entries
            .iter()
            .map(|p| p.entry.name.as_str())
            .collect()
    }

    pub fn len(&self) -> usize {
        self.inner.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.inner.entries.is_empty()
    }

    /// Every entry with its recorded state, in registration order.
    pub async fn list(&self) -> CoreResult<Vec<EntryState>> {
        let recorded = store::all(&self.inner.db).await?;
        let now = Utc::now();
        Ok(self
            .inner
            .entries
            .iter()
            .map(|p| {
                let mut state = recorded
                    .get(&p.entry.name)
                    .cloned()
                    .unwrap_or_else(|| EntryState::fresh(&p.entry.name));
                state.expression = p.entry.expression.clone();
                state.timezone = p.entry.timezone.clone();
                state.description = p.entry.description.clone();
                state.next_run_at = p.next_after(now).map(crate::queue::time_text);
                state
            })
            .collect())
    }

    /// Runs every entry due at `now`'s minute. Each runs on its own task;
    /// the tick returns once they are started, naming what ran and what was
    /// skipped (held by another process, or still running).
    pub async fn tick(&self, now: DateTime<Utc>) -> CoreResult<Tick> {
        let minute = now
            .with_second(0)
            .and_then(|t| t.with_nanosecond(0))
            .unwrap_or(now);
        let mut tick = Tick::default();
        for (at, prepared) in self.inner.entries.iter().enumerate() {
            if !prepared.is_due(minute) {
                continue;
            }
            match self.start(at, minute).await? {
                true => tick.ran.push(prepared.entry.name.clone()),
                false => tick.skipped.push(prepared.entry.name.clone()),
            }
        }
        Ok(tick)
    }

    /// Runs one entry now, due or not, under the same locks as a tick, and
    /// waits for it. `false` when it was skipped (still running elsewhere).
    pub async fn run_now(&self, name: &str) -> CoreResult<bool> {
        let Some(&at) = self.inner.by_name.get(name) else {
            return Err(CoreError::NotFound(format!("schedule entry `{name}`")));
        };
        match self.spawn(at, Utc::now()).await? {
            Some(handle) => {
                let _ = handle.await;
                Ok(true)
            }
            None => Ok(false),
        }
    }

    async fn start(&self, at: usize, minute: DateTime<Utc>) -> CoreResult<bool> {
        Ok(self.spawn(at, minute).await?.is_some())
    }

    /// Takes the locks and starts the entry on its own task; `None` when a
    /// lock was held elsewhere.
    async fn spawn(
        &self,
        at: usize,
        minute: DateTime<Utc>,
    ) -> CoreResult<Option<tokio::task::JoinHandle<()>>> {
        let prepared = &self.inner.entries[at];
        let name = prepared.entry.name.clone();
        let cache = &self.inner.cx.cache;
        if prepared.entry.one_server {
            let key = format!("laterite.schedule::{name}@{}", minute.format("%Y%m%d%H%M"));
            if cache.lock(&key, MINUTE_LOCK).await?.is_none() {
                tracing::debug!(entry = %name, "scheduled entry taken by another process");
                return Ok(None);
            }
        }
        let overlap = if prepared.entry.overlap == Overlap::Skip {
            match cache
                .lock(&format!("laterite.schedule::{name}"), OVERLAP_LOCK)
                .await?
            {
                Some(lock) => Some(lock),
                None => {
                    tracing::warn!(entry = %name, "scheduled entry still running; skipped");
                    store::skipped(&self.inner.db, &name).await?;
                    return Ok(None);
                }
            }
        } else {
            None
        };
        store::started(&self.inner.db, &name).await?;
        let schedule = self.clone();
        Ok(Some(tokio::spawn(async move {
            let outcome = schedule.execute(at).await;
            let db = &schedule.inner.db;
            match &outcome {
                Ok(()) => {
                    tracing::info!(entry = %name, "scheduled entry ran");
                    if let Err(e) = store::finished(db, &name, None).await {
                        tracing::error!(entry = %name, error = %e, "run could not be recorded");
                    }
                }
                Err(e) => {
                    tracing::error!(entry = %name, error = %e, "scheduled entry failed");
                    if let Err(e2) = store::finished(db, &name, Some(&e.to_string())).await {
                        tracing::error!(entry = %name, error = %e2, "failure could not be recorded");
                    }
                }
            }
            if let Some(lock) = overlap {
                let _ = lock.release().await;
            }
        })))
    }

    async fn execute(&self, at: usize) -> CoreResult<()> {
        let prepared = &self.inner.entries[at];
        match &prepared.entry.action {
            Action::Run(f) => f(self.inner.cx.clone()).await,
            Action::Job {
                name,
                payload,
                queue,
            } => self
                .inner
                .cx
                .queue
                .push_named(name, payload, queue)
                .await
                .map(|_| ()),
            Action::None => Ok(()),
        }
    }

    /// Ticks at every minute until `stop` says so.
    pub async fn run_ticker(&self, mut stop: tokio::sync::watch::Receiver<bool>) {
        loop {
            let now = Utc::now();
            let next_minute = (now + chrono::Duration::seconds(60))
                .with_second(0)
                .and_then(|t| t.with_nanosecond(0))
                .unwrap_or(now);
            let wait = (next_minute - now)
                .to_std()
                .unwrap_or(Duration::from_secs(1));
            tokio::select! {
                _ = tokio::time::sleep(wait) => {}
                _ = stop.changed() => return,
            }
            if *stop.borrow() {
                return;
            }
            match self.tick(Utc::now()).await {
                Ok(tick) if !tick.ran.is_empty() || !tick.skipped.is_empty() => {
                    tracing::debug!(ran = ?tick.ran, skipped = ?tick.skipped, "schedule tick");
                }
                Ok(_) => {}
                Err(e) => tracing::error!(error = %e, "schedule tick failed"),
            }
        }
    }

    /// Starts the ticker on its own task.
    pub fn spawn_ticker(
        &self,
        stop: tokio::sync::watch::Receiver<bool>,
    ) -> tokio::task::JoinHandle<()> {
        let schedule = self.clone();
        tokio::spawn(async move { schedule.run_ticker(stop).await })
    }
}

/// Every entry the table records, sorted by name: what a tool outside the
/// application can see of the schedule.
pub async fn recorded(db: &Db) -> CoreResult<Vec<EntryState>> {
    let mut rows: Vec<EntryState> = store::all(db).await?.into_values().collect();
    rows.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(rows)
}

/// `ticker`, `external` or `off`, as `[schedule] mode` names them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    Ticker,
    External,
    Off,
}

impl Mode {
    pub fn parse(s: &str) -> CoreResult<Self> {
        match s {
            "ticker" => Ok(Self::Ticker),
            "external" => Ok(Self::External),
            "off" => Ok(Self::Off),
            other => Err(CoreError::Config(format!(
                "schedule.mode `{other}` is not one of ticker, external, off"
            ))),
        }
    }
}

/// The mode `[schedule]` configures.
pub fn mode_from_config(config: &ScheduleConfig) -> CoreResult<Mode> {
    Mode::parse(&config.mode)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sugar_builds_the_expected_expressions() {
        assert_eq!(
            ScheduleEntry::every_minutes("a", 5).expression(),
            "*/5 * * * *"
        );
        assert_eq!(ScheduleEntry::hourly("a").expression(), "0 * * * *");
        assert_eq!(
            ScheduleEntry::daily_at("a", "07:30").expression(),
            "30 7 * * *"
        );
        assert_eq!(ScheduleEntry::daily_at("a", "7").expression(), "0 7 * * *");
    }

    #[test]
    fn an_entry_is_due_in_its_own_timezone() {
        let entry = ScheduleEntry::new("a", "0 7 * * *")
            .timezone("Asia/Colombo")
            .run(|_| async { Ok(()) });
        let prepared = Prepared::new(entry).unwrap();
        // 07:00 in Colombo is 01:30 UTC.
        let due = "2026-10-24T01:30:00Z".parse::<DateTime<Utc>>().unwrap();
        assert!(prepared.is_due(due));
        let not = "2026-10-24T07:00:00Z".parse::<DateTime<Utc>>().unwrap();
        assert!(!prepared.is_due(not));
        let next = prepared.next_after(not).unwrap();
        assert_eq!(next.to_rfc3339(), "2026-10-25T01:30:00+00:00");
    }

    #[test]
    fn a_bad_expression_timezone_or_empty_entry_is_refused() {
        assert!(Prepared::new(ScheduleEntry::new("a", "nope").run(|_| async { Ok(()) })).is_err());
        assert!(Prepared::new(
            ScheduleEntry::hourly("a")
                .timezone("Mars/Olympus")
                .run(|_| async { Ok(()) })
        )
        .is_err());
        assert!(Prepared::new(ScheduleEntry::hourly("a")).is_err());
        assert_eq!(Mode::parse("off").unwrap(), Mode::Off);
    }
}