Introduction
The following is an example of a complete runnable daemon.
This daemon is part of the composer test cases that were installed from npm.
Let's go through the different files relating to this daemon:daemon that uses the composer to function.
Similar to with services, we adhere to this folder schema to maintain code standardization.
Folder Schema
Code Samples
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
"use strict"; var prefixdaemon = ''; module.exports = { "type": "daemon", //tells the composer whether this is the config for a service or a daemon "dbs": [ //tells the composer what databases does this daemon use "dbs": [ { prefix: "", name: "company", multitenant: false, mongo: {true } prefix: prefix], prerequisites: { name cpu: "esClient"'', memory: '' }, esserviceName: "composerDaemon", true serviceGroup: "Composer", } servicePort: 4211, "errors": {}, { "schema": { prefix"insertRecords": prefix,{ name"mw": "myDatabase__dirname + "/lib/mw/add.daemon.js", multitenant: false,"_apiInfo": { mongo: true"l": "Insert Records" } } ], "esIndexes": { "master": ["test_intersection"],} }; module.exports = daemon; |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
"use strict";
var composer = require("soajs.composer");
composer.deploy(__dirname +"/config.js", function(error){
console.log( (error)? error : "Daemon started ...");
}); |
Middleware:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
"use strict"; var mongo = require("../model/mongo"); module.exports = { "profile": ["clean"]checkIfExists": function (soajs, next) { var condition = { "id": 1 },; var combo = { prerequisites: { //requirements for High Availability modecondition: condition }; if(!soajs.data){ soajs.data = {} } mongo.findEntry(soajs, combo, cpu: '',function (error, oneRecord) { memory: '' if (error) { return next(error); }, if (oneRecord) { serviceName: "aggregator", //the name of our daemonconsole.log("next()") serviceGroup: "411AGG", //the name of the daemon group next(); servicePort: 4060, //the port where the daemon will run } "errors": {}, //list of errors that can be returned by this daemon "schema":else { var combo2 = { "buildMasterIndex "record": { //name of the daemon job "mwname": __dirname + "/lib/mw/master.js", //business logic of the jobJoe", "_apiInfoid": { 1, "lposition": "Build Master Index"supervisor" } }; mongo.insertEntries(soajs, combo2, function(error){ if(error){ return next(error); "cleanUpProfileIndex": { } else{ console.log("mw": __dirname + "/lib/mw/profile.js", insertEntries") soajs.data.new= true; next(); "_apiInfo": { } "l": "Clean Profile Cities, Features & Categories from Autocomplete Indexes"}); } },); }, "buildProfileAccessoriesIndex": update": function (soajs, end) { if (!soajs.data.new){ var condition = { "mwid": __dirname + "/lib/mw/profile.js", 1 }; var combo "_apiInfo":= { "l": "Build Profile Categories, Cities, Features and Auto Complete Indexes"condition: condition }; mongo.findEntry(soajs, combo, function(error, oneRecord){ if (error){ return end(error); } }, else{ var opt "buildProfilecategoryCitiesIndex":= { "mw": __dirname + "/lib/mw/profile.js", ts: new Date().getTime() }; var updated "_apiInfo":= { "l$set": opt "Build Profile Category_cities Indexes" }; }, var combo1 "buildProfileIndex":= { "mw": __dirname + "/lib/mw/profile.js", updatedFields: updated, "_apiInfo"options: { "l": "Build Profile Addresses and Autocomplete Indexes" 'upsert': false, 'multi': true } }, "fullTest": { versioning: true, "mw": __dirname + "/lib/mw/fullRun.js",condition: condition }; mongo.updateEntries(soajs, combo1, function (error) { "_apiInfo": {if (error) { return end(error); } "l": "Build Both Master and Profile Indexes" console.log("timestamp updated"); return end(null, true); }); } }); } else { return end(null, true); } } }; |
Model:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
"use strict"; var database = 'company'; var composercollection = 'employees'; var lib = { require("soajs.composer"); //require the composerdefaults": function (combo) { if (!combo.database) { composer.deploy(__dirname + "/../data/daemon.test2.js", function(error)combo.database = database } if (!combo.collection) { combo.collection = collection } if (!Object.hasOwnProperty.call(combo, 'versioning')) { //call composer.deploy to deploy the service from its config file combo.versioning = false; } }, console.log( (error)? error : "Cart Service started ...""findEntry": function (soajs, combo, cb) { lib.defaults(combo); //optional log message after service is started }); |
Example of a daemon job:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
"use strict"; module.exports = { "mw soajs.mongo[combo.database].findOne(combo.collection, combo.condition || {}, combo.fields || {}, cb); }, "insertEntries": function (soajs, combo, cb) { lib.defaults(combo); soajs.mongo[combo.database].insert(combo.collection, combo.record, combo.versioning || false, cb); }, // Business logic of daemon job goes here "updateEntries": function (soajs, combo, cb) { consolelib.log("inside daemon mw file...."); return cb(null, truedefaults(combo); soajs.mongo[combo.database].update(combo.collection, combo.condition, combo.updatedFields, combo.options || {}, combo.versioning || false, cb); } }; module.exports = lib; |