目录

MoonBit Cron

MoonBit CI

MoonBit Cron is a zero-dependency, pure MoonBit library for parsing standard five-field Cron expressions, matching times, and calculating the next trigger. It is suitable for schedulers, automation tools, backend services, and CLIs.

Features

  • Parses minute, hour, day-of-month, month, and day-of-week fields.
  • Supports *, values, ranges (a-b), lists (a,b), and steps (*/n).
  • Supports uppercase month aliases (JAN through DEC) and weekday aliases (SUN through SAT).
  • Rejects malformed input, out-of-range values, non-positive steps, and oversized numeric fields before integer overflow.
  • Uses standard Cron day-of-month/day-of-week OR semantics.
  • Finds the next matching minute across month and leap-year boundaries.
  • Exposes CronExpr::to_string() for canonical serialization and config round-tripping.
  • Supports wasm, wasm-gc, JavaScript, and native targets.
  • Provides a deterministic in-memory scheduler with task lifecycle and retry policies.
  • Includes task configuration parsing, dependency validation, conflict detection, audit history, health reports, preflight checks, and snapshots.
  • Offers bounded windows, business-hours filtering, rate limiting, concurrency policies, and deterministic benchmark/report output.

Install

Install MoonBit with the official installer, then add the package from Mooncakes:

moon add lijunjie860/moonbit_cron

To develop locally:

git clone https://github.com/lijunjie860/MoonBit-Cron.git
cd MoonBit-Cron
moon check --target all --warn-list +73 --deny-warn
moon test --target all --warn-list +73 --deny-warn
moon run cmd/main

API example

import {
  "lijunjie860/moonbit_cron" @cron,
}

fn next_workday_trigger() -> Result[@cron.Time, String] {
  match @cron.parse("*/15 9-17 * JAN-MAR MON-FRI") {
    Err(message) => Err(message)
    Ok(expr) => expr.next(@cron.Time::{
      year: 2026,
      month: 1,
      day: 5,
      hour: 9,
      minute: 7,
      weekday: 1,
    })
  }
}

Runnable CLI demonstration

moon run cmd/main

Expected output:

Expression: */15 9-17 * * 1-5
Canonical: */15 9-17 * * 1-5
Matches now: false
Next trigger: 2026-10-15 9:15 (weekday 4)

CronExpr::to_string() emits a normalized numeric form. Named aliases such as JAN and MON are parsed correctly and serialize to their numeric values, which makes persisted schedules deterministic.

Validation

The GitHub Actions workflow installs the latest official MoonBit toolchain, formats its isolated checkout, then runs warning-free checks, builds, generated-interface generation, and tests on Ubuntu, macOS, and Windows.

Run the same checks locally:

moon fmt --check
moon check --target all --warn-list +73 --deny-warn
moon build --target all --warn-list +73 --deny-warn
moon test --target all --warn-list +73 --deny-warn
moon info

See CHANGELOG.md for release history.

Production scheduler workflow

Task definitions use the stable format id|cron|enabled|max_attempts:

backup|0 2 * * *|true|3
report|0 9 * * 1-5|true|1

Applications can load the document with parse_task_document, validate it with preflight, build bounded ExecutionPlan values, and record outcomes in an AuditLog. SchedulerReport and SchedulerSnapshot provide stable text summaries for health endpoints and operational runbooks.

License

Copyright (c) 2026 lijunjie860. Licensed under the MIT License.

关于

本项目是一个纯 MoonBit 实现的 Cron 表达式解析与时间匹配库。它致力于为 MoonBit 生态提供标准的定时任务表达式解析能力,帮助开发者在后端服务、云原生组件及各类自动化脚本中轻松实现复杂的时间调度逻辑。

179.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9 京公网安备 11010802047560号