@@ -375,26 +375,25 @@ reports every night at midnight, check for abandoned carts every 15 minutes,
375375that sort of thing. We call these recurring jobs.
376376
377377CedarJS's job system has native support for these kinds of jobs by specifying a
378- ` cron ` schedule when scheduling a job:
378+ ` cron ` schedule when scheduling a job. The easiest way to schedule a cron job
379+ locally is probably to have a script do it. So first generate a script:
380+ ` yarn cedarjs generate script ScheduleCronJobs ` . Then update the script to look
381+ something like this:
379382
380- ``` js
381- import { jobs } from ' src/lib/jobs'
382-
383- export const NightlyReportJob = jobs .createJob ({
384- queue: ' default' ,
385- perform: async () => {
386- await DailyUsageReport .run ()
387- },
388- })
383+ ``` ts
384+ import { NightlyReportJob } from ' api/src/jobs/NightlyReportJob/NightlyReportJob'
385+ import { later } from ' api/src/lib/jobs'
389386
390- const scheduler = manager .createScheduler ({ adapter: ' prisma' })
387+ export default async () => {
388+ console .log (' Scheduling Nightly Report Job' )
391389
392- // Run every day at midnight
393- // highlight-start
394- await scheduler (NightlyReportJob, [], { cron: ' 0 0 * * *' })
395- // highlight-end
390+ await later (NightlyReportJob , { cron: ' 0 0 * * *' })
391+ }
396392```
397393
394+ Now you can just run that script and the job will be scheduled:
395+ ` yarn cedarjs exec ScheduleCronJobs `
396+
398397CedarJS uses https://github.com/harrisiirak/cron-parser under the hood for
399398parsing the ` cron ` schedule. So all the syntax supported by ` cron-parser ` is
400399supported. Including, for example, the six-groups format for seconds, and their
@@ -403,10 +402,10 @@ like `@hourly`, `@daily`, `@weekends`, etc. So the cron schedule from the
403402example above could also have been written as ` cron: '0 0 0 * * *' ` and
404403` cron: '@daily' ` .
405404
406- When you create recurring jobs you can not specify any options when you later
407- schedule the job ( like ` wait ` or ` waitUntil ` ). Those options don't make sense
408- for recurring jobs because they are scheduled automatically according to the
409- given schedule.
405+ When you schedule recurring jobs you can only specify the ` cron ` option. Other
406+ options, like ` wait ` or ` waitUntil ` , are not supported. These options don't make
407+ sense for recurring jobs because they are scheduled automatically according to
408+ the given schedule.
410409
411410## Configuration
412411
0 commit comments