all files / dist/ engine.js

80.84% Statements 173/214
56.48% Branches 61/108
84.09% Functions 37/44
79.59% Lines 156/196
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669                                                                              225× 225× 225×                                           27× 27×     27× 261×       27× 468×       27× 261× 675×         27×                                 18× 18×                                                                                                                                                                                                                       171×     171× 171×                                                                                                                                                                                                                                                                                                                                                                                                                                               18× 441×     441× 441× 441×     441× 216×       225×     225×   225× 225×     225× 225× 225×               225×   225× 81×     81× 81× 81×     144×         225× 225× 36×   36× 36× 36×     189×         225× 225× 225× 225×     225× 225× 225×       18× 441×   441×               18×     18×                                                                                                      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); // Enable source map support
 
 
// Module Dependencies
 
 
require('source-map-support/register');
 
var _lodash = require('lodash');
 
var _lodash2 = _interopRequireDefault(_lodash);
 
var _path = require('path');
 
var _path2 = _interopRequireDefault(_path);
 
var _async = require('async');
 
var _async2 = _interopRequireDefault(_async);
 
var _utils = require('./utils');
 
var _utils2 = _interopRequireDefault(_utils);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 * Main Stellar entry point class.
 *
 * This makes the system bootstrap, loading and execution all
 * satellites. Each initializer load new features to the
 * engine instance or perform a set of instruction to accomplish
 * a certain goal.
 */
 
var Engine = function () {
  _createClass(Engine, null, [{
    key: 'normalizeInitializerPriority',
 
 
    /**
     * Normalize satellite priorities.
     *
     * @param satellite Satellite instance to be normalized.
     */
    value: function normalizeInitializerPriority(satellite) {
      satellite.loadPriority = satellite.loadPriority || Engine.defaultPriorities.load;
      satellite.startPriority = satellite.startPriority || Engine.defaultPriorities.start;
      satellite.stopPriority = satellite.stopPriority || Engine.defaultPriorities.stop;
    }
 
    /**
     * Order satellites array by their priority.
     *
     * @param collection  Satellites array to be ordered.
     * @returns {Array}   New ordered array.
     */
 
 
    // ---------------------------------------------------------------------------------------------------------- [STATIC]
 
    /**
     * Default proprieties for the satellites.
     *
     * @type {{load: number, start: number, stop: number}}
     */
 
  }, {
    key: 'flattenOrderedInitializer',
    value: function flattenOrderedInitializer(collection) {
      var keys = [];
      var output = [];
 
      // get keys from the collection
      for (var key in collection) {
        keys.push(parseInt(key));
      }
 
      // sort the keys in ascendant way
      keys.sort(function (a, b) {
        return a - b;
      });
 
      // iterate the ordered keys and create the new ordered object to be outputted
      keys.forEach(function (key) {
        return collection[key].forEach(function (d) {
          return output.push(d);
        });
      });
 
      // return the new ordered object
      return output;
    }
 
    /**
     * Print fatal error on the console and exit from the engine
     * execution.
     *
     * @private
     * @param api     API instance.
     * @param errors  String or array with the fatal error(s).
     * @param type    String with the error type.
     */
 
  }, {
    key: 'fatalError',
    value: function fatalError(api, errors, type) {
      // if errors variables if not defined return
      Eif (!errors) {
        return;
      }
 
      // ensure the errors variable is an instance of Array
      if (!(errors instanceof Array)) {
        errors = [errors];
      }
 
      // log an emergency message
      api.log('Error with satellite step: ' + type, 'emergency');
 
      // log all the errors
      errors.forEach(function (err) {
        return api.log(err.stack, 'emergency');
      });
 
      // finish the process execution
      process.exit(1);
    }
 
    // ----------------------------------------------------------------------------------------------------------- [Class]
 
    /**
     * API object.
     *
     * This object will be shared across all the platform, it's here the
     * satellites will load logic and the developers access the functions.
     *
     * @type {{}}
     */
 
 
    /**
     * List with all satellites.
     *
     * @type {{}}
     */
 
 
    /**
     * Array with the initial satellites.
     *
     * @type {Array}
     */
 
 
    /**
     * Array with the load satellites.
     *
     * This array contains all the satellites who has a load method.
     *
     * @type {Array}
     */
 
 
    /**
     * Array with the start satellites.
     *
     * This array contains all the satellites who has a start method.
     *
     * @type {Array}
     */
 
 
    /**
     * Array with the stop satellites.
     *
     * This array contains all the satellites who has a stop method.
     *
     * @type {Array}
     */
 
  }]);
 
  /**
   * Create a new instance of the Engine.
   *
   * @param scope Initial scope
   */
 
  function Engine(scope) {
    _classCallCheck(this, Engine);
 
    this.api = {
      initialized: false,
      shuttingDown: false,
      running: false,
      started: false,
      bootTime: null,
 
      commands: {
        start: null,
        stop: null,
        restart: null
      },
 
      _self: null,
 
      log: null,
 
      scope: {}
    };
    this.satellites = {};
    this.initialSatellites = [];
    this.loadSatellites = [];
    this.startSatellites = [];
    this.stopSatellites = [];
 
    var self = this;
 
    // save current execution scope
    self.api.scope = scope;
 
    // save the engine reference for external calls
    self.api._self = self;
 
    // define a dummy logger
    //
    // this only should print error, emergency levels
    self.api.log = function (msg) {
      var level = arguments.length <= 1 || arguments[1] === undefined ? 'info' : arguments[1];
 
      // if we are on test environment don't use the console
      Eif (process.env.NODE_ENV === 'test') {
        return;
      }
 
      if (level === 'emergency' || level === 'error') {
        console.log('\u001b[31m[-] ' + msg + '\u001b[37m');
      } else if (level === 'info') {
        console.log('[!] ' + msg);
      }
    };
 
    // define the available engine commands
    self.api.commands = {
      start: self.start,
      stop: self.stop,
      restart: self.restart
    };
  }
 
  // ----------------------------------------------------------------------------------------- [State Manager Functions]
 
  /**
   * Start engine execution.
   *
   * @param callback This function is called when the Engine finish their startup.
   */
 
 
  _createClass(Engine, [{
    key: 'start',
    value: function start() {
      var callback = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
 
      var self = this;
 
      // if this function has called outside of the Engine the 'this'
      // variable has an invalid reference
      Iif (this._self) {
        self = this._self;
      }
      self.api._self = self;
 
      // print current execution path
      self.api.log('Current universe "' + self.api.scope.rootPath + '"', 'info');
 
      // start stage0 loading method
      self.stage0(callback);
    }
 
    /**
     * Stop the Engine execution.
     *
     * This method try shutdown the engine in a non violent way, this
     * starts to execute all the stop method on the supported satellites.
     *
     * @param callback Callback function to be executed at the stop end execution.
     */
 
  }, {
    key: 'stop',
    value: function stop() {
      var callback = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
 
      var self = this;
 
      // if this function has called outside of the Engine the 'this'
      // variable has an invalid reference
      Iif (this._self) {
        self = this._self;
      }
 
      Eif (self.api.running === true) {
        // stop Engine
        self.api.shuttingDown = true;
        self.api.running = false;
        self.api.initialized = false;
 
        // log a shutting down message
        self.api.log('Shutting down open servers and stopping task processing', 'alert');
 
        // if this is the second shutdown we need remove the `finalStopInitializer` callback
        Iif (self.stopSatellites[self.stopSatellites.length - 1].name === 'finalStopInitializer') {
          self.stopSatellites.pop();
        }
 
        // add the final callback
        self.stopSatellites.push(function finalStopInitializer(next) {
          // stop watch for file changes
          self.api.configs.unwatchAllFiles();
 
          // clear cluster PIDs
          self.api.pids.clearPidFile();
 
          // log a shutdown message
          self.api.log('Stellar has been stopped', 'alert');
          self.api.log('***', 'debug');
 
          // execute the callback on the next tick
          process.nextTick(function () {
            Eif (callback !== null) {
              callback(null, self.api);
            }
          });
 
          // async callback
          next();
        });
 
        // iterate all satellites and stop them
        _async2.default.series(self.stopSatellites, function (errors) {
          return Engine.fatalError(self.api, errors, 'stop');
        });
      } else if (self.api.shuttingDown === true) {
        // double sigterm; ignore it
      } else {
        // we can shutdown the Engine if it is not running
        self.api.log('Cannot shutdown Stellar, not running', 'error');
 
        // exists a callback?
        if (callback !== null) {
          callback(null, self.api);
        }
      }
    }
 
    /**
     * Restart the Stellar Engine.
     *
     * This execute a stop action and execute the stage2 load actions.
     *
     * @param callback Callback function to be executed at the restart end.s
     */
 
  }, {
    key: 'restart',
    value: function restart() {
      var callback = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
 
      var self = this;
 
      // if this function has called outside of the Engine the 'this'
      // variable has an invalid reference
      if (this._self) {
        self = this._self;
      }
 
      if (self.api.running === true) {
        // stop the engine
        self.stop(function (err) {
          // log error if present
          if (err) {
            self.api.log(err, 'error');
          }
 
          // start the engine again
          self.stage2(function (err) {
            if (err) {
              self.api.log(err, 'error');
            }
 
            // log a restart message
            self.api.log('*** Stellar Restarted ***', 'info');
 
            // exists a callback
            if (callback !== null) {
              callback(null, self.api);
            }
          });
        });
      } else {
        self.stage2(function (err) {
          // log any encountered error
          if (err) {
            self.api.log(err, 'error');
          }
 
          // log a restart message
          self.api.log('*** Stellar Restarted ***', 'info');
 
          // exists a callback
          if (callback !== null) {
            callback(null, self.api);
          }
        });
      }
    }
 
    // ------------------------------------------------------------------------------------------------ [States Functions]
 
    /**
     * First startup stage.
     *
     * Steps:
     *  - executes the initial satellites;
     *  - call stage1
     *
     * @param callback This callback only are executed at the end of stage2.
     */
 
  }, {
    key: 'stage0',
    value: function stage0() {
      var callback = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
 
      var self = this;
 
      // we need to load the config first
      var initialSatellites = [_path2.default.resolve(__dirname + '/satellites/config.js')];
      initialSatellites.forEach(function (file) {
        // get full file name
        var filename = file.replace(/^.*[\\\/]/, '');
 
        // get the first part of the file name
        var initializer = filename.split('.')[0];
 
        // get the initializer
        self.satellites[initializer] = new (require(file).default)();
 
        // add it to array
        self.initialSatellites.push(function (next) {
          return self.satellites[initializer].load(self.api, next);
        });
      });
 
      // stage1 is called at the end of execution of all initial satellites
      self.initialSatellites.push(function () {
        return self.stage1(callback);
      });
 
      // execute stage0 satellites in series
      _async2.default.series(self.initialSatellites, function (error) {
        return Engine.fatalError(self.api, error, 'stage0');
      });
    }
 
    /**
     * Second startup stage.
     *
     * Steps:
     *  - load all satellites into memory;
     *  - load satellites;
     *  - mark Engine like initialized;
     *  - call stage2.
     *
     * @param callback This callback only is executed at the stage2 end.
     */
 
  }, {
    key: 'stage1',
    value: function stage1() {
      var callback = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
 
      var self = this;
 
      // ranked object for all stages
      var loadSatellitesRankings = {};
      var startSatellitesRankings = {};
      var stopSatellitesRankings = {};
 
      // reset satellites arrays
      self.satellites = {};
 
      // function to load the satellites in the right place
      var loadSatellitesInPlace = function loadSatellitesInPlace(satellitesFiles) {
        var _loop = function _loop(key) {
          var f = satellitesFiles[key];
 
          // get satellite normalized file name and
          var file = _path2.default.normalize(f);
          var initializer = _path2.default.basename(f).split('.')[0];
          var ext = _lodash2.default.last(file.split('.'));
 
          // only load files with the `.js` extension
          if (ext !== 'js') {
            return 'continue';
          }
 
          // get initializer module and instantiate it
          self.satellites[initializer] = new (require(file).default)();
 
          // initializer load function
          var loadFunction = function loadFunction(next) {
            // check if the initializer have a load function
            Eif (typeof self.satellites[initializer].load === 'function') {
              self.api.log(' > load: ' + initializer, 'debug');
 
              // call `load` property
              self.satellites[initializer].load(self.api, function (err) {
                self.api.log('   loaded: ' + initializer, 'debug');
                next(err);
              });
            } else {
              next();
            }
          };
 
          // initializer start function
          var startFunction = function startFunction(next) {
            // check if the initializer have a start function
            if (typeof self.satellites[initializer].start === 'function') {
              self.api.log(' > start: ' + initializer, 'debug');
 
              // execute start routine
              self.satellites[initializer].start(self.api, function (err) {
                self.api.log('   started: ' + initializer, 'debug');
                next(err);
              });
            } else {
              next();
            }
          };
 
          // initializer stop function
          var stopFunction = function stopFunction(next) {
            if (typeof self.satellites[initializer].stop === 'function') {
              self.api.log(' > stop: ' + initializer, 'debug');
 
              self.satellites[initializer].stop(self.api, function (err) {
                self.api.log('   stopped: ' + initializer, 'debug');
                next(err);
              });
            } else {
              next();
            }
          };
 
          // normalize satellite priorities
          Engine.normalizeInitializerPriority(self.satellites[initializer]);
          loadSatellitesRankings[self.satellites[initializer].loadPriority] = loadSatellitesRankings[self.satellites[initializer].loadPriority] || [];
          startSatellitesRankings[self.satellites[initializer].startPriority] = startSatellitesRankings[self.satellites[initializer].startPriority] || [];
          stopSatellitesRankings[self.satellites[initializer].stopPriority] = stopSatellitesRankings[self.satellites[initializer].stopPriority] || [];
 
          // push loader state function to ranked arrays
          loadSatellitesRankings[self.satellites[initializer].loadPriority].push(loadFunction);
          startSatellitesRankings[self.satellites[initializer].startPriority].push(startFunction);
          stopSatellitesRankings[self.satellites[initializer].stopPriority].push(stopFunction);
        };
 
        // iterate all files
        for (var key in satellitesFiles) {
          var _ret = _loop(key);
 
          if (_ret === 'continue') continue;
        }
      };
 
      // get an array with all satellites
      loadSatellitesInPlace(_utils2.default.getFiles(__dirname + '/satellites'));
 
      // load satellites from all the active modules
      self.api.config.modules.forEach(function (moduleName) {
        // build the full path to the satellites folder
        var moduleSatellitePaths = self.api.scope.rootPath + '/modules/' + moduleName + '/satellites';
 
        // check if the folder exists
        if (_utils2.default.directoryExists(moduleSatellitePaths)) {
          loadSatellitesInPlace(_utils2.default.getFiles(moduleSatellitePaths));
        }
      });
 
      // organize final array to match the satellites priorities
      self.loadSatellites = Engine.flattenOrderedInitializer(loadSatellitesRankings);
      self.startSatellites = Engine.flattenOrderedInitializer(startSatellitesRankings);
      self.stopSatellites = Engine.flattenOrderedInitializer(stopSatellitesRankings);
 
      // on the end of loading all satellites set engine like initialized
      self.loadSatellites.push(function () {
        // mark engine like initialized
        self.api.initialized = true;
 
        // call stage2
        self.stage2(callback);
      });
 
      // start initialization process
      _async2.default.series(self.loadSatellites, function (errors) {
        return Engine.fatalError(self.api, errors, 'stage0');
      });
    }
 
    /**
     * Third startup stage.
     *
     * Steps:
     *  - start satellites;
     *  - mark Engine as running.
     *
     *  @param callback
     */
 
  }, {
    key: 'stage2',
    value: function stage2() {
      var callback = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
 
      var self = this;
 
      self.startSatellites.push(function (next) {
        // define Stellar like running
        self.api.running = true;
 
        self.api.bootTime = new Date().getTime();
        self.api.log('** Server Started @ ' + new Date() + ' ***', 'notice');
 
        // call the callback if it's present
        Eif (callback !== null) {
          callback(null, self.api);
        }
 
        next();
      });
 
      _async2.default.series(self.startSatellites, function (err) {
        return Engine.fatalError(self.api, err, 'stage2');
      });
    }
  }]);
 
  return Engine;
}();
 
Engine.defaultPriorities = {
  load: 100,
  start: 100,
  stop: 100
};
exports.default = Engine;
//# sourceMappingURL=engine.js.map