Daemon
Objective
We just saw how easy building a service with SOAJS is, let's extend that to daemons.
This space will show you:
Create your daemon next to the node_modules directory:
mkdir /opt/soajs/myDaemonCreate 2 files in that directory:
index.js used to write your the code for the daemon jobs.
config.js used for custom daemon configuration.
The following code snippet shows how to write a daemon with SOAJS that has one job. The job simply prints a string message to the terminal. This message is loaded from the daemon configuration that is provisioned in the database. The message varies depending on the environment the daemon is running in.
The second file config.js, contains the daemon declaration information such as name and port number.
index.js
"use strict"; // "strict mode" in JavaScript
var soajs = require("soajs"); // require soajs framework
var config = require("./config"); // require local configuration file
var daemonDriver = new soajs.server.daemon(config); // create a daemon using SOAJS
daemonDriver.init(function () { // initialize the daemon
daemonDriver.job('myJob', function (soajs, next) { // create job "myJob" and pass SOAJS and next as parameters
//print the message value from daemon configuration above // formulate a message from the group configuration followed by the environment value and print it to the terminal
console.log(soajs.servicesConfig[process.env.SOAJS_ENV.toUpperCase()].message + " " + process.env.SOAJS_ENV.toUpperCase());
next();
});
daemonDriver.start(); // start the daemon
});config.js
"use strict"; // "strict mode" in JavaScript
var config = { // export the JSON object when the file is required
serviceVersion: 1, // specify the daemon version
serviceName: "daemonname", // specify the daemon name
serviceGroup: "My Group", // specify the daemon group
servicePort: 4171, // specify the daemon port
"errors": {}, // object containing error codes of the daemon
"schema": { // object containing the daemon jobs
"myJob": {
"l": "My Job" // first job name
}
}
};
module.exports = config;The daemon requires a group configuration to be provisioned in the database. Upon starting the daemon, the name of the group should be provided so that at runtime, the daemon load the configuration from that group.
{
"_id": ObjectId('57a350a7a7a0f5a214516d3a'),
"daemonConfigGroup": "GROUPNAME",
"daemon": "daemonname",
"interval": 60000, //wait 1 min after the last job finishes and then run again
"status": 1, //1 means active
"processing": "sequential", //if the daemon has more than one job, process the job sequentially or in parallel
"jobs": {
"myJob": {
"type": "global", //type of configuration: global or tenantSpecific
"serviceConfig": {
"DEV": { //environment code + configuration per environment
"message": "hi"
},
"PROD": { //environment code + configuration per environment
"catalog": "hello"
}
},
"tenantExtKeys": [], //if type is tenantSpecific, the list will contain tenant keys
"tenantsInfo": [] // if type is tenantSpecific, the list will contain tenant information
}
},
"order": [ //applied only if processing is sequential and the order of daemon job is based on array
"myJob"
],
"solo": false, //if more than one daemon are deployed for the same environment, only one will run
"type":"interval"
}Every daemon created with SOAJS is implemented using NodeJs therefore start the service using node in a terminal:
cd /opt/soajs/myDaemon
export SOAJS_ENV=dev
export SOAJS_DAEMON_GRP_CONF=GROUPNAME
node indexYour daemon will run and will be listening on the port specified in config.js which in this case is 4171. Your Daemon will also be listening on a second port used for maintenance purposes. That port is equal to the default port specified in config.js + 1000 and in this case has the value of 5171.
Once you start this daemon, on your terminal you will see the following message printed every 1 min.
//daemon started in dev
$ hi DEV
// after 60000 milliseconds
$ hi DEV
// after 60000 milliseconds
$ hi DEV