all files / dist/satellites/ pids.js

93.94% Statements 62/66
62.5% Branches 10/16
100% Functions 15/15
95.92% Lines 47/49
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                      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) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _fs = require('fs');
 
var _fs2 = _interopRequireDefault(_fs);
 
var _cluster = require('cluster');
 
var _cluster2 = _interopRequireDefault(_cluster);
 
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"); } }
 
var Pids = function () {
 
  /**
   * Class constructor.
   *
   * @param api API reference.
   */
 
 
  /**
   * Pids folder.
   */
 
 
  /**
   * API reference.
   */
 
  function Pids(api) {
    _classCallCheck(this, Pids);
 
    // save API reference
    this.api = api;
  }
 
  /**
   * Init the pid manager.
   */
 
 
  /**
   * Process title.
   */
 
 
  /**
   * Process ID.
   */
 
 
  _createClass(Pids, [{
    key: 'init',
    value: function init() {
      var self = this;
 
      // set the process id
      self.pid = process.pid;
 
      // save pids folder to syntax sugar
      self.path = self.api.config.general.paths.pid;
 
      // define the process name
      Eif (_cluster2.default.isMaster) {
        self.title = 'stellar-' + self._sanitizeId();
      } else {
        self.title = self._sanitizeId();
      }
 
      // create the 'pids' directory if not exists
      try {
        _fs2.default.mkdirSync(self.path);
      } catch (e) {}
    }
 
    /**
     * Write pid file.
     */
 
  }, {
    key: 'writePidFile',
    value: function writePidFile() {
      var self = this;
      _fs2.default.writeFileSync(self.path + '/' + self.title, self.pid.toString(), 'ascii');
    }
 
    /**
     * Clear pid file.
     */
 
  }, {
    key: 'clearPidFile',
    value: function clearPidFile() {
      var self = this;
 
      try {
        _fs2.default.unlinkSync(self.path + '/' + self.title);
      } catch (e) {
        self.api.log('Unable to remove pidfile', 'error', e);
      }
    }
 
    /**
     * Get a sanitized pid name for this process.
     *
     * The pid name is based on the process id.
     *
     * @returns {*}
     * @private
     */
 
  }, {
    key: '_sanitizeId',
    value: function _sanitizeId() {
      var self = this;
      var pidfile = self.api.id;
 
      pidfile = pidfile.replace(/:/g, '-');
      pidfile = pidfile.replace(/\s/g, '-');
      pidfile = pidfile.replace(/\r/g, '');
      pidfile = pidfile.replace(/\n/g, '');
 
      return pidfile;
    }
  }]);
 
  return Pids;
}();
 
var _class = function () {
  function _class() {
    _classCallCheck(this, _class);
 
    this.loadPriority = 110;
    this.startPriority = 1;
  }
 
  /**
   * Load priority.
   *
   * @type {number}
   */
 
 
  /**
   * Start priority.
   *
   * @type {number}
   */
 
 
  _createClass(_class, [{
    key: 'load',
 
 
    /**
     * Load initializer.
     *
     * @param api   API reference.
     * @param next  Callback.
     */
    value: function load(api, next) {
      // add pids class to the API
      api.pids = new Pids(api);
 
      // init pid manager
      api.pids.init();
 
      // finish the initializer load
      next();
    }
 
    /**
     * Start initializer.
     *
     * @param api   API reference.
     * @param next  Callback.
     */
 
  }, {
    key: 'start',
    value: function start(api, next) {
      // write pid file
      api.pids.writePidFile();
 
      // log the process pid
      api.log('pid: ' + process.pid, 'notice');
 
      // finish the initializer start
      next();
    }
  }]);
 
  return _class;
}();
 
exports.default = _class;
//# sourceMappingURL=pids.js.map