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

Function fin

lib/API/Startup.js:447–502  ·  view source on GitHub ↗

* Description * @method fin * @param {} err * @return

(err)

Source from the content-addressed store, hash-verified

445 * @return
446 */
447 function fin(err) {
448
449 // try to fix issues with empty dump file
450 // like #3485
451 if (!force && env_arr.length === 0 && !process.env.FORCE) {
452
453 // fix : if no dump file, no process, only module and after pm2 update
454 if (!fs.existsSync(cst.DUMP_FILE_PATH)) {
455 that.clearDump(function(){});
456 }
457
458 // if no process in list don't modify dump file
459 // process list should not be empty
460 if (cb) {
461 return cb(new Error('Process list empty, cannot save empty list'));
462 } else {
463 Common.printOut(cst.PREFIX_MSG_WARNING + 'PM2 is not managing any process, skipping save...');
464 Common.printOut(cst.PREFIX_MSG_WARNING + 'To force saving use: pm2 save --force');
465 that.exitCli(cst.SUCCESS_EXIT);
466 return;
467 }
468 }
469
470 // Back up dump file
471 try {
472 if (fs.existsSync(cst.DUMP_FILE_PATH)) {
473 fs.writeFileSync(cst.DUMP_BACKUP_FILE_PATH, fs.readFileSync(cst.DUMP_FILE_PATH));
474 }
475 } catch (e) {
476 console.error(e.stack || e);
477 Common.printOut(cst.PREFIX_MSG_ERR + 'Failed to back up dump file in %s', cst.DUMP_BACKUP_FILE_PATH);
478 }
479
480 // Overwrite dump file, delete if broken and exit
481 try {
482 fs.writeFileSync(cst.DUMP_FILE_PATH, JSON.stringify(env_arr, '', 2));
483 } catch (e) {
484 console.error(e.stack || e);
485 try {
486 // try to backup file
487 if (fs.existsSync(cst.DUMP_BACKUP_FILE_PATH)) {
488 fs.writeFileSync(cst.DUMP_FILE_PATH, fs.readFileSync(cst.DUMP_BACKUP_FILE_PATH));
489 }
490 } catch (e) {
491 // don't keep broken file
492 fs.unlinkSync(cst.DUMP_FILE_PATH);
493 console.error(e.stack || e);
494 }
495 Common.printOut(cst.PREFIX_MSG_ERR + 'Failed to save dump file in %s', cst.DUMP_FILE_PATH);
496 return that.exitCli(cst.ERROR_EXIT);
497 }
498 if (cb) return cb(null, {success:true});
499
500 Common.printOut(cst.PREFIX_MSG + 'Successfully saved in %s', cst.DUMP_FILE_PATH);
501 return that.exitCli(cst.SUCCESS_EXIT);
502 }
503
504 (function ex(apps) {

Callers 1

Startup.jsFile · 0.70

Calls 2

cbFunction · 0.85
exitCliMethod · 0.80

Tested by

no test coverage detected