定时任务配置_iii-cron
以下为本文档的中文说明该技能用于在iii框架中配置和管理定时任务Cron Job。它允许开发者将注册的函数按时间表自动触发执行支持标准的Cron表达式和灵活的调度策略。适用于需要定期执行后台任务、数据同步、报告生成等自动化运维场景简化定时任务的管理。该技能与iii框架的任务系统深度集成开发者只需注册函数并绑定Cron触发器即可实现定时执行无需单独管理调度基础设施。iii框架的Cron触发器允许开发者将普通函数转化为定时执行的任务。该技能支持标准的Cron表达式分 时 日 月 周和扩展的调度策略如every 5m、daily等简化表达。技能与iii框架的任务系统深度集成开发者只需在函数注册时绑定Cron触发器即可实现定时执行无需管理独立的调度服务器或Cron进程。该技能适用于需要定期执行数据同步、缓存刷新、报告生成、健康检查等后台任务的场景大幅简化了定时任务的管理复杂度。iii框架的Cron定时任务机制为开发者提供了简洁高效的后台任务调度能力。开发者只需将普通函数注册并与Cron触发器绑定即可实现按计划自动执行。技能支持标准的Unix Cron表达式格式提供分钟、小时、日、月、星期五个维度的灵活调度配置同时还支持更人性化的简化表达式如every 10m、hourly、daily等。该技能与iii框架的任务执行和监控系统深度集成每次执行都会记录任务状态、执行耗时和输出结果。iii-cronTheiii-cronworker schedules a registered function to run on a recurring cron expression. It exposes no callable functions — its entire surface is one trigger type,cron, bound viaiii.registerTrigger({ type: cron, function_id, config }). On every firing the engine builds an event payload, optionally evaluates a condition function, acquires a distributed lock through the configured adapter, and invokes the target function. Each firing reportsscheduled_timevs.actual_timeso drift and reentrancy are observable from inside the handler.The schedule grammar is the seven-field cron dialect —second minute hour day-of-month month day-of-week [year]— where the year is optional and defaults to*. Both six- and seven-field forms work; the leading field is always seconds, so0 */5 * * * *fires every 5 minutes at second 0, not every 5 seconds.Two adapters govern once-only execution:kv(default) takes a process-local lock and is single-instance only — on a multi-instance fleet every engine fires the same job (tunableslock_ttl_ms,lock_index);redistakes a distributed lock and is required for once-only firing across a fleet (tunableredis_url).When to UseA function should run periodically without standing up a separate scheduler process or a system crontab entry.You need once-only firing across a fleet — nightly cleanup, hourly reports, batch maintenance — paired with theredisadapter.A scheduled job should be conditionally skipped (holiday calendar, feature flag, weekend pause) viacondition_function_idwithout threading the check through the handler.BoundariesNo callable functions — never invoked through acron::*id; everything flows throughiii.registerTrigger.The defaultkvadapter only locks process-local; never rely on it for once-only jobs in a multi-instance deployment — useredis.Reading the leading field as minutes (the five-field crontab convention) schedules jobs 60x too often; always count fields and remember position 0 is seconds.For>Reactive triggersBind acrontrigger when a handler should run on a recurring schedule. The handler runs server-side on a tokio task spawned by the engine; with theredisadapter it fires once across the fleet per scheduled run.Reach for it when:You need recurring execution (cleanup, reports, maintenance) without an external scheduler.You want condition-gated firing: setcondition_function_idand the engine evaluates it before each run, skipping the handler (and releasing the lock) on a falsy or erroring result.How to bindRegister a handler:iii.registerFunction(jobs::cleanup-old-data, handler).Register the trigger:iii.registerTrigger({type:cron,function_id:jobs::cleanup-old-data,config:{expression:0 0 2 * * * *,// required. sec min hour dom month dow [year]; daily at 02:00:00.// condition_function_id is also supported.},})expressionis required and must parse, or registration fails synchronously. Bind onefunction_idto several triggers with distinct ids to drive multiple schedules into one handler — the trigger id arrives asjob_idin the event. The handler’s return value is ignored.For the firing event payload (trigger,job_id,scheduled_time,actual_time), calliii get function infoon the trigger type or handler function id.