diff --git a/app/lib/common-libs/constants.ts b/app/lib/common-libs/constants.ts index 9b15d71a6f548bdc0923e0046e837a55142902cc..728e506b7eb5f85bcb41f239bf87b3448cf61b56 100755 --- a/app/lib/common-libs/constants.ts +++ b/app/lib/common-libs/constants.ts @@ -83,11 +83,6 @@ export const duniterDocument2str = (type:DuniterDocument) => { } } -export const ErrorConstants = { - - EXIT_CODE_MINDEX_WRITING_ERROR: 500 -} - export const CommonConstants = { FORMATS: { diff --git a/app/lib/common-libs/exit-codes.ts b/app/lib/common-libs/exit-codes.ts new file mode 100755 index 0000000000000000000000000000000000000000..f9bcf11a21e621792dd36c07c181f111deec5465 --- /dev/null +++ b/app/lib/common-libs/exit-codes.ts @@ -0,0 +1,10 @@ +export enum ExitCodes { + OK = 0, + UNHANDLED_ERROR = 1, + UNCAUGHT_EXCEPTION = 2, + DUNITER_NOT_RUNNING = 2, + SIGINT = 3, + SYNC_FAIL = 50, + FORCE_CLOSE_AFTER_ERROR = 100, + MINDEX_WRITING_ERROR = 500, +} diff --git a/app/lib/dal/indexDAL/sqlite/SqliteNodeIOManager.ts b/app/lib/dal/indexDAL/sqlite/SqliteNodeIOManager.ts index 10c630b3beafff3b2d0711bce9fd2ef4c8e6d042..2ddd55b633e644003aeead0624cf2d2ba3e76f7f 100644 --- a/app/lib/dal/indexDAL/sqlite/SqliteNodeIOManager.ts +++ b/app/lib/dal/indexDAL/sqlite/SqliteNodeIOManager.ts @@ -1,7 +1,7 @@ import {SQLiteDriver} from "../../drivers/SQLiteDriver" import {MonitorExecutionTime} from "../../../debug/MonitorExecutionTime" -import {ErrorConstants} from "../../../common-libs/constants" import {NewLogger} from "../../../logger" +import {ExitCodes} from "../../../common-libs/exit-codes" export class SqliteNodeIOManager<T> { @@ -24,7 +24,7 @@ export class SqliteNodeIOManager<T> { .then(() => this.driver.executeAll(sql, params)) .catch(e => { NewLogger().error(e) - process.exit(ErrorConstants.EXIT_CODE_MINDEX_WRITING_ERROR) + process.exit(ExitCodes.MINDEX_WRITING_ERROR) }) } diff --git a/app/modules/crawler/index.ts b/app/modules/crawler/index.ts index b14d31075d7651eebc0c0e79d578b60df0b44c9e..1afe22ac6dc1b87e9c357df65bf0af403d12a788 100644 --- a/app/modules/crawler/index.ts +++ b/app/modules/crawler/index.ts @@ -30,6 +30,7 @@ import {CommonConstants} from "../../lib/common-libs/constants" import {DataErrors} from "../../lib/common-libs/errors" import {NewLogger} from "../../lib/logger" import {CrawlerConstants} from "./lib/constants" +import {ExitCodes} from "../../lib/common-libs/exit-codes" export const CrawlerDependency = { duniter: { @@ -144,7 +145,7 @@ export const CrawlerDependency = { process.on('unhandledRejection', (reason) => { if (reason.message === DataErrors[DataErrors.NO_NODE_FOUND_TO_DOWNLOAD_CHUNK]) { NewLogger().error('Synchronization interrupted: no node was found to continue downloading after %s tries.', CrawlerConstants.SYNC_MAX_FAIL_NO_NODE_FOUND) - process.exit(1) + process.exit(ExitCodes.SYNC_FAIL) } }) diff --git a/app/modules/crawler/lib/sync/v2/ValidatorStream.ts b/app/modules/crawler/lib/sync/v2/ValidatorStream.ts index 85c4c2d875580ffb1177405655dec08f4183e3d0..a082a0782993e4e2f14ca6497661cdc9f2150020 100644 --- a/app/modules/crawler/lib/sync/v2/ValidatorStream.ts +++ b/app/modules/crawler/lib/sync/v2/ValidatorStream.ts @@ -6,6 +6,7 @@ import {DataErrors} from "../../../../../lib/common-libs/errors" import {NewLogger} from "../../../../../lib/logger" import {ISyncDownloader} from "../ISyncDownloader" import {Watcher} from "../Watcher" +import {ExitCodes} from "../../../../../lib/common-libs/exit-codes" export class ValidatorStream extends Readable { @@ -67,7 +68,7 @@ export class ValidatorStream extends Readable { await new Promise((res) => setTimeout(res, 3000)) if (failures >= 15) { NewLogger().error('Could not get a validation from remote blockchain after %s trials. Stopping sync.', failures) - process.exit(1) + process.exit(ExitCodes.SYNC_FAIL) } block = null } diff --git a/app/modules/daemon.ts b/app/modules/daemon.ts index 9fbff1423b4dec5f7cf07837b41b026a0ea39cc8..8d14bdba73a3d4c38f582d8a67f25934d69027b6 100644 --- a/app/modules/daemon.ts +++ b/app/modules/daemon.ts @@ -14,6 +14,7 @@ import {ConfDTO} from "../lib/dto/ConfDTO" import {Server} from "../../server" import {Directory, RealFS} from "../lib/system/directory" +import {ExitCodes} from "../lib/common-libs/exit-codes" const constants = require('../lib/constants'); const Tail = require("tail").Tail @@ -80,10 +81,10 @@ module.exports = { const pid = server.getDaemon().status() if (pid) { console.log('Duniter is running using PID %s.', pid) - process.exit(0) + process.exit(ExitCodes.OK) } else { console.log('Duniter is not running.') - process.exit(2) + process.exit(ExitCodes.DUNITER_NOT_RUNNING) } } }, { diff --git a/app/modules/prover/lib/powCluster.ts b/app/modules/prover/lib/powCluster.ts index 359a82533bbdf7e338e41fb3b2451d13a6db4751..207b55f12ff04c06781a44c6a025da34d04af9f7 100644 --- a/app/modules/prover/lib/powCluster.ts +++ b/app/modules/prover/lib/powCluster.ts @@ -18,6 +18,7 @@ import {PowWorker} from "./PowWorker" import {FileDAL} from "../../../lib/dal/fileDAL" import {Underscore} from "../../../lib/common-libs/underscore" import {ProofAsk} from "./blockProver" +import {ExitCodes} from "../../../lib/common-libs/exit-codes" const nuuid = require('node-uuid'); const cluster = require('cluster') @@ -263,7 +264,7 @@ if (cluster.isMaster) { } else { process.on("SIGTERM", function() { - process.exit(0) + process.exit(ExitCodes.OK) }); createPowWorker() diff --git a/app/modules/prover/lib/proof.ts b/app/modules/prover/lib/proof.ts index 50329b0a595ed6976b47dfc4390a4d5eb9951cc0..d01bfb0b68f688e42ddce8769851ffba6d1564a6 100644 --- a/app/modules/prover/lib/proof.ts +++ b/app/modules/prover/lib/proof.ts @@ -23,6 +23,7 @@ import {rawer} from "../../../lib/common-libs/index" import {ProcessCpuProfiler} from "../../../ProcessCpuProfiler" import {PowDAL} from "../../../lib/dal/fileDALs/PowDAL"; import {Directory} from "../../../lib/system/directory" +import {ExitCodes} from "../../../lib/common-libs/exit-codes" const querablep = require('querablep'); @@ -47,7 +48,7 @@ export function createPowWorker() { }); process.on('unhandledRejection', () => { - process.exit() + process.exit(ExitCodes.OK) }) process.on('message', async (message) => { diff --git a/bin/duniter b/bin/duniter index 47f0e6e7b075cd3c30981798d4add6226ae5d03f..a86b92a4250052ef9319f135b670a8531d2a3e06 100755 --- a/bin/duniter +++ b/bin/duniter @@ -1,6 +1,7 @@ #!/usr/bin/env node "use strict"; +const ExitCodes = require("../app/lib/common-libs/exit-codes").ExitCodes; const logger = require("../app/lib/logger").NewLogger(); // Specific errors handling @@ -8,7 +9,7 @@ process.on('uncaughtException', (err) => { // Dunno why this specific exception is not caught if (err.code && err.code !== "EADDRNOTAVAIL" && err.code !== "EINVAL" && err.code !== "ENOENT") { logger.error(err.stack || err.message || err); - process.exit(2); + process.exit(ExitCodes.UNCAUGHT_EXCEPTION); } }); @@ -18,13 +19,13 @@ process.on('uncaughtException', (err) => { const stack = require('../index').Statics.autoStack(); await stack.executeStack(process.argv); // Everything went well, close Duniter quietly. - process.exit(); + process.exit(ExitCodes.OK); } catch (e) { // If an unhandled error occured logger.error(e.stack || e.message || e); - process.exit(1); + process.exit(ExitCodes.UNHANDLED_ERROR); } finally { // If we did not succeed to close before, force close with error. - process.exit(100); + process.exit(ExitCodes.FORCE_CLOSE_AFTER_ERROR); } })() diff --git a/index.ts b/index.ts index 7d1358ec95b7901aac6ad62fa84b76113e8c001a..3d7418c598c1755745fd9e173948afd80568f221 100644 --- a/index.ts +++ b/index.ts @@ -28,6 +28,7 @@ import {Directory} from "./app/lib/system/directory" import {Underscore} from "./app/lib/common-libs/underscore" import {CliCommand, DuniterDependency, DuniterModule} from "./app/modules/DuniterModule" import {ProgramOptions} from "./app/lib/common-libs/programOptions" +import {ExitCodes} from "./app/lib/common-libs/exit-codes" const path = require('path'); const constants = require('./app/lib/constants'); @@ -319,10 +320,10 @@ export class Stack { // Save DB try { await server.disconnect(); - process.exit(); + process.exit(ExitCodes.OK); } catch (e) { logger.error(e); - process.exit(3); + process.exit(ExitCodes.SIGINT); } } })