0

information I looking for :(

How to implementing cron job using Blitz.js.

what I done so far

have double-checked Blitz.js document.

what I want ask

Does anyone know or have been done implementing cron job using Blitz.js?? simple sample code would be very helpful.

Ryoma Kishimoto
  • 425
  • 1
  • 6
  • 16

2 Answers2

2

I was facing a similar issue, trying to implement cronjobs in Express layer of Blitzjs/Nextjs stack, turns out there is a solution for that, in blitzjs Documentation.

It's implemented it using Quirrel a queuing service for projects like NextJs & BlitzJs.

Plenty of code samples in the tutorial implementing how to use it.

ArchNoob
  • 3,946
  • 5
  • 32
  • 59
0

Implementing a cron job on Blitz is not different than on Next.js.

First of all you have to define your serverless function, under the /api folder (at the root, not in the rpc folder).

Then, create your file, such as:


import { api } from "app/blitz-server"
import db from "db"

export default api(async (req, res, ctx) => {
  try {
    // Do something here

    res.status(200).json({
      message: `Success!`,
    })
  } catch (e) {
    res.status(400).json({
      message: `Error : ${e.message}`,
    })
  }
})

Finally, you just need to create a cron job that target your api route.

For this, you have several free options:

Otherwise, you have many third party options, but not always free.

Hope this helps

Manu
  • 372
  • 2
  • 12