/
02 - Daemon

02 - Daemon

Introduction


The following is an example of a 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


config.js
"use strict";

var daemon = {
   "type": "daemon",
   "dbs": [
      {
         prefix: "",
         name: "company",
         multitenant: false,
         mongo: true
      }
   ],
   prerequisites: {
      cpu: '',
      memory: ''
   },
   serviceName: "composerDaemon",
   serviceGroup: "Composer",
   servicePort: 4211,
   "errors": {},
   "schema": {
      "insertRecords": {
         "mw": __dirname + "/lib/mw/add.daemon.js",
         "_apiInfo": {
            "l": "Insert Records"
         }
      }
   }
};

module.exports = daemon;


index.js
"use strict";
var composer = require("soajs.composer");
composer.deploy(__dirname +"/config.js", function(error){
   console.log( (error)? error : "Daemon started ...");
});


Middleware:

buildMasterIndex job
"use strict";

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

module.exports = {
   
   "checkIfExists": function (soajs, next) {
      
      var condition = {
         "id": 1
      };
      
      var combo = {
         condition: condition
      };
      if(!soajs.data){
         soajs.data = {}
      }
      mongo.findEntry(soajs, combo, function (error, oneRecord) {
         if (error) {
            return next(error);
         }
         if (oneRecord) {
            console.log("next()")
            next();
         }
         else {
            var combo2 = {
               "record": {
                  "name": "Joe",
                  "id": 1,
                  "position": "supervisor"
               }
            };
            
            mongo.insertEntries(soajs, combo2, function(error){
               if(error){
                  return next(error);
               }
               else{
                  console.log("insertEntries")
                  soajs.data.new= true;
                  next();
               }
            });
         }
      });
      
   },
   "update": function (soajs, end) {
      if (!soajs.data.new){
         var condition = {
            "id": 1
         };
         
         var combo = {
            condition: condition
         };
         
         mongo.findEntry(soajs, combo, function(error, oneRecord){
            if (error){
               return end(error);
            }
            else{
               var opt = {
                  ts: new Date().getTime()
               };
               
               var updated = {
                  "$set": opt
               };
               
               var combo1 = {
                  updatedFields: updated,
                  options: {
                     'upsert': false,
                     'multi': true
                  },
                  versioning: true,
                  condition: condition
               };
               mongo.updateEntries(soajs, combo1, function (error) {
                  if (error) {
                     return end(error);
                  }
                  
                  console.log("timestamp updated");
                  return end(null, true);
               });
            }
         });
      }
      else {
         return end(null, true);
      }
      
   }
};

Model:

mongo.js
"use strict";
var database = 'company';
var collection = 'employees';

var lib = {
   "defaults": function (combo) {
      if (!combo.database) {
         combo.database = database
      }
      if (!combo.collection) {
         combo.collection = collection
      }
      if (!Object.hasOwnProperty.call(combo, 'versioning')) {
         combo.versioning = false;
      }
   },
   
   "findEntry": function (soajs, combo, cb) {
      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

Start controller
# 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 .
Start daemon
# 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 .

Related content

Daemon Config File
Daemon Config File
More like this
01 - Service
01 - Service
More like this
Daemon Index File
Daemon Index File
More like this
Get Daemon
Get Daemon
More like this
02 Daemon
02 Daemon
More like this
Service Config File
Service Config File
More like this