Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
feat: opt-in deasync server startup (#735)
Browse files Browse the repository at this point in the history
* Add deasync lib

* Update cli.js - deasync startup

* Seems to work on Windows okay now

* Make deasync an optional dep

* Remove try/catch for windows support

* Make `deasync` opt-in with cli flag

* Don't pass the new option to core

Co-authored-by: David Murdoch <david@davidmurdoch.com>
  • Loading branch information
nicholasjpaterno and davidmurdoch authored May 6, 2020
1 parent 8dd721f commit efc33f2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ Both `.provider()` and `.server()` take a single object which allows you to spec
* `"gasLimit"`: `String::hex | number` Sets the block gas limit. Must be specified as a `hex` string or `number`. Defaults to `"0x6691b7"`.
* `"callGasLimit"`: `number` Sets the transaction gas limit for `eth_call` and `eth_estimateGas` calls. Must be specified as a `hex` string. Defaults to `"0x1fffffffffffff"` (`Number.MAX_SAFE_INTEGER`).
* `"keepAliveTimeout"`: `number` If using `.server()` - Sets the HTTP server's `keepAliveTimeout` in milliseconds. See the [NodeJS HTTP docs](https://nodejs.org/api/http.html#http_server_keepalivetimeout) for details. `5000` by default.
* `"deasync"`: `boolean` Synchronizes ganache server startup. Useful in certain scenarios - see ganache-cli issue [#733](https://github.com/trufflesuite/ganache-cli/issues/733).

## Implemented Methods

Expand Down
6 changes: 6 additions & 0 deletions args.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ module.exports = exports = function(yargs, version, isDocker) {
type: 'boolean',
default: false
})
.option('deasync', {
group: 'Other::',
describe: 'Synchronize ganache server startup. Useful in certain scenarios (see ganache-cli issue #733).',
type: 'boolean',
default: false
})
.showHelpOnFail(false, 'Specify -? or --help for available options')
.help('help')
.alias('help', '?')
Expand Down
93 changes: 56 additions & 37 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ var detailedVersion = "Ganache CLI v" + pkg.version + " (ganache-core: " + ganac

var isDocker = "DOCKER" in process.env && process.env.DOCKER.toLowerCase() === "true";
var argv = initArgs(yargs, detailedVersion, isDocker).argv;
var deasync;
try {
deasync = argv.deasync ? require("deasync") : false;
} catch(e) {
deasync = false;
}

function parseAccounts(accounts) {
function splitAccount(account) {
Expand Down Expand Up @@ -106,12 +112,54 @@ var server = ganache.server(options);

console.log(detailedVersion);

server.listen(options.port, options.hostname, function(err, result) {
let started = false;
process.on("uncaughtException", function(e) {
if (started) {
console.log(e);
} else {
console.log(e.stack);
}
process.exit(1);
})

// See http://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js
if (process.platform === "win32") {
require("readline").createInterface({
input: process.stdin,
output: process.stdout
})
.on("SIGINT", function () {
process.emit("SIGINT");
});
}

const closeHandler = function () {
// graceful shutdown
server.close(function(err) {
if (err) {
// https://nodejs.org/api/process.html#process_process_exit_code
// writes to process.stdout in Node.js are sometimes asynchronous and may occur over
// multiple ticks of the Node.js event loop. Calling process.exit(), however, forces
// the process to exit before those additional writes to stdout can be performed.
if(process.stdout._handle) process.stdout._handle.setBlocking(true);
console.log(err.stack || err);
process.exit();
} else {
process.exit(0);
}
});
}

process.on("SIGINT", closeHandler);
process.on("SIGTERM", closeHandler);
process.on("SIGHUP", closeHandler);

function startGanache(err, result) {
if (err) {
console.log(err);
return;
}

started = true;
var state = result ? result : server.provider.manager.state;

console.log("");
Expand Down Expand Up @@ -190,41 +238,12 @@ server.listen(options.port, options.hostname, function(err, result) {

console.log("");
console.log("Listening on " + options.hostname + ":" + options.port);
});

process.on('uncaughtException', function(e) {
console.log(e.stack);
process.exit(1);
})

// See http://stackoverflow.com/questions/10021373/what-is-the-windows-equivalent-of-process-onsigint-in-node-js
if (process.platform === "win32") {
require("readline").createInterface({
input: process.stdin,
output: process.stdout
})
.on("SIGINT", function () {
process.emit("SIGINT");
});
}

const closeHandler = function () {
// graceful shutdown
server.close(function(err) {
if (err) {
// https://nodejs.org/api/process.html#process_process_exit_code
// writes to process.stdout in Node.js are sometimes asynchronous and may occur over
// multiple ticks of the Node.js event loop. Calling process.exit(), however, forces
// the process to exit before those additional writes to stdout can be performed.
if(process.stdout._handle) process.stdout._handle.setBlocking(true);
console.log(err.stack || err);
process.exit();
} else {
process.exit(0);
}
});
if (deasync) {
const listen = deasync(server.listen);
const result = listen(options.port, options.hostname);
startGanache(null, result);
} else {
server.listen(options.port, options.hostname, startGanache);
}

process.on("SIGINT", closeHandler);
process.on("SIGTERM", closeHandler);
process.on("SIGHUP", closeHandler);
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"webpack": "^4.18.0",
"webpack-cli": "3.1.0"
},
"optionalDependencies": {
"deasync": "0.1.20"
},
"repository": {
"type": "git",
"url": "https://github.com/trufflesuite/ganache-cli"
Expand Down

0 comments on commit efc33f2

Please sign in to comment.