Skip to content

Commit

Permalink
feat(config): add pid file
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jun 3, 2019
1 parent b9a9fa5 commit 1ea52ca
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/isolex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export interface BotData extends BaseServiceData {
};
modules: Array<ExternalModule>;
parsers: Array<ServiceDefinition<ParserData>>;
process: {
pid: {
file: string;
};
};
services: {
timeout: number;
};
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -193,7 +195,9 @@ export async function main(argv: Array<string>): Promise<number> {
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;
}
Expand Down
12 changes: 11 additions & 1 deletion src/schema/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/utils/PidFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { open, unlink, write } from 'fs';
import { pid } from 'process';

export async function writePid(path: string): Promise<void> {
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<void> {
return new Promise((res, rej) => {
unlink(path, (err: Error) => {
if (err) {
rej(err);
} else {
res();
}
});
});
}
5 changes: 5 additions & 0 deletions test/TestBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ describeAsync('bot service', async () => {
},
modules: [],
parsers: [],
process: {
pid: {
file: '../out/isolex.pid',
},
},
services: {
timeout: 1000,
},
Expand Down

0 comments on commit 1ea52ca

Please sign in to comment.