Scheduler for AdonisJS without Command

AdonisJS is Laravel-like framework written for a Node. If you want to run a task like cron, there is a package called: adonis-scheduler

Assumptions / Requirements

I am assuming that you have already followed the installation steps of adonis-scheduler. Once you are done, following steps is what it takes to make ‘cron-less’ scheduler for your Adonis App

Running Scheduler without Command

Step 1 – Set up Scheduler

Create a file called scheduler.js in start folder with following content:

// start/scheduler.js

'use strict'

const Scheduler = use('Scheduler')
Scheduler.run()

Step 2 – Load with Server

Open server.js file and load the above file with preLoad(‘start/start-scheduler’)

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .preLoad('start/scheduler')
  .fireHttpServer()
  .catch(console.error)

This way, you don’t need pm2 setup and scheduler will not load with ace commands.

Cron syntax for JavaScript

Cron for JavaScript supports second as well as described following:

 # ┌────────────── second (optional)
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

Scheduler for AdonisJS without Command

4 thoughts on “Scheduler for AdonisJS without Command

  1. There is any other drop in way to do it like a folder which adonis read configuration from? You created a file then had to specify the location of that file. Particularly this thing I want to avoid.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top