2.9. Scheduled Jobs

In this chapter, you’ll learn about scheduled jobs and how to use them.

What is a Scheduled Job?#

A scheduled job is a function executed at a specified interval of time in the background of your Medusa application. It’s like a cron job that runs during the application's runtime.

For example, you can synchronize your inventory with an Enterprise Resource Planning (ERP) system once a day using a scheduled job.


How to Create a Scheduled Job?#

A scheduled job is created in a TypeScript or JavaScript file under the src/jobs directory.

For example, create the file src/jobs/hello-world.ts with the following content:

src/jobs/hello-world.ts
1// the scheduled-job function2export default function () {3  console.log("Time to say hello world!")4}5
6// the job's configurations7export const config = {8  name: "every-minute-message",9  // execute every minute10  schedule: "* * * * *",11}

A scheduled job file must export:

  • A function to be executed whenever it’s time to run the scheduled job.
  • A configuration object defining the job. It has two properties:
    • name: a unique name for the job.
    • schedule: a cron expression specifying when to run the job.

This scheduled job executes every minute and logs into the terminal Time to say hello world!.

Test Scheduled Jobs#

To test out your scheduled job, start the Medusa application:

After a minute, the following message will be logged to the terminal:

Terminal
Time to say hello world!

When to Use Scheduled Jobs#

Use scheduled jobs when
  • You're executing an action at a specified time interval during application runtime.
  • The action must be executed automatically.
Don't use scheduled jobs if
  • You want the action to execute at a specified time interval while the Medusa application isn't running. Instead, use the operating system's equivalent of a cron job.
  • You want to execute the action once. Use loaders instead.
  • You want to execute the action if an event occurs. Use subscribers instead.

Resolve Resources#

The scheduled job function receives a container parameter, which is the Medusa container. Use it to resolve resources in your Medusa application, such as services.

For example:

src/jobs/hello-world.ts
1import {2  IProductModuleService,3  MedusaContainer,4} from "@medusajs/framework/types"5import { Modules } from "@medusajs/framework/utils"6
7export default async function myCustomJob(8  container: MedusaContainer9) {10  const productModuleService: IProductModuleService = 11    container.resolve(Modules.PRODUCT)12
13  const [, count] = await productModuleService.listAndCountProducts()14
15  console.log(16    `Time to check products! You have ${count} product(s)`17  )18}19
20export const config = {21  name: "every-minute-message",22  // execute every minute23  schedule: "* * * * *",24}

In the scheduled job function, you resolve the Product Module's main service and retrieve the number of products in the store, then log the number in the terminal.

Was this chapter helpful?
Edit this page