Time and IDs

Cron lines

The next times a cron line runs, and whether it is valid: show a schedule, or run your own.

Version 1.0.0 · 2 functions · 2 crates

#schedules#jobs#time

Functions

What use cron.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use cron_next, cron_valid from cron (Quillo 0.6).

cron_next(line: str, count: int, after: int) → listor an error

The next count times line runs after after (milliseconds since 1970; 0 for now), in UTC, as milliseconds.

cron_valid(line: str) → bool

Whether line is a cron line this reads (5 fields, or 6 and 7 with seconds and years).

Get it

$ quillo install cron               # cron.qwer into this folder$ quillo install cron --example     # and its example, cron.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/cron.qwer$ curl -O https://quillolang.org/plugins/files/cron.qwe

Then, in your program, all of it or only what you call:

main.qwe
use cron.qweruse cron_next, cron_valid from cron      // Quillo 0.6: only these

It uses these crates, which cargo fetches the first time the plugin is built:

[dependencies]
cron = "0.12"chrono = "0.4"

Example

cron.qwe
// A page that shows when each job runs next.////   quillo run cron.qwe      then open http://localhost:8080/next?line=0+9+*+*+1-5use "cron.qwer"route GET /next ? { line: str } = cron_next(line, 5, 0)? | map((ms) => date.format(date.from_ms(ms), "yyyy-MM-dd HH:mm"))route GET /fixed = { runs: cron_next("30 2 * * *", 2, 1767225600000)?, valid: cron_valid("*/15 * * * *"), invalid: cron_valid("61 * * * *") }
$ quillo run cron.qwe$ curl 'localhost:8080/next?line=0+9+*+*+1-5'$ curl 'localhost:8080/fixed'

Source

cron.qwer
//! Cron lines read: the next times one runs, whether it is valid, for showing a schedule or running your own.//! version: 1.0.0//!//! [dependencies]//! cron = "0.12"//! chrono = "0.4"use chrono::{TimeZone, Utc};use std::str::FromStr;/// A 5-field line (`minute hour day month weekday`) given a seconds field, as the crate wants; 6 and 7 fields pass as they are.fn schedule(line: &str) -> Result<cron::Schedule, String> {    let fields = line.split_whitespace().count();    let full = if fields == 5 { format!("0 {}", line.trim()) } else { line.trim().to_string() };    cron::Schedule::from_str(&full).map_err(|e| format!("`{}`: {}", line, e))}/// The next `count` times `line` runs after `after` (milliseconds since 1970; 0 for now), in UTC, as milliseconds.pub fn cron_next(line: &str, count: i64, after: i64) -> Result<Vec<i64>, String> {    let s = schedule(line)?;    let from = if after > 0 { Utc.timestamp_millis_opt(after).single().ok_or("a time out of range")? } else { Utc::now() };    Ok(s.after(&from).take(count.clamp(0, 1000) as usize).map(|t| t.timestamp_millis()).collect())}/// Whether `line` is a cron line this reads (5 fields, or 6 and 7 with seconds and years).pub fn cron_valid(line: &str) -> bool {    schedule(line).is_ok()}
Thread
77Useful? Like it, or add to its thread below.

Thread 0 posts

On Cron lines: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.