Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 773f21a

Browse files
committed
Tidy ups from code review, add section on Detached mode to README.
1 parent 2dec992 commit 773f21a

File tree

4 files changed

+47
-20
lines changed

4 files changed

+47
-20
lines changed

src/packages/cli/src/args.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TruffleColors } from "@ganache/colors";
2-
32
import yargs, { Options } from "yargs";
43
import {
54
DefaultFlavor,
@@ -233,7 +232,7 @@ export default function (
233232
description: highlight(
234233
"Run Ganache in detached (daemon) mode." +
235234
EOL +
236-
"See `ganache instances --help` for information omn managing detached instances."
235+
"See `ganache instances --help` for information on managing detached instances."
237236
),
238237
type: "boolean",
239238
alias: ["D", "😈"]

src/packages/cli/src/cli.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import initializeFilecoin from "./initialize/filecoin";
99
import type { FilecoinProvider } from "@ganache/filecoin";
1010
import type { EthereumProvider } from "@ganache/ethereum";
1111
import {
12-
removeDetachedInstanceFile,
1312
notifyDetachedInstanceReady,
1413
stopDetachedInstance,
1514
startDetachedInstance,
@@ -19,18 +18,6 @@ import { TruffleColors } from "@ganache/colors";
1918
import { table } from "table";
2019
import chalk from "chalk";
2120

22-
// if process.send is defined, this is a child_process (we assume a detached
23-
// instance), so we need to notify that we are ready.
24-
const isDetachedInstance = process.send !== undefined;
25-
26-
if (isDetachedInstance) {
27-
// we want to attach this listener as early as possible, to avoid leaving a
28-
// dangling instance file
29-
process.on("exit", () => {
30-
removeDetachedInstanceFile(process.pid);
31-
});
32-
}
33-
3421
const logAndForceExit = (messages: any[], exitCode = 0) => {
3522
// https://nodejs.org/api/process.html#process_process_exit_code
3623
// writes to process.stdout in Node.js are sometimes asynchronous and may occur over
@@ -176,6 +163,9 @@ if (argv.action === "start") {
176163
}
177164
}
178165

166+
// if process.send is defined, this is a child_process (we assume a detached
167+
// instance), so we need to notify that we are ready.
168+
const isDetachedInstance = process.send !== undefined;
179169
if (isDetachedInstance) {
180170
notifyDetachedInstanceReady();
181171
}
@@ -223,7 +213,6 @@ if (argv.action === "start") {
223213
chalk.bold("Uptime")
224214
]
225215
];
226-
227216
for (let i = 0; i < instances.length; i++) {
228217
const instance = instances[i];
229218

src/packages/cli/src/detach.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ export async function stopDetachedInstance(
7474
process.kill(instance.pid, "SIGTERM");
7575
} catch (err) {
7676
// process.kill throws if the process was not found (or was a group process in Windows)
77-
removeDetachedInstanceFile(instance.pid);
7877
return false;
78+
} finally {
79+
removeDetachedInstanceFile(instance.pid);
7980
}
8081
return true;
8182
}
@@ -166,17 +167,17 @@ export async function startDetachedInstance(
166167
/**
167168
* Fetch all instance of Ganache running in detached mode. Cleans up any
168169
* instance files for processes that are no longer running.
169-
* @returns Promise<DetachedInstance[]> resolves with an array of instances
170+
* @returns {Promise<DetachedInstance[]>} resolves with an array of instances
170171
*/
171172
export async function getDetachedInstances(): Promise<DetachedInstance[]> {
172173
const files = readdirSync(dataPath);
173174
const instances: DetachedInstance[] = [];
175+
const processes = await psList();
174176

175177
for (let i = 0; i < files.length; i++) {
176178
const filename = files[i];
177179
const pid = parseInt(filename);
178180

179-
const processes = await psList();
180181
const foundProcess = processes.find(p => p.pid === pid);
181182

182183
let shouldRemoveFile = false;
@@ -206,6 +207,8 @@ export async function getDetachedInstances(): Promise<DetachedInstance[]> {
206207
if (shouldRemoveFile) removeDetachedInstanceFile(pid);
207208
}
208209

210+
instances.sort((a, b) => b.startTime - a.startTime);
211+
209212
return instances;
210213
}
211214

@@ -222,7 +225,7 @@ async function findDetachedInstanceByName(
222225
}
223226

224227
/**
225-
* Flattens parsed, and namespaced args into an array of arguments to be passed
228+
* Flattens parsed and namespaced args into an array of arguments to be passed
226229
* to a child process. This handles "special" arguments, such as "action",
227230
* "flavor" and "--detach".
228231
* @param {object} args to be flattened

src/packages/ganache/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,43 @@ Server:
470470
-p, --server.port, --port Port to listen on.
471471
deprecated aliases: --port
472472
[number] [default: 8545]
473+
```
474+
475+
### Detached Instances
476+
477+
Ganache can be started as a background instance via the CLI by providing the following argument (along with any combination
478+
of the Ganache startup arguments above):
479+
480+
```
481+
-D, --detach, --😈 Run Ganache in detached (daemon) mode. [boolean]
482+
```
483+
484+
This will start Ganache as a background process, and return to the console as soon as Ganache has started and ready to
485+
receive requests. A friendly name will be returned to STDOUT which can then be used to interact with the instance via
486+
the `ganache instances` command with the following arguments:
487+
488+
```
489+
Commands:
490+
cli.ts instances list List instances running in detached mode
491+
cli.ts instances stop <name> Stop the instance specified by <name>
492+
```
493+
494+
E.g., start Ganache on port 8544, with a block time of 10 seconds, and then stop the instance.
495+
496+
```
497+
$ ganache --port=8544 --miner.blockTime=10 --detach
498+
salted_caramel_ganache
499+
500+
$ ganache instances list
501+
╔═══════╤════════════════════════╤══════════╤═════════╤═══════════╤══════╤════════════╗
502+
║ PID │ Name │ Flavor │ Version │ Host │ Port │ Uptime ║
503+
╟───────┼────────────────────────┼──────────┼─────────┼───────────┼──────┼────────────╢
504+
║ 56004 │ salted_caramel_ganache │ ethereum │ 7.4.3 │ 127.0.0.1 │ 8544 │ 48 seconds ║
505+
╚═══════╧════════════════════════╧══════════╧═════════╧═══════════╧══════╧════════════╝
506+
473507
508+
$ ganache instances stop salted_caramel_ganache
509+
Process stopped
474510
```
475511

476512
### Ganache Provider Events

0 commit comments

Comments
 (0)