[{"data":1,"prerenderedAt":549},["ShallowReactive",2],{"$fW84hHxQtSUUaogKaZQ6Mp3sJzQ8UbTGXPr4mEbAZImc":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":533,"editUrl":543,"prev":544,"next":546},"techniques\u002Fscheduling","Scheduling","Run recurring jobs on cron schedules with CronJob and keep schedules configurable with CronTicker.","---\ntitle: Scheduling\ndescription: Run recurring jobs on cron schedules with CronJob and keep schedules configurable with CronTicker.\n---\n\n# Scheduling\n\n`ferrite-scheduler` adds cron-based scheduling in the NestJS `@Cron`\u002F`CronJob` style: declare schedules in your provider with attribute macros, and the module starts a `CronTicker` that ticks over a `cron` schedule.\n\n## Enable it\n\n```cargo\nfr add scheduler\n```\n\n```rust\n#[module(imports = [CronModuleImpl])]\npub struct AppModule;\n```\n\n## Scheduled functions\n\n```rust\nuse ferrite_scheduler::cron;\nuse ferrite_framework::injectable;\n\n#[injectable]\npub struct DailyDigestService; \u002F\u002F or with #[inject] constructor + deps\n\nimpl DailyDigestService {\n    #[cron(\"0 8 * * *\", name = \"daily_digest\")]\n    pub async fn run(&self) {\n        build_and_send_digest().await;\n    }\n}\n```\n\n### `#[cron]` options\n\n| Option | Default | Purpose |\n| --- | --- | --- |\n| (positional) | required | A `cron` crate schedule string |\n| `name` | fn name | Identifier for the job |\n| `schedule` | – | Same as positional; for codegen-referenced schedules |\n| `config_path` | – | Read the schedule from config at runtime (see below) |\n| `run_on_start` | `false` | Fire once when the ticker starts |\n\n## CronTicker\n\n`CronTicker` is an injectable that owns the ticking loop. **Construct it in your module with your provider** so dependencies resolve under DI:\n\n```rust\n#[injectable]\npub struct CronTicker {\n    \u002F\u002F holds a loop that reads the job registry\n}\n```\n\n```rust\n#[module(imports = [CronModuleImpl])]\npub struct AppModule;\n```\n\n`CronModuleImpl::on_application_bootstrap` starts the ticker, so schedules begin firing when the app is ready.\n\n## Schedules from config\n\nKeeping schedules in env\u002Fconfig is supported two ways — via `config_path` on the attribute, or by choosing a schedule expression directly when the module reads `CRON_TICKER_*` env vars:\n\n| Env | Default | Purpose |\n| --- | --- | --- |\n| `CLOCK_DEFAULT_SCHEDULE` | `*\u002F15 * * * * * *` | Fallback schedule |\n| `CLOCK_TIMEZONE` | `utc` | Ticker timezone |\n\nUsing `config_path = \"cron.daily_digest\"` reads the schedule from your app config (see [Configuration](\u002Fdocs\u002Foverview\u002Fconfiguration)) instead of hardcoding.\n\n## Error handling\n\nA job returning an error logs it and the ticker continues; `CronJob` and `cron::Schedule` failures surface via `CronError`.\n\n## Next steps\n\n- [Background jobs](\u002Fdocs\u002Ftechniques\u002Fbackground-jobs).\n- [Lifecycle events](\u002Fdocs\u002Ffundamentals\u002Flifecycle-events).",{"frontmatter":9,"meta":10,"nodes":38},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,26,29,32,35],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21,"children":22},"scheduled-functions","Scheduled functions",[23],{"id":24,"depth":14,"text":25},"scheduled-functions-cron-options","#[cron] options",{"id":27,"depth":13,"text":28},"cronticker","CronTicker",{"id":30,"depth":13,"text":31},"schedules-from-config","Schedules from config",{"id":33,"depth":13,"text":34},"error-handling","Error handling",{"id":36,"depth":13,"text":37},"next-steps","Next steps",[39,43,66,69,78,123,125,260,267,357,359,369,397,423,429,431,441,486,499,501,515,517],[40,41,5],"h1",{"id":42},"scheduling",[44,45,46,50,51,54,55,58,59,61,62,65],"p",{},[47,48,49],"code",{},"ferrite-scheduler"," adds cron-based scheduling in the NestJS ",[47,52,53],{},"@Cron","\u002F",[47,56,57],{},"CronJob"," style: declare schedules in your provider with attribute macros, and the module starts a ",[47,60,28],{}," that ticks over a ",[47,63,64],{},"cron"," schedule.",[67,68,18],"h2",{"id":17},[70,71,74],"pre",{"language":72,"class":73},"cargo","shiki dark:github-dark",[47,75,77],{"class":76},"language-cargo","fr add scheduler",[70,79,82],{"language":80,"class":81},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[47,83,85,108,109],{"class":84},"language-rust",[86,87,90,94,98,101,105],"span",{"class":88,"style":89},"line","display: inline",[86,91,93],{"style":92},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[86,95,97],{"style":96},"color:#D73A49;--shiki-dark:#F97583","=",[86,99,100],{"style":92}," [",[86,102,104],{"style":103},"color:#6F42C1;--shiki-dark:#B392F0","CronModuleImpl",[86,106,107],{"style":92},"])]","\n",[86,110,111,114,117,120],{"class":88,"style":89},[86,112,113],{"style":96},"pub",[86,115,116],{"style":96}," struct",[86,118,119],{"style":103}," AppModule",[86,121,122],{"style":92},";",[67,124,21],{"id":20},[70,126,127],{"language":80,"class":81},[47,128,129,108,143,108,155,108,157,108,162,108,178,108,180,108,190,108,210,108,237,108,250,108,255],{"class":84},[86,130,131,134,137,140],{"class":88,"style":89},[86,132,133],{"style":96},"use",[86,135,136],{"style":103}," ferrite_scheduler",[86,138,139],{"style":96},"::",[86,141,142],{"style":92},"cron;",[86,144,145,147,150,152],{"class":88,"style":89},[86,146,133],{"style":96},[86,148,149],{"style":103}," ferrite_framework",[86,151,139],{"style":96},[86,153,154],{"style":92},"injectable;",[86,156],{"class":88,"style":89},[86,158,159],{"class":88,"style":89},[86,160,161],{"style":92},"#[injectable]",[86,163,164,166,168,171,174],{"class":88,"style":89},[86,165,113],{"style":96},[86,167,116],{"style":96},[86,169,170],{"style":103}," DailyDigestService",[86,172,173],{"style":92},"; ",[86,175,177],{"style":176},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F or with #[inject] constructor + deps",[86,179],{"class":88,"style":89},[86,181,182,185,187],{"class":88,"style":89},[86,183,184],{"style":96},"impl",[86,186,170],{"style":103},[86,188,189],{"style":92}," {",[86,191,192,195,199,202,204,207],{"class":88,"style":89},[86,193,194],{"style":92},"    #[cron(",[86,196,198],{"style":197},"color:#032F62;--shiki-dark:#9ECBFF","\"0 8 * * *\"",[86,200,201],{"style":92},", name ",[86,203,97],{"style":96},[86,205,206],{"style":197}," \"daily_digest\"",[86,208,209],{"style":92},")]",[86,211,212,215,218,221,224,227,230,234],{"class":88,"style":89},[86,213,214],{"style":96},"    pub",[86,216,217],{"style":96}," async",[86,219,220],{"style":96}," fn",[86,222,223],{"style":103}," run",[86,225,226],{"style":92},"(",[86,228,229],{"style":96},"&",[86,231,233],{"style":232},"color:#005CC5;--shiki-dark:#79B8FF","self",[86,235,236],{"style":92},") {",[86,238,239,242,245,248],{"class":88,"style":89},[86,240,241],{"style":103},"        build_and_send_digest",[86,243,244],{"style":92},"()",[86,246,247],{"style":96},".await",[86,249,122],{"style":92},[86,251,252],{"class":88,"style":89},[86,253,254],{"style":92},"    }",[86,256,257],{"class":88,"style":89},[86,258,259],{"style":92},"}",[261,262,263,266],"h3",{"id":24},[47,264,265],{},"#[cron]"," options",[268,269,270,286],"table",{},[271,272,273],"thead",{},[274,275,276,280,283],"tr",{},[277,278,279],"th",{},"Option",[277,281,282],{},"Default",[277,284,285],{},"Purpose",[287,288,289,304,317,330,342],"tbody",{},[274,290,291,295,298],{},[292,293,294],"td",{},"(positional)",[292,296,297],{},"required",[292,299,300,301,303],{},"A ",[47,302,64],{}," crate schedule string",[274,305,306,311,314],{},[292,307,308],{},[47,309,310],{},"name",[292,312,313],{},"fn name",[292,315,316],{},"Identifier for the job",[274,318,319,324,327],{},[292,320,321],{},[47,322,323],{},"schedule",[292,325,326],{},"–",[292,328,329],{},"Same as positional; for codegen-referenced schedules",[274,331,332,337,339],{},[292,333,334],{},[47,335,336],{},"config_path",[292,338,326],{},[292,340,341],{},"Read the schedule from config at runtime (see below)",[274,343,344,349,354],{},[292,345,346],{},[47,347,348],{},"run_on_start",[292,350,351],{},[47,352,353],{},"false",[292,355,356],{},"Fire once when the ticker starts",[67,358,28],{"id":27},[44,360,361,363,364,368],{},[47,362,28],{}," is an injectable that owns the ticking loop. ",[365,366,367],"strong",{},"Construct it in your module with your provider"," so dependencies resolve under DI:",[70,370,371],{"language":80,"class":81},[47,372,373,108,377,108,388,108,393],{"class":84},[86,374,375],{"class":88,"style":89},[86,376,161],{"style":92},[86,378,379,381,383,386],{"class":88,"style":89},[86,380,113],{"style":96},[86,382,116],{"style":96},[86,384,385],{"style":103}," CronTicker",[86,387,189],{"style":92},[86,389,390],{"class":88,"style":89},[86,391,392],{"style":176},"    \u002F\u002F holds a loop that reads the job registry",[86,394,395],{"class":88,"style":89},[86,396,259],{"style":92},[70,398,399],{"language":80,"class":81},[47,400,401,108,413],{"class":84},[86,402,403,405,407,409,411],{"class":88,"style":89},[86,404,93],{"style":92},[86,406,97],{"style":96},[86,408,100],{"style":92},[86,410,104],{"style":103},[86,412,107],{"style":92},[86,414,415,417,419,421],{"class":88,"style":89},[86,416,113],{"style":96},[86,418,116],{"style":96},[86,420,119],{"style":103},[86,422,122],{"style":92},[44,424,425,428],{},[47,426,427],{},"CronModuleImpl::on_application_bootstrap"," starts the ticker, so schedules begin firing when the app is ready.",[67,430,31],{"id":30},[44,432,433,434,436,437,440],{},"Keeping schedules in env\u002Fconfig is supported two ways — via ",[47,435,336],{}," on the attribute, or by choosing a schedule expression directly when the module reads ",[47,438,439],{},"CRON_TICKER_*"," env vars:",[268,442,443,454],{},[271,444,445],{},[274,446,447,450,452],{},[277,448,449],{},"Env",[277,451,282],{},[277,453,285],{},[287,455,456,471],{},[274,457,458,463,468],{},[292,459,460],{},[47,461,462],{},"CLOCK_DEFAULT_SCHEDULE",[292,464,465],{},[47,466,467],{},"*\u002F15 * * * * * *",[292,469,470],{},"Fallback schedule",[274,472,473,478,483],{},[292,474,475],{},[47,476,477],{},"CLOCK_TIMEZONE",[292,479,480],{},[47,481,482],{},"utc",[292,484,485],{},"Ticker timezone",[44,487,488,489,492,493,498],{},"Using ",[47,490,491],{},"config_path = \"cron.daily_digest\""," reads the schedule from your app config (see ",[494,495,497],"a",{"href":496},"\u002Fdocs\u002Foverview\u002Fconfiguration","Configuration",") instead of hardcoding.",[67,500,34],{"id":33},[44,502,503,504,506,507,510,511,514],{},"A job returning an error logs it and the ticker continues; ",[47,505,57],{}," and ",[47,508,509],{},"cron::Schedule"," failures surface via ",[47,512,513],{},"CronError",".",[67,516,37],{"id":36},[518,519,520,527],"ul",{},[521,522,523,514],"li",{},[494,524,526],{"href":525},"\u002Fdocs\u002Ftechniques\u002Fbackground-jobs","Background jobs",[521,528,529,514],{},[494,530,532],{"href":531},"\u002Fdocs\u002Ffundamentals\u002Flifecycle-events","Lifecycle events",{"title":12,"searchDepth":13,"depth":14,"links":534},[535,536,539,540,541,542],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21,"children":537},[538],{"id":24,"depth":14,"text":25},{"id":27,"depth":13,"text":28},{"id":30,"depth":13,"text":31},{"id":33,"depth":13,"text":34},{"id":36,"depth":13,"text":37},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Fscheduling.md",{"title":526,"slug":545},"techniques\u002Fbackground-jobs",{"title":547,"slug":548},"CQRS","techniques\u002Fcqrs",1789565439199]