MCPcopy
hub / github.com/Unitech/pm2 / daemonize

Method daemonize

modules/pm2-io-agent/src/InteractorClient.js:171–291  ·  view source on GitHub ↗

* Start or Restart the Interaction Daemon depending if its online or not * @private * @param {Object} conf global constants * @param {Object} infos data used to start the interactor [can be recovered from FS] * @param {String} infos.secret_key the secret key used to cipher data * @par

(cst, conf, cb)

Source from the content-addressed store, hash-verified

169 * @param {Function} cb invoked with <err, msg, process>
170 */
171 static daemonize (cst, conf, cb) {
172 const InteractorJS = path.resolve(path.dirname(module.filename), 'InteractorDaemon.js')
173 const PM2Path = require.main.filename
174
175 // Redirect PM2 internal err and out
176 // to STDERR STDOUT when running with Travis
177 const testEnv = process.env.TRAVIS || (process.env.NODE_ENV && process.env.NODE_ENV.match(/test/))
178 const out = testEnv ? 1 : fs.openSync(constants.INTERACTOR_LOG_FILE_PATH, 'a')
179 const err = testEnv ? 2 : fs.openSync(constants.INTERACTOR_LOG_FILE_PATH, 'a')
180
181 let binary = process.execPath
182
183 if (cst.IS_BUN === true)
184 binary = process.execPath
185 else if (binary.indexOf('node') === -1)
186 binary = 'node'
187 if (process.env.NODEJS_EXECUTABLE)
188 binary = process.env.NODEJS_EXECUTABLE
189
190 const child = childProcess.spawn(binary, [InteractorJS], {
191 silent: false,
192 detached: true,
193 windowsHide: true,
194 cwd: process.cwd(),
195 env: Object.assign({
196 PM2_HOME: cst.PM2_HOME,
197 PM2_MACHINE_NAME: conf.machine_name,
198 PM2_SECRET_KEY: conf.secret_key,
199 PM2_PUBLIC_KEY: conf.public_key,
200 PM2_REVERSE_INTERACT: conf.reverse_interact,
201 PM2_BINARY_PATH: PM2Path,
202 KEYMETRICS_NODE: conf.info_node,
203 PM2_VERSION: conf.pm2_version,
204 DEBUG: process.env.DEBUG || 'interactor:*,-interactor:axon,-interactor:websocket,-interactor:pm2:client,-interactor:push'
205 }, process.env),
206 stdio: [null, out, err, 'ipc'], // Redirect stdout, stderr, and enable IPC
207 //stdio: ['ipc', out, err]
208 })
209
210 try {
211 let prevPid = fs.readFileSync(constants.INTERACTOR_PID_PATH)
212 prevPid = parseInt(prevPid)
213 process.kill(prevPid)
214 } catch (e) {
215 }
216
217 let pid = ''
218
219 if (child.pid)
220 pid = child.pid.toString()
221
222 fs.writeFileSync(cst.INTERACTOR_PID_PATH, pid)
223
224 child.on('close', (status) => {
225 if (status === constants.ERROR_EXIT) {
226 return cb(new Error('Agent has shutdown for unknown reason'))
227 }
228

Callers 2

launchOrAttachMethod · 0.95

Calls 12

cbFunction · 0.85
printOutFunction · 0.85
resolveMethod · 0.80
indexOfMethod · 0.80
cwdMethod · 0.80
toStringMethod · 0.80
killMethod · 0.65
onMethod · 0.65
onceMethod · 0.65
unrefMethod · 0.65
removeAllListenersMethod · 0.65
disconnectMethod · 0.65

Tested by

no test coverage detected