This repository has been archived by the owner on Sep 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #669 from gemini-testing/correct-handling-of-proce…
…ss-events Correct exit on 'SIGHUP', 'SIGINT' or 'SIGTERM'
- Loading branch information
Showing
25 changed files
with
527 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,53 @@ | ||
'use strict'; | ||
var util = require('util'), | ||
Browser = require('../browser'), | ||
Pool = require('./pool'), | ||
signalHandler = require('../signal-handler'), | ||
_ = require('lodash'), | ||
Promise = require('bluebird'); | ||
|
||
var activeSessions = {}; | ||
|
||
/** | ||
* @constructor | ||
* @extends BasicPool | ||
* @param {Config} config | ||
* @param {Calibrator} calibrator | ||
*/ | ||
function BasicPool(config, calibrator) { | ||
this._config = config; | ||
this._calibrator = calibrator; | ||
} | ||
|
||
util.inherits(BasicPool, Pool); | ||
|
||
Pool.prototype.getBrowser = function(id) { | ||
var _this = this, | ||
browser = Browser.create(this._config.forBrowser(id)), | ||
launchPromise = browser.launch(this._calibrator); | ||
|
||
activeSessions[browser.id] = { | ||
browser: browser, | ||
launchPromise: launchPromise | ||
}; | ||
|
||
return launchPromise | ||
.then(browser.reset.bind(browser)).thenReturn(browser) | ||
.catch(function(e) { | ||
return _this.freeBrowser(browser) | ||
.then(function() { | ||
return Promise.reject(e); | ||
}); | ||
}); | ||
}; | ||
|
||
Pool.prototype.freeBrowser = function(browser) { | ||
delete activeSessions[browser.id]; | ||
return browser.quit(); | ||
const Promise = require('bluebird'); | ||
const _ = require('lodash'); | ||
const Browser = require('../browser'); | ||
const CancelledError = require('../errors/cancelled-error'); | ||
const Pool = require('./pool'); | ||
|
||
module.exports = class BasicPool extends Pool { | ||
/** | ||
* @constructor | ||
* @extends Pool | ||
* @param {Config} config | ||
* @param {Calibrator} calibrator | ||
*/ | ||
constructor(config, calibrator) { | ||
super(); | ||
|
||
this._config = config; | ||
this._calibrator = calibrator; | ||
|
||
this._activeSessions = {}; | ||
} | ||
|
||
getBrowser(id) { | ||
const browser = Browser.create(this._config.forBrowser(id)); | ||
|
||
return browser.launch(this._calibrator) | ||
.then(() => { | ||
if (this._cancelled) { | ||
return Promise.reject(new CancelledError()); | ||
} | ||
|
||
this._activeSessions[browser.sessionId] = browser; | ||
}) | ||
.then(() => browser.reset()) | ||
.thenReturn(browser) | ||
.catch((e) => this.freeBrowser(browser).then(() => Promise.reject(e))); | ||
} | ||
|
||
freeBrowser(browser) { | ||
delete this._activeSessions[browser.sessionId]; | ||
return browser.quit(); | ||
} | ||
|
||
cancel() { | ||
this._cancelled = true; | ||
|
||
_.forEach(this._activeSessions, (browser) => browser.quit()); | ||
|
||
this._activeSessions = {}; | ||
} | ||
}; | ||
|
||
signalHandler.on('exit', function() { | ||
console.log('Killing browsers...'); | ||
return _(activeSessions) | ||
.map(function(session) { | ||
var quit_ = session.browser.quit.bind(session.browser); | ||
return session.launchPromise.then(quit_); | ||
}) | ||
.thru(Promise.all) | ||
.value(); | ||
}); | ||
|
||
module.exports = BasicPool; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
'use strict'; | ||
|
||
var QEmitter = require('qemitter'), | ||
signalHandler = new QEmitter(); | ||
const EventEmitter = require('events').EventEmitter; | ||
const Events = require('./constants/events'); | ||
const logger = require('./utils').logger; | ||
|
||
module.exports = signalHandler; | ||
const signalHandler = module.exports = new EventEmitter(); | ||
|
||
process.on('SIGHUP', notifyAndExit(1)); | ||
process.on('SIGINT', notifyAndExit(2)); | ||
process.on('SIGTERM', notifyAndExit(15)); | ||
process.on('SIGHUP', handleSignal(1)); | ||
process.on('SIGINT', handleSignal(2)); | ||
process.on('SIGTERM', handleSignal(15)); | ||
|
||
var callCount = 0; | ||
let callCount = 0; | ||
|
||
function notifyAndExit(signalNo) { | ||
var exitCode = 128 + signalNo; | ||
function handleSignal(signalNo) { | ||
const exitCode = 128 + signalNo; | ||
|
||
return function() { | ||
return () => { | ||
if (callCount++ > 0) { | ||
console.log('Force quit.'); | ||
logger.warn('Force quit.'); | ||
process.exit(exitCode); | ||
} | ||
|
||
signalHandler.emitAndWait('exit') | ||
.then(function() { | ||
console.log('Done.'); | ||
process.exit(exitCode); | ||
}) | ||
.done(); | ||
logger.warn('Cancelling...'); | ||
signalHandler.emit(Events.INTERRUPT, {exitCode}); | ||
}; | ||
} |
Oops, something went wrong.