a3s-cron
Cron scheduling library for A3S with natural language support.
Features
- Standard Cron Syntax: 5-field cron expressions (minute hour day month weekday)
- Natural Language: Parse schedules from English and Chinese
- Persistence: JSON file-based storage with pluggable backends
- CRUD Operations: Create, pause, resume, update, and remove jobs
- Execution History: Track job runs with output and status
- Agent-Mode Jobs: Schedule AI agent prompts alongside shell commands via
AgentExecutortrait - 85 Unit Tests: Comprehensive test coverage
Installation
[]
= "0.1"
Library Usage
Shell Jobs
use ;
async
Agent-Mode Jobs
use ;
async
CLI Usage (via a3s-tools)
# Parse natural language to cron expression
TOOL_ARGS='{"action":"parse","input":"every day at 2am"}'
# Output: Cron expression: 0 2 * * *
TOOL_ARGS='{"action":"parse","input":"每周一上午9点"}'
# Output: Cron expression: 0 9 * * 1
# Create a job with natural language schedule
TOOL_ARGS='{"action":"add","name":"backup","schedule":"every day at 3am","command":"./backup.sh"}'
# List all jobs
TOOL_ARGS='{"action":"list"}'
# Get job details
TOOL_ARGS='{"action":"get","id":"<job-id>"}'
# Manually run a job
TOOL_ARGS='{"action":"run","id":"<job-id>"}'
# View execution history
TOOL_ARGS='{"action":"history","id":"<job-id>"}'
# Pause a job
TOOL_ARGS='{"action":"pause","id":"<job-id>"}'
# Resume a job
TOOL_ARGS='{"action":"resume","id":"<job-id>"}'
# Update job schedule
TOOL_ARGS='{"action":"update","id":"<job-id>","schedule":"every monday at 9am"}'
# Remove a job
TOOL_ARGS='{"action":"remove","id":"<job-id>"}'
Natural Language Support
English
| Expression | Cron | Description |
|---|---|---|
every minute |
* * * * * |
Every minute |
every 5 minutes |
*/5 * * * * |
Every 5 minutes |
every hour |
0 * * * * |
Every hour |
every 2 hours |
0 */2 * * * |
Every 2 hours |
daily at 2am |
0 2 * * * |
Daily at 2:00 AM |
every day at 14:30 |
30 14 * * * |
Daily at 2:30 PM |
weekly on monday at 9am |
0 9 * * 1 |
Every Monday at 9:00 AM |
monthly on the 15th |
0 0 15 * * |
15th of every month |
every weekday at 8am |
0 8 * * 1-5 |
Mon-Fri at 8:00 AM |
every weekend at 10am |
0 10 * * 0,6 |
Sat-Sun at 10:00 AM |
Chinese (中文)
| 表达式 | Cron | 描述 |
|---|---|---|
每分钟 |
* * * * * |
每分钟执行 |
每5分钟 |
*/5 * * * * |
每5分钟执行 |
每小时 |
0 * * * * |
每小时执行 |
每2小时 |
0 */2 * * * |
每2小时执行 |
每天凌晨2点 |
0 2 * * * |
每天凌晨2点 |
每天下午3点30分 |
30 15 * * * |
每天下午3:30 |
每周一上午9点 |
0 9 * * 1 |
每周一上午9点 |
每月15号 |
0 0 15 * * |
每月15号 |
工作日上午9点 |
0 9 * * 1-5 |
工作日上午9点 |
周末上午10点 |
0 10 * * 0,6 |
周末上午10点 |
Cron Expression Format
Standard 5-field cron format:
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sun=0)
│ │ │ │ │
* * * * *
Special characters:
*- any value,- value list (e.g.,1,3,5)-- range (e.g.,1-5)/- step (e.g.,*/5)
Architecture
a3s-cron/
├── src/
│ ├── lib.rs # Public API
│ ├── types.rs # CronJob, JobType, AgentJobConfig, AgentExecutor
│ ├── parser.rs # Cron expression parser
│ ├── natural.rs # Natural language parser
│ ├── store.rs # CronStore trait, FileCronStore, MemoryCronStore
│ ├── scheduler.rs # CronManager with CRUD + agent-mode execution
│ └── telemetry.rs # OpenTelemetry metrics and spans
└── Cargo.toml
Roadmap
Phase 1: Core ✅
- Standard 5-field cron expression parsing
- Natural language schedule parsing (English + Chinese)
- JSON file-based persistence with pluggable backends
- CRUD operations (create, pause, resume, update, remove)
- Execution history tracking with output and status
- CLI integration via a3s-tools
- Agent-mode jobs via
AgentExecutortrait (Shell + Agent job types) - 85 comprehensive unit tests
Phase 2: Distributed Scheduling 📋
- Cluster-aware Scheduling: Multi-node job distribution
- Leader election for scheduler coordination (etcd / NATS)
- Job assignment with node affinity and load balancing
- Failover: automatic job reassignment on node failure
- Exactly-once execution guarantee (distributed lock)
- Advanced Scheduling Strategies:
- Job dependency chains (Job B runs after Job A completes)
- Conditional execution (run only if previous job succeeded/failed)
- Job groups with shared concurrency limits
- Backfill: catch up missed executions after downtime
- Observability:
- OpenTelemetry spans for job execution lifecycle
- Span:
a3s.cron.executewith attributes: job_id, job_name, schedule, duration_ms - Metrics:
a3s_cron_job_duration_seconds{job}histogram - Metrics:
a3s_cron_job_failures_total{job}counter - Metrics:
a3s_cron_missed_executions_total{job}counter
- Storage Backends:
- Redis backend for distributed state
- PostgreSQL backend for durable persistence
- Migration tool between storage backends
License
MIT