-
Notifications
You must be signed in to change notification settings - Fork 74
Handling process exit #1249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling process exit #1249
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,7 +64,7 @@ export class NewBrowser extends Browser { | |
| constructor(config: Config, opts: BrowserOpts) { | ||
| super(config, opts); | ||
|
|
||
| signalHandler.on("exit", () => this.quit()); | ||
| signalHandler.on("exit", (err?: Error) => this.quit(err)); | ||
| } | ||
|
|
||
| async init(): Promise<NewBrowser> { | ||
|
|
@@ -81,7 +81,9 @@ export class NewBrowser extends Browser { | |
| return Promise.resolve(); | ||
| } | ||
|
|
||
| async quit(): Promise<void> { | ||
| async quit(err?: Error): Promise<void> { | ||
| this._exitError = err; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. save in browser for using in test fail |
||
|
|
||
| try { | ||
| this.setHttpTimeout(this._config.sessionQuitTimeout); | ||
| await this._session!.deleteSession(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,8 @@ export class MainRunner extends RunnableEmitter { | |
| this.workersRegistry.init(); | ||
| this.workers = this.registerWorkers(require.resolve("../worker"), ["runTest", "cancel"] as const) as Workers; | ||
| this.browserPool = pool.create(this.config, this); | ||
|
|
||
| eventsUtils.passthroughEvent(this, this.workersRegistry, MasterEvents.EXIT); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass event to childs |
||
| } | ||
|
|
||
| _isRunning(): boolean { | ||
|
|
||
This file was deleted.
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactoring this to typescript |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { AsyncEmitter } from "./events"; | ||
| import { log } from "./utils/logger"; | ||
| import { MasterEvents } from "./events"; | ||
|
|
||
| const signalHandler = new AsyncEmitter(); | ||
|
|
||
| signalHandler.setMaxListeners(0); | ||
|
|
||
| let callCount = 0; | ||
|
|
||
| let lastCallTime = 0; | ||
|
|
||
| const throttleTime = 10; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throttle for SIGINT, by some reason if kill using Control + C event fired twice with delay 7-8ms, probably it is duplicates from webdriver.io |
||
|
|
||
| function notifyAndExit(signalNo: number): (signal: NodeJS.Signals) => void { | ||
| const exitCode = 128 + signalNo; | ||
|
|
||
| return function (signal: NodeJS.Signals) { | ||
| const time = Date.now(); | ||
|
|
||
| if (time - lastCallTime < throttleTime) { | ||
| lastCallTime = time; | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| lastCallTime = time; | ||
|
|
||
| if (callCount++ > 0) { | ||
| log("Force quit."); | ||
| process.exit(exitCode); | ||
| } | ||
|
|
||
| const err = new Error(`The process was terminated by a signal: ${signal}`); | ||
|
|
||
| signalHandler.emitAndWait(MasterEvents.EXIT, err).then(() => { | ||
| signalHandler.emitAndWait(MasterEvents.RUNNER_END, err).then(() => { | ||
| process.exit(exitCode); | ||
| }); | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| process.on("SIGHUP", notifyAndExit(1)); | ||
| process.on("SIGINT", notifyAndExit(2)); | ||
| process.on("SIGTERM", notifyAndExit(15)); | ||
|
|
||
| export default signalHandler; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,6 +182,8 @@ export class Testplane extends BaseTestplane { | |
| eventsUtils.passthroughEvent(this.runner, this, _.values(MasterSyncEvents)); | ||
| eventsUtils.passthroughEventAsync(this.runner, this, _.values(MasterAsyncEvents)); | ||
| eventsUtils.passthroughEventAsync(signalHandler, this, MasterEvents.EXIT); | ||
| eventsUtils.passthroughEventAsync(signalHandler, this, MasterEvents.RUNNER_END); | ||
| eventsUtils.passthroughEventAsync(signalHandler, this.runner, MasterEvents.EXIT); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pass exit event to runner |
||
|
|
||
| await this._startServersIfNeeded(); | ||
| await this._emitInitEventOnce(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,6 +156,8 @@ module.exports = class WorkersRegistry extends EventEmitter { | |
| logger.error(`testplane:worker:${child.pid} terminated unexpectedly with ${errMsg}`); | ||
| }); | ||
|
|
||
| this.once(MasterEvents.EXIT, () => child.kill()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kill child process if get EXIT event from master (through runner) |
||
|
|
||
| child.on("message", (data = {}) => { | ||
| switch (data.event) { | ||
| case WORKER_INIT: | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor to typescript |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass error from exit event