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AO,mBAEI,IAAT,EACF,MAAM,uCAAwC,GAE9C,MAAM,4CAA6C,EAAK,CAAE,OAAM,eAE7D,CACL,MAAM,EAAS,KAAK,MAAQ,EAAO,gBAC7B,EAAe,KAAK,IAAI,EAAG,gBAAkB,GAEnD,MAAM,iCAAkC,EAAK,CAAE,OAAM,SAAQ,SAAQ,iBACrE,YAAW,KACT,KAAK,EAAO,cAAe,EAAe,GACzC,EACL,CACF,IAEA,MAAM,qBAAsB,iBAE5B,IAAK,IAAI,EAAI,EAAG,EAAI,gBAAiB,IACnC,KAAK,EAAG,EAEZ","file":"/npm/master-process@4.2.0/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/master-process@4.2.0/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","var cluster = require('cluster');\nvar path    = require('path');\nvar fs      = require('fs');\nvar async   = require('async');\nvar _       = require('./lib/fakedash');\nvar monitor = require('./lib/monitor');\nvar debug   = require('debug')('master-process');\nvar os      = require('os');\nconst ms    = require('ms');\nconst proc_util = require('./lib/proc_util');\n\nvar cwd     = process.cwd();\n\nvar maxCpus = os.cpus().length;\nvar autoCpus = maxCpus > 1 ? maxCpus - 1 : 1;\n\nvar DESIRED_WORKERS;\nif (process.env.WORKERS === 'MAX') {\n  DESIRED_WORKERS = maxCpus;\n} else if (process.env.WORKERS === 'AUTO') {\n  DESIRED_WORKERS = autoCpus;\n} else {\n  DESIRED_WORKERS = parseInt(process.env.WORKERS || 1, 10) || 1;\n}\n\nconst WORKER_THROTTLE = typeof process.env.WORKER_THROTTLE === 'string' ? ms(process.env.WORKER_THROTTLE) : ms('1 second');\n\nfunction getVersion () {\n  var pkg = fs.readFileSync(path.join(__dirname, '/package.json'), 'utf8');\n  return JSON.parse(pkg).version;\n}\n\nvar version = getVersion();\n\n/**\n * Fork a new worker.\n *\n * The new worker will be monitored (cpu and memory)\n * and once is listening it will kill all the other workers\n * with different reload index.\n *\n * @param  {number}  worker_index the worker index (range: `0<=worker_index<DESIRED_WORKERS`)\n * @param  {number}  reload_counter number of times this process has been reloaded.\n * @param  {function(Worker)} [callback] called once the new worker is listening\n */\nfunction fork (worker_index, reload_counter, callback) {\n  process.chdir(cwd);\n\n  if (version !== getVersion()) {\n    debug('master-process changed, restarting');\n    return process.exit(1);\n  }\n\n  debug('starting a new worker');\n\n  const additionalEnvs = {\n    PPID: process.pid,\n    RELOAD_INDEX: reload_counter,\n    WORKER_INDEX: worker_index\n  };\n\n  const new_worker = cluster.fork(additionalEnvs);\n  new_worker._reload_counter = reload_counter;\n  new_worker._worker_index = worker_index;\n  new_worker._worker_started = Date.now();\n  new_worker._worker_terminated = false;\n\n  monitor(new_worker, debug, fork);\n\n  new_worker.once('listening', function () {\n    debug('PID/%s: worker is listening', new_worker.process.pid);\n\n    _.values(cluster.workers)\n    .filter(function (worker) {\n      return worker._reload_counter < reload_counter && worker._worker_index === worker_index;\n    })\n    .forEach(function (old_worker) {\n      var old_proc = old_worker.process;\n      debug('PID/%s: killing old worker ', old_proc.pid);\n      proc_util.terminate(old_worker);\n    });\n\n    if (callback) {\n      callback(new_worker);\n    }\n  });\n\n  return new_worker;\n}\n\nfunction ensurePermsAsync(path, perms) {\n  fs.chmod(path, perms, err => {\n    if (err) {\n      debug('Error setting permissions on %s', path, err);\n    }\n  });\n}\n\nmodule.exports.init = function () {\n  debug('starting master-process %s with pid %s and config: %o', version, process.pid, { DESIRED_WORKERS, WORKER_THROTTLE });\n  var reload_counter = 0;\n\n  if (process.env.PORT && process.env.PORT[0] === '/' && fs.existsSync(process.env.PORT)) {\n    fs.unlinkSync(process.env.PORT);\n  }\n\n  const unix_sockets = new Set();\n\n  process\n    .on('SIGHUP', function () {\n      reload_counter++;\n      for (var i = 0; i < DESIRED_WORKERS; i++) {\n        fork(i, reload_counter);\n      }\n    })\n    .on('SIGTERM', function () {\n\n      debug('SIGTERM: stopping all workers');\n\n      async.each(_.values(cluster.workers), function (worker, callback) {\n        worker.process.once('exit', callback);\n        proc_util.terminate(worker);\n      }, function () {\n        unix_sockets.forEach(function (socket) {\n          debug('SIGTERM: cleaning socket ' + socket);\n          try {\n            fs.unlinkSync(socket);\n          } catch(er){}\n        });\n\n        process.exit(0);\n      });\n\n    }).on('SIGUSR2', function () {\n      debug('SIGUSR2: sending the signal to all workers');\n      _.values(cluster.workers).forEach(function (worker) {\n        worker.kill('SIGUSR2');\n      });\n    });\n\n  cluster.once('listening', function (worker, address) {\n    debug('cluster is listening on %s', address.port || address.address);\n  }).on('listening', function (worker, address) {\n    if (address.addressType === -1) { // https://nodejs.org/api/cluster.html#cluster_event_listening_1\n      unix_sockets.add(address.address);\n\n      // due to https://github.com/nodejs/node/issues/19729 the underlying path may have\n      // been unlinked after this cluster has started. ensure that the permissions are\n      // correct whenever a new worker has bound to a UNIX socket.\n      ensurePermsAsync(address.address, '664');\n    }\n  }).on('exit', function (worker, code, signal) {\n    const pid = worker.process.pid;\n\n    if (worker._worker_terminated) {\n      // this is used here to distinguish expected/unexpected worker deaths.\n      if (code === 0) {\n        debug('PID/%s: terminated worker has exited', pid);\n      } else {\n        debug('PID/%s: terminated worker has crashed: %o', pid, { code, signal });\n      }\n    } else {\n      const uptime = Date.now() - worker._worker_started;\n      const restartDelay = Math.max(0, WORKER_THROTTLE - uptime);\n\n      debug('PID/%s: worker has crashed: %o', pid, { code, signal, uptime, restartDelay });\n      setTimeout(() => {\n        fork(worker._worker_index, reload_counter);\n      }, restartDelay);\n    }\n  });\n\n  debug('forking %s workers', DESIRED_WORKERS);\n\n  for (var i = 0; i < DESIRED_WORKERS; i++) {\n    fork(i, reload_counter);\n  }\n};\n"]}