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

Commit

Permalink
Tidy ups from code review, add section on Detached mode to README.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffsmale90 committed Sep 27, 2022
1 parent 2dec992 commit 773f21a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
3 changes: 1 addition & 2 deletions src/packages/cli/src/args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TruffleColors } from "@ganache/colors";

import yargs, { Options } from "yargs";
import {
DefaultFlavor,
Expand Down Expand Up @@ -233,7 +232,7 @@ export default function (
description: highlight(
"Run Ganache in detached (daemon) mode." +
EOL +
"See `ganache instances --help` for information omn managing detached instances."
"See `ganache instances --help` for information on managing detached instances."
),
type: "boolean",
alias: ["D", "😈"]
Expand Down
17 changes: 3 additions & 14 deletions src/packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import initializeFilecoin from "./initialize/filecoin";
import type { FilecoinProvider } from "@ganache/filecoin";
import type { EthereumProvider } from "@ganache/ethereum";
import {
removeDetachedInstanceFile,
notifyDetachedInstanceReady,
stopDetachedInstance,
startDetachedInstance,
Expand All @@ -19,18 +18,6 @@ import { TruffleColors } from "@ganache/colors";
import { table } from "table";
import chalk from "chalk";

// if process.send is defined, this is a child_process (we assume a detached
// instance), so we need to notify that we are ready.
const isDetachedInstance = process.send !== undefined;

if (isDetachedInstance) {
// we want to attach this listener as early as possible, to avoid leaving a
// dangling instance file
process.on("exit", () => {
removeDetachedInstanceFile(process.pid);
});
}

const logAndForceExit = (messages: any[], exitCode = 0) => {
// https://nodejs.org/api/process.html#process_process_exit_code
// writes to process.stdout in Node.js are sometimes asynchronous and may occur over
Expand Down Expand Up @@ -176,6 +163,9 @@ if (argv.action === "start") {
}
}

// if process.send is defined, this is a child_process (we assume a detached
// instance), so we need to notify that we are ready.
const isDetachedInstance = process.send !== undefined;
if (isDetachedInstance) {
notifyDetachedInstanceReady();
}
Expand Down Expand Up @@ -223,7 +213,6 @@ if (argv.action === "start") {
chalk.bold("Uptime")
]
];

for (let i = 0; i < instances.length; i++) {
const instance = instances[i];

Expand Down
11 changes: 7 additions & 4 deletions src/packages/cli/src/detach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ export async function stopDetachedInstance(
process.kill(instance.pid, "SIGTERM");
} catch (err) {
// process.kill throws if the process was not found (or was a group process in Windows)
removeDetachedInstanceFile(instance.pid);
return false;
} finally {
removeDetachedInstanceFile(instance.pid);
}
return true;
}
Expand Down Expand Up @@ -166,17 +167,17 @@ export async function startDetachedInstance(
/**
* Fetch all instance of Ganache running in detached mode. Cleans up any
* instance files for processes that are no longer running.
* @returns Promise<DetachedInstance[]> resolves with an array of instances
* @returns {Promise<DetachedInstance[]>} resolves with an array of instances
*/
export async function getDetachedInstances(): Promise<DetachedInstance[]> {
const files = readdirSync(dataPath);
const instances: DetachedInstance[] = [];
const processes = await psList();

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

const processes = await psList();
const foundProcess = processes.find(p => p.pid === pid);

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

instances.sort((a, b) => b.startTime - a.startTime);

return instances;
}

Expand All @@ -222,7 +225,7 @@ async function findDetachedInstanceByName(
}

/**
* Flattens parsed, and namespaced args into an array of arguments to be passed
* Flattens parsed and namespaced args into an array of arguments to be passed
* to a child process. This handles "special" arguments, such as "action",
* "flavor" and "--detach".
* @param {object} args to be flattened
Expand Down
36 changes: 36 additions & 0 deletions src/packages/ganache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,43 @@ Server:
-p, --server.port, --port Port to listen on.
deprecated aliases: --port
[number] [default: 8545]
```

### Detached Instances

Ganache can be started as a background instance via the CLI by providing the following argument (along with any combination
of the Ganache startup arguments above):

```
-D, --detach, --😈 Run Ganache in detached (daemon) mode. [boolean]
```

This will start Ganache as a background process, and return to the console as soon as Ganache has started and ready to
receive requests. A friendly name will be returned to STDOUT which can then be used to interact with the instance via
the `ganache instances` command with the following arguments:

```
Commands:
cli.ts instances list List instances running in detached mode
cli.ts instances stop <name> Stop the instance specified by <name>
```

E.g., start Ganache on port 8544, with a block time of 10 seconds, and then stop the instance.

```
$ ganache --port=8544 --miner.blockTime=10 --detach
salted_caramel_ganache
$ ganache instances list
╔═══════╤════════════════════════╤══════════╤═════════╤═══════════╤══════╤════════════╗
║ PID │ Name │ Flavor │ Version │ Host │ Port │ Uptime ║
╟───────┼────────────────────────┼──────────┼─────────┼───────────┼──────┼────────────╢
║ 56004 │ salted_caramel_ganache │ ethereum │ 7.4.3 │ 127.0.0.1 │ 8544 │ 48 seconds ║
╚═══════╧════════════════════════╧══════════╧═════════╧═══════════╧══════╧════════════╝
$ ganache instances stop salted_caramel_ganache
Process stopped
```

### Ganache Provider Events
Expand Down

0 comments on commit 773f21a

Please sign in to comment.