Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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


Image Added

Code Samples


Code Block
languagejs
titleconfig.js
linenumberstrue
"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 service user
   "dbs": [
      {
         prefix: "",
         name: "company",
         multitenant: false,
         mongo: {true
      }
  prefix: prefix],
   prerequisites: {
    name  cpu: "esClient"'',
      memory: ''
    es: true
 },
   serviceName: "composerDaemon",
   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"
         }
      }
   }
};

module.exports = daemon;




Code Block
languagejs
titleindex.js
linenumberstrue
"use strict";
var ],composer = require("soajs.composer");
composer.deploy(__dirname +"esIndexes": /config.js", function(error){
   console.log( (error)? error : "master": ["test_intersection"],Daemon started ...");
});



Middleware:

Code Block
languagejs
titlebuildMasterIndex job
linenumberstrue
"use strict";

var mongo = require("../model/mongo");

module.exports = {
   
   "profilecheckIfExists": ["clean"] function (soajs, next) {
      
      var condition = {
         "id": 1
      },;
      
      var combo = {
         prerequisites: {		//requirements for High Availability modecondition: condition
      };
      if(!soajs.data){
         soajs.data = {}
      }
      cpu: ''mongo.findEntry(soajs, combo, 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 = {
               "buildMasterIndexrecord": {			//name of the daemon job
                  "mwname": __dirname + "/lib/mw/master.js",		//business logic of the job"Joe",
                  "_apiInfoid": { 1,
                  "lposition": "Build Master Index""supervisor"
               }
            };
            
            mongo.insertEntries(soajs, combo2, function(error){
            "cleanUpProfileIndex": {   if(error){
                  return next(error);
               }
               else{
           "mw": __dirname + "/lib/mw/profile.js",
          console.log("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"          };
      },         
                "buildProfileIndex": {var combo1 = {
                  "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
languagejs
titleindexmongo.js
linenumberstrue
"use strict";
var database = 'company';
var composer collection = 'employees';

var lib = require("soajs.composer");  {
   "defaults": function (combo) {
      if (!combo.database) {
   //require the composer    composer.deploy(__dirname + "/../data/daemon.test2.js", function(error)combo.database = database
      }
      if (!combo.collection) {
         combo.collection = collection
      }
      if (!Object.hasOwnProperty.call(combo, 'versioning')) {
         combo.versioning = false;
      }
   },
   
   "findEntry": function (soajs, combo, cb) {
     //call composer.deploy to deploy the service from its config file
 
    console.log( (error)? error : "Cart Service started ...");   //optional log message after service is started
});

Example of a daemon job:

Code Block
languagejs
titlebuildMasterIndex job
linenumberstrue
"use strict";

module.exports = {
   
   "mw": function (soajs, cb) {
     
		// Business logic of daemon job goes here

      console.log("inside daemon mw file....");
      return cb(null, true);
   }
}; lib.defaults(combo);
      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);
   },
   "updateEntries": function (soajs, combo, cb) {
      lib.defaults(combo);
      soajs.mongo[combo.database].update(combo.collection, combo.condition, combo.updatedFields, combo.options || {}, combo.versioning || false, cb);
   }
};

module.exports = lib;


How to test


You can test this service for yourself by doing the following:

  • Create a local version of the folder schema on your machine
  • Copy the code from this page into their respective files
  • Run the following commands from a terminal window


Start controller

Code Block
languagebash
titleStart controller
linenumberstrue
# go to the controller directory and start the controller in the correct environment
 
cd /opt/soajs/node_modules/soajs.controller
 
export SOAJS_ENV=dev
export SOAJS_SRVIP=127.0.0.1
 
node .


Code Block
languagebash
titleStart daemon
linenumberstrue
# go to the service directory and start the service in the correct environment
 
 
cd /opt/soajs/node_modules/composer.example/daemon/insertRecords
 
 
export SOAJS_ENV=dev
export SOAJS_SRVIP=127.0.0.1
export SOAJS_DAEMON_GRP_CONF=catalog
 
node .