diff --git a/lib/API/Startup.js b/lib/API/Startup.js index 36c74ce6..fc701cdc 100644 --- a/lib/API/Startup.js +++ b/lib/API/Startup.js @@ -412,18 +412,51 @@ module.exports = function(CLI) { var apps = {}; var that = this; - Common.printOut(cst.PREFIX_MSG + 'Restoring processes located in %s', cst.DUMP_FILE_PATH); + var processes; - try { - apps = fs.readFileSync(cst.DUMP_FILE_PATH); - } catch(e) { - Common.printError(cst.PREFIX_MSG_ERR + 'No processes saved; DUMP file doesn\'t exist'); - // if (cb) return cb(Common.retErr(e)); - // else return that.exitCli(cst.ERROR_EXIT); - return that.speedList(); + function readDumpFile(dumpFilePath) { + Common.printOut(cst.PREFIX_MSG + 'Restoring processes located in %s', cst.DUMP_FILE_PATH); + try { + var apps = fs.readFileSync(dumpFilePath); + } catch (e) { + Common.printError(cst.PREFIX_MSG_ERR + 'Failed to read dump file in %s', dumpFilePath); + throw e; + } + + return apps; } - var processes = Common.parseConfig(apps, 'none'); + function parseDumpFile(dumpFilePath, apps) { + try { + var processes = Common.parseConfig(apps, 'none'); + } catch (e) { + Common.printError(cst.PREFIX_MSG_ERR + 'Failed to parse dump file in %s', dumpFilePath); + try { + fs.unlinkSync(dumpFilePath); + } catch (e) { + console.error(e.stack || e); + } + throw e; + } + + return processes; + } + + // Read dump file, fall back to backup, delete if broken + try { + apps = readDumpFile(cst.DUMP_FILE_PATH); + processes = parseDumpFile(cst.DUMP_FILE_PATH, apps); + } catch(e) { + try { + apps = readDumpFile(cst.DUMP_BACKUP_FILE_PATH); + processes = parseDumpFile(cst.DUMP_BACKUP_FILE_PATH, apps); + } catch(e) { + Common.printError(cst.PREFIX_MSG_ERR + 'No processes saved; DUMP file doesn\'t exist'); + // if (cb) return cb(Common.retErr(e)); + // else return that.exitCli(cst.ERROR_EXIT); + return that.speedList(); + } + } that.Client.executeRemote('getMonitorData', {}, function(err, list) { if (err) {