diff --git a/docs/isolex.yml b/docs/isolex.yml index 6349593a3..b065ebf1f 100644 --- a/docs/isolex.yml +++ b/docs/isolex.yml @@ -75,6 +75,9 @@ data: - !include ../docs/parser/split-parser-math.yml - !include ../docs/parser/split-parser-sed.yml - !include ../docs/parser/yaml-parser.yml + process: + pid: + file: ./out/isolex.pid services: timeout: 5000 storage: diff --git a/src/Bot.ts b/src/Bot.ts index e771cf3b8..f8e3eab67 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -38,6 +38,11 @@ export interface BotData extends BaseServiceData { }; modules: Array; parsers: Array>; + process: { + pid: { + file: string; + }; + }; services: { timeout: number; }; diff --git a/src/index.ts b/src/index.ts index 34741dcf4..1421066f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -42,6 +42,8 @@ import { ModuleCtor } from 'src/utils/ExternalModule'; import { signal, SIGNAL_RELOAD, SIGNAL_RESET, SIGNAL_STOP } from 'src/utils/Signal'; import { VERSION_INFO } from 'src/version'; +import { writePid, removePid } from './utils/PidFile'; + // re-exports export const base = { BaseController, @@ -193,7 +195,9 @@ export async function main(argv: Array): Promise { botModule.setBot(bot); logger.info('starting bot'); + await writePid(config.data.process.pid.file); await handleSignals(bot, logger); + await removePid(config.data.process.pid.file); return STATUS_SUCCESS; } diff --git a/src/schema/schema.yml b/src/schema/schema.yml index ce47c5a3f..68cfa93f8 100644 --- a/src/schema/schema.yml +++ b/src/schema/schema.yml @@ -157,7 +157,7 @@ definitions: service-bot: type: object - required: [controllers, filters, listeners, locale, logger, modules, parsers, services, storage] + required: [controllers, filters, listeners, locale, logger, modules, parsers, process, services, storage] properties: controllers: type: array @@ -198,6 +198,16 @@ definitions: type: array items: $ref: "#/definitions/service-definition" + process: + type: object + required: [pid] + properties: + pid: + type: object + required: [file] + properties: + file: + type: string services: type: object additionalProperties: false diff --git a/src/utils/PidFile.ts b/src/utils/PidFile.ts new file mode 100644 index 000000000..44069d193 --- /dev/null +++ b/src/utils/PidFile.ts @@ -0,0 +1,32 @@ +import { open, unlink, write } from 'fs'; +import { pid } from 'process'; + +export async function writePid(path: string): Promise { + return new Promise((res, rej) => { + open(path, 'wx', (err: Error, fd: number) => { + if (err) { + rej(err); + } else { + write(fd, pid.toString(), 0, 'utf8', (err: Error) => { + if (err) { + rej(err); + } else { + res(); + } + }); + } + }); + }); +} + +export async function removePid(path: string): Promise { + return new Promise((res, rej) => { + unlink(path, (err: Error) => { + if (err) { + rej(err); + } else { + res(); + } + }); + }); +} diff --git a/test/TestBot.ts b/test/TestBot.ts index bbc47b901..7d83f89c8 100644 --- a/test/TestBot.ts +++ b/test/TestBot.ts @@ -48,6 +48,11 @@ describeAsync('bot service', async () => { }, modules: [], parsers: [], + process: { + pid: { + file: '../out/isolex.pid', + }, + }, services: { timeout: 1000, },