|
1
|
If you have a scheduled job that is configured with a cron expression, it may not run at the expected times sometimes. |
|
2
|
|
|
3
|
### Why this Happens |
|
4
|
|
|
5
|
When a scheduled job is configured with a cron expression, it will only run when the specified time is reached. |
|
6
|
|
|
7
|
So, if the events system is busy and the specified time is missed, the job will not run until the next scheduled time. |
|
8
|
|
|
9
|
### How to Fix it |
|
10
|
|
|
11
|
If you prioritize running your scheduled jobs consistently over precise timing, consider using a scheduled job interval instead. |
|
12
|
|
|
13
|
By using an interval, the job will run after a defined duration, regardless of the system's state. |
|
14
|
|
|
15
|
For example, to run a scheduled job every minute, you can set the interval to 60000 milliseconds (1 minute): |
|
16
|
|
|
17
|
```ts highlights={[["17"]]} |
|
18
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
19
|
import { |
|
20
|
ContainerRegistrationKeys, |
|
21
|
} from "@medusajs/framework/utils" |
|
22
|
|
|
23
|
export default async function greetingJob(container: MedusaContainer) { |
|
24
|
const logger = container.resolve( |
|
25
|
ContainerRegistrationKeys.LOGGER |
|
26
|
) |
|
27
|
|
|
28
|
logger.info("Greeting!") |
|
29
|
} |
|
30
|
|
|
31
|
export const config = { |
|
32
|
name: "greeting-every-minute", |
|
33
|
schedule: { |
|
34
|
interval: 60000, // 1 minute |
|
35
|
}, |
|
36
|
} |
|
37
|
``` |
|
38
|
|
|
39
|
### Additional Resources |
|
40
|
|
|
41
|
- [Set Interval for Scheduled Jobs](!docs!/learn/fundamentals/scheduled-jobs/interval) |