Scheduling Redis population
The only thing that remains is to create a scheduler that polls our Redis populate
script, ./populate.js
.
First, let's install a scheduler named node-schedule
via NPM:
npm install node-schedule --save
We start by importing node-schedule
, which allows us to do cron-like scheduling. We call schedule.scheduleJob
every five minutes using */5
; however, it will also run as soon as the script starts. We then call populate.run
to start population:
var schedule = require('node-schedule') , logger = require('./lib/logger') , Populate = require('./lib/cache/populate') , populate = new Populate(); schedule.scheduleJob('*/5 * * * *', function() { populate.run(function(err) { if (err) logger.error('Redis Population error', err); if (!err) logger.info('Redis Population complete'); }); });
In order to run the application with real-time updates, open a new terminal and run the following command:
npm start
Now, open another terminal to run the Redis population script...