Skip to content

Commit

Permalink
fix(bin/attach, utils/guardian): identifies triggered exit via enviro…
Browse files Browse the repository at this point in the history
…nment variable to account for d
  • Loading branch information
rafamel committed May 13, 2019
1 parent 2282279 commit dc77e13
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/bin/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { attach as _attach, options, resolver, add } from 'exits';
import terminate from '~/utils/terminate-children';
import logger from '~/utils/logger';
import { wait, status } from 'promist';
import { KPO_EXIT_ENV } from '~/constants';
import EnvManager from '~/utils/env-manager';

export default function attach(): void {
_attach();
Expand All @@ -23,6 +25,8 @@ export default function attach(): void {
}
});
add(async () => {
new EnvManager(process.env).set(KPO_EXIT_ENV, 'triggered');

const term = status(terminate(process.pid, 'SIGTERM', 150));
await Promise.race([term, wait(3000)]);
if (term.status !== 'pending') return;
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export const CONCURRENTLY_PATH = require.resolve(
);
/* Shared between instances: changes might imply a major version release */
export const KPO_LOG_ENV = 'kpo_log';
export const KPO_EXIT_ENV = 'kpo_exit';
6 changes: 4 additions & 2 deletions src/utils/guardian.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { state } from 'exits';
import EnvManager from './env-manager';
import { KPO_EXIT_ENV } from '~/constants';

const manager = new EnvManager(process.env);
export default function guardian(): void {
if (state().triggered) throw Error(`Process is terminating`);
if (manager.get(KPO_EXIT_ENV)) throw Error(`Process is terminating`);
}

0 comments on commit dc77e13

Please sign in to comment.