3.8. Scheduled Jobs

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

What is a Scheduled Job?#

When building your commerce application, you may need to automate tasks and run them repeatedly at a specific schedule. For example, you need to automatically sync products to a third-party service once a day.

In other commerce platforms, this feature isn't natively supported. Instead, you have to setup a separate application to execute cron jobs, which adds complexity as to how you expose this task to be executed in a cron job, or how do you debug it when it's not running within the platform's tooling.

Medusa removes this overhead by supporting this feature natively with scheduled jobs. A scheduled job is an asynchronous function that the Medusa application runs at the interval you specify during the Medusa application's runtime. Your efforts are only spent on implementing the functionality performed by the job, such as syncing products to an ERP.

Don't use scheduled jobs if
  • You want the action to execute at a specified schedule 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 when the application loads. Use loaders instead.
  • You want to execute the action if an event occurs. Use subscribers instead.

How to Create a Scheduled Job?#

You create a scheduled job in a TypeScript or JavaScript file under the src/jobs directory. The file exports the asynchronous function to run, and the configurations indicating the schedule to run the function.

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

Example of scheduled job file in the application's directory structure

src/jobs/hello-world.ts
1import { MedusaContainer } from "@medusajs/framework/types"2
3export default async function greetingJob(container: MedusaContainer) {4  const logger = container.resolve("logger")5
6  logger.info("Greeting!")7}8
9export const config = {10  name: "greeting-every-minute",11  schedule: "* * * * *",12}

You export an asynchronous function that receives the Medusa container as a parameter. In the function, you resolve the Logger utility from the Medusa container and log a message.

You also export a config object that has the following properties:

  • name: A unique name for the job.
  • schedule: A string that holds a cron expression indicating the schedule to run the job.

This scheduled job executes every minute and logs into the terminal Greeting!.

Test the Scheduled Job#

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

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

Terminal
info:    Greeting!

Example: Sync Products Once a Day#

In this section, you'll find a brief example of how you use a scheduled job to sync products to a third-party service.

When implementing flows spanning across systems or modules, you use workflows. A workflow is a task made up of a series of steps, and you construct it like you would a regular function, but it's a special function that supports rollback mechanism in case of errors, background execution, and more.

You can learn how to create a workflow in this chapter, but this example assumes you already have a syncProductToErpWorkflow implemented. To execute this workflow once a day, create a scheduled job at src/jobs/sync-products.ts with the following content:

src/jobs/sync-products.ts
1import { MedusaContainer } from "@medusajs/framework/types"2import { syncProductToErpWorkflow } from "../workflows/sync-products-to-erp"3
4export default async function syncProductsJob(container: MedusaContainer) {5  await syncProductToErpWorkflow(container)6    .run()7}8
9export const config = {10  name: "sync-products-job",11  schedule: "0 0 * * *",12}

In the scheduled job function, you execute the syncProductToErpWorkflow by invoking it and passing it the container, then invoking the run method. You also specify in the exported configurations the schedule 0 0 * * * which indicates midnight time of every day.

The next time you start the Medusa application, it will run this job every day at midnight.

Was this chapter helpful?
Edit this page