Skip to content

Commit

Permalink
fix: copy pid utils from lib
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jul 15, 2020
1 parent b1b5d9b commit 25d4b64
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removePid, signal, SIGNAL_RELOAD, SIGNAL_RESET, SIGNAL_STOP, writePid } from '@apextoaster/js-utils';
import { signal, SIGNAL_RELOAD, SIGNAL_RESET, SIGNAL_STOP } from '@apextoaster/js-utils';
import { BaseOptions, Container, Logger } from 'noicejs';
import yargs from 'yargs-parser';

Expand All @@ -10,6 +10,7 @@ import { BotModule } from './module/BotModule';
import { ServiceModule } from './module/ServiceModule';
import { Schema } from './schema';
import { ServiceEvent } from './Service';
import { removePid, writePid } from './utils/PidFile';
import { VERSION_INFO } from './version';

export interface CreateOptions {
Expand Down
43 changes: 43 additions & 0 deletions src/utils/PidFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { doesExist, Optional } from '@apextoaster/js-utils';
import { open, unlink, write } from 'fs';
import { pid } from 'process';

type OptionalErrno = Optional<NodeJS.ErrnoException>;

/**
* Write the current process ID to a file at the given `path`.
*
* @public
*/
export async function writePid(path: string): Promise<void> {
return new Promise((res, rej) => {
open(path, 'wx', (openErr: OptionalErrno, fd: number) => {
if (doesExist(openErr)) {
rej(openErr);
} else {
write(fd, pid.toString(), 0, 'utf8', (writeErr: OptionalErrno) => {
if (doesExist(writeErr)) {
rej(writeErr);
} else {
res();
}
});
}
});
});
}

/**
* Remove the file at the given `path`.
*/
export async function removePid(path: string): Promise<void> {
return new Promise((res, rej) => {
unlink(path, (err: OptionalErrno) => {
if (doesExist(err)) {
rej(err);
} else {
res();
}
});
});
}

0 comments on commit 25d4b64

Please sign in to comment.