medusajs / roadmap

3 stars 2 forks source link

Scheduled Jobs #24

Closed olivermrbl closed 5 months ago

olivermrbl commented 5 months ago

Scheduled Jobs in Medusa is a feature that lets developers schedule tasks or cron jobs to automate repetitive tasks. For example, you can push daily updates to an external ERP system or generate weekly sales reports.

The new Scheduled Jobs API will allow you to set up recurring tasks with only a few lines of code with a syntax that is familiar to any JavaScript developer.

Here's a basic example of the API:

// src/jobs/daily-erp-update.ts
import { ProductService, ScheduledJobsConfig, ScheduledJobsArgs } from "@medusajs/medusa"
import ErpService from "../services/erp"

export default async function ({ container }: ScheduledJobsArgs) {
    const productService: ProductService = container.resolve("productService")
    // Service for interacting with external ERP system
    const erpService: ErpService = container.resolve("erpService")

    const products = await productService.listAndCount()

    await erpService.update(products)
}

export const config: ScheduledJobsConfig = {
  name: "daily-erp-update",
    schedule: "0 0 * * *" // At 00:00 every night
}
olivermrbl commented 5 months ago

Released in v1.18.0.

Read the announcement post and documentation.