DEV Community

Anth Rogan
Anth Rogan

Posted on

AdonisJS prldfiles

Lets make AdonisJS run some code on start up

Super useful but often overlooked is being able to run code on start up with Adonis. This is great for starting up services like CRON jobs or event listeners.

Adonis documentation describes how to do this in the context of their Events functionality so it can be a bit difficult to find unless you've read the whole documentation, hence this very short post!

Setup

Adonis has something called prldfiles aka preload files. This is a file that can be "hooked" into the app ready event on start-up, once everything is up and running. Let's make one using the terminal

node ace make:prldfile someFile
Enter fullscreen mode Exit fullscreen mode

Select your enviroments, in this case we want to select During HTTP Server using the space bar.

This command will create a new file in your start folder as well as modify your .adonisrc.js file instructing the file to run when the HTTP server starts

πŸ“¦ adonis-app
 ┣ πŸ“‚ app
 ┣ πŸ“‚ commands
 ┣ πŸ“‚ config
 ┣ πŸ“‚ contracts
 ┣ πŸ“‚ node_modules
 ┣ πŸ“‚ providers
 β”—  πŸ“‚ start
   ┣ πŸ“œ kernal.ts
   ┣ πŸ“œ routes.ts
   β”— πŸ“œ someFile.ts <-- this is your new file 
Enter fullscreen mode Exit fullscreen mode

In your .adonisrc.js you'll see this entry in the preloads property

    {
      "file": "./start/someFile",
      "environment": [
        "web"
      ]
    },
Enter fullscreen mode Exit fullscreen mode

Examples

Ok now thats all setup lets get some code in there. open your someFile.ts and add a simple console log and run the server

/*
|--------------------------------------------------------------------------
| Preloaded File
|--------------------------------------------------------------------------
|
| Any code written inside this file will be executed during the application
| boot.
|
*/

console.log('this is a preload file')
Enter fullscreen mode Exit fullscreen mode

You'll notice your console log in there as there server starts. And that is basically it, you can now call any cron library, job queue or any code you desire. I use this all the time for starting Bull jobs with a cron schedule like so

jobRunner.ts

/*
|--------------------------------------------------------------------------
| Preloaded File
|--------------------------------------------------------------------------
|
|   Job Runner simply creates a cron job on HTTP Server start
|
|--------------------------------------------------------------------------
*/

import Bull from '@ioc:Rocketseat/Bull'
import Job from 'App/Jobs/Getcustomer'
import Logger from '@ioc:Adonis/Core/Logger'

Logger.info('Preload Job Runner loaded')

Bull.add(new Job().key, data, {
  repeat: {
    cron: '* * * * *',
  },
})
Enter fullscreen mode Exit fullscreen mode

Another use case for this is as described by the Adonis team making a Event router
https://docs.adonisjs.com/guides/events#usage

Hope this helps someone somewhere πŸ‘

Top comments (1)

Collapse
 
fschwall profile image
Fschwall

Great article! Thanks a lot