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

feat: add a detach mode to the CLI #3568

Merged
merged 71 commits into from
Dec 1, 2022
Merged

feat: add a detach mode to the CLI #3568

merged 71 commits into from
Dec 1, 2022

Conversation

jeffsmale90
Copy link
Contributor

@jeffsmale90 jeffsmale90 commented Aug 22, 2022

Do you need to detach? Do you need to do it now?

It can be annoying needing an extra terminal open just to run Ganache. This becomes super annoying when you want to have different instances and flavors of Ganache running at the same time.

Enter: Detach mode.

Start Ganache with the --detach flag, and it'll return to the console as soon as Ganache is ready to receive requests (writing the name of the instance to stdout).

ganache instances list will show you all of the instances of Ganache running in Detach mode, and you can stop them with ganache instances stop <name>.

┌───────┬─────────────────────────┬──────────┬─────────┬────────────────┬────────┐
│   PID │ Name                    │ Flavor   │ Version │ Host           │ Uptime │
├───────┼─────────────────────────┼──────────┼─────────┼────────────────┼────────┤
│ 12182 │ salted_caramel_ganache  │ ethereum │ 7.6.0   │ 127.0.0.1:8545 │ 2m 46s │
├───────┼─────────────────────────┼──────────┼─────────┼────────────────┼────────┤
│ 53184 │ candied_caramel_truffle │ ethereum │ 7.6.0   │ 127.0.0.1:8545 │ 2m 56s │
└───────┴─────────────────────────┴──────────┴─────────┴────────────────┴────────┘

With the following command, you can start Ganache, run your tests, and stop Ganache when you are finished.

GANACHE=$(ganache --detach) && npm run test; ganache instances stop $GANACHE

Or if you are running PowerShell on Windows, you can do:

$GANACHE=ganache --detach; npm run test; ganache instances stop $GANACHE

Fixes: #1001

@jeffsmale90 jeffsmale90 changed the title feat: allow users to specify a option, which causes Ganache to start up as a background process feat: allow users to start Ganache as a background process, by providing a --detach flag Aug 22, 2022
@jeffsmale90
Copy link
Contributor Author

jeffsmale90 commented Aug 24, 2022

I looked into different ways to test this, and unfortunately came up empty.

I think it would be reasonable to test the cli via child_process.fork to execute the cli with given args. We can hang onto the ChildProcess instance, and listen to the close event after calling kill.

Unfortunately this becomes difficult with --detach. We can listen to the PID of the grandchild process via stdout, but we don't have a reasonable way to ensure that it shuts down cleanly. We would call kill(), wait an arbitrary length of time, cross our fingers and toes, and hope that it succeeds.

There's some workarounds I've found in unmaintained projects, where they spawn the ps command to check whether the process exists, and poll to wait for it to not exist, but I'm not comfortable with that approach, given the inconsistency across different platforms.

Keen to hear people's thoughts.

… (but don't show that in the help, because it's ugly)
@jeffsmale90
Copy link
Contributor Author

I've manually validated this on Mac, Ubuntu, and Windows 🎉

@jeffsmale90 jeffsmale90 marked this pull request as ready for review August 25, 2022 07:51
@tenthirtyone
Copy link
Contributor

IIRC a .service file lets systemd run Node apps as a background process that lives inside it's own session and you can control it with systemctl.

I have no idea how that would translate to other operating systems or how npm would install a .service file into /var or /etc/systemd without some sort of hacky script and root permission.

If I understand, there is no way to stop the background process without killing the PID directly. It would be awesome if the system-level tools could manage that - it would avoid the polling.

Forever-js used to be the way to keep node going in the background. I believe they do something similar but start a worker that runs in the background, listens to a socket for communication from the cli and stores the PID of the child process somewhere. So you are in good company there.

@davidmurdoch
Copy link
Member

Is it typical for a detach mode to return the PID and only the PID? What other tools operate this way?
If that is typical behavior, I think i'm 👌 with shipping this without additional measures to track the instance. I'm sure we'll get users asking how to kill or get the process back, so maybe we'll figure out the best way of implementing some sort of re-connection API or instance-management from user feedback.

Tracking the PIDs ✨somewhere✨, as @tenthirtyone suggested, sounds reasonable for a follow up PR. IN the future we could have detached child open a socket (don't know if there is a way to reconnect to the original IPC socket) to allow for new connections to communicate with the process, then we could do things like ganache --attach <PID>.

Oh, one question. Should We pipe stdout somewhere else by default in this initial release, or should we let just let it get directed into the blackhole (/dev/null) until a user asks for a way to access the logs?

Last note: for the release notes description, can you also provide a Windows (either cmd.exe or Powershell) example, if there is reasonable equivalent?

…etach flags, rather append to the child args, cancelling out all preceeding flags.
…rom instances directory that aren't valid json files. Rename 'poppy-seed' to 'poppyseed' because hyphens in the instance name are a pain!
@davidmurdoch davidmurdoch added the doc-change-required Issue indicates a change to documentation is required label Nov 25, 2022
Copy link
Member

@cds-amal cds-amal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by feedback. I'm excited to get this in my hands. OR, get my hands on this. wheeeeee

@jeffsmale90
Copy link
Contributor Author

Documentation added here: trufflesuite/trufflesuite.com#1367

@jeffsmale90 jeffsmale90 removed the doc-change-required Issue indicates a change to documentation is required label Nov 30, 2022
Copy link
Member

@davidmurdoch davidmurdoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHIP IT!

@jeffsmale90 jeffsmale90 merged commit ccf63bd into develop Dec 1, 2022
@jeffsmale90 jeffsmale90 deleted the feat/detach-mode branch December 1, 2022 19:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add flag for starting ganache in detached mode
6 participants