Skip to content

CLI: Send site events over socket instead of through events-relay pm2 process#2398

Merged
fredrikekelund merged 46 commits intotrunkfrom
f26d/cli-site-events-through-socket-suggestion
Jan 16, 2026
Merged

CLI: Send site events over socket instead of through events-relay pm2 process#2398
fredrikekelund merged 46 commits intotrunkfrom
f26d/cli-site-events-through-socket-suggestion

Conversation

@fredrikekelund
Copy link
Contributor

@fredrikekelund fredrikekelund commented Jan 15, 2026

Related issues

Proposed Changes

Use the pm2-axon library to send events over a socket/named pipe instead of using the events-relay child process. The benefit of this approach is that it doesn't break if users run studio site stop --all from their terminal while Studio is also running.

Tip

Now that we use a socket, we should start thinking about whether there's a future scenario where Studio can be responsible for binding a server on the socket, and events could be sent directly from CLI processes to Studio.

Testing Instructions

  1. npm start
  2. Create a site
  3. Ensure that it shows up in the sidebar
  4. Leave Studio running in the background and open a new terminal window
  5. Run node dist/cli/main.js site stop --all
  6. Ensure that all sites are marked as stopped in Studio
  7. Run node dist/cli/main.js site start --path PATH_TO_SITE where PATH_TO_SITE is the path to a Studio site
  8. Ensure that the site is marked as running in Studio

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@fredrikekelund fredrikekelund self-assigned this Jan 15, 2026
@fredrikekelund
Copy link
Contributor Author

fredrikekelund commented Jan 15, 2026

Status update: we've decided to follow this strategy, but we are currently trying to figure out why the E2E tests fail on macOS (on CI)

Base automatically changed from stu-1193-refactor-site-watchers to dev/studio-cli-i2 January 15, 2026 16:12
Base automatically changed from dev/studio-cli-i2 to trunk January 16, 2026 10:35
}
childProcess.kill();
const pid = childProcess.pid;
const result = childProcess.kill( 'SIGKILL' );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was battling failing E2E tests on macOS today and yesterday. The reason they were failing was that the first test suite (e2e/app.test.ts) runs very quickly. So quickly, in fact, that the events subscriber child process only ran for 25ms or so before stopCliEventsSubscriber was called.

The _events command wouldn't even have time to register SIGINT/SIGTERM event listeners in that time, which meant that the process would not be killed. I initially tried to counteract this by hardening the cleanup logic in the runCommand function, not running any async IO before registering the SIGINT/SIGTERM listeners, etc.

Even after I did all that, the tests still failed the same way. This led me to the conclusion that the SIGTERM signal (which is what ChildProcess::kill sends by default on Posix platforms in node) was sent before runCommand was even triggered, while some other async work was happening. It could, for instance, be the translation loading in cli/index.ts, or something that yargs does internally. I guess it could potentially even be an issue with multiple cores executing logic in parallel…

In any case, SIGKILL signals don't allow the child process to perform any cleanup; it's essentially a force kill operation. pm2-axon has logic that gracefully binds to a socket if the process that originally bound to it is no longer running, which is why it's not critical to let the _events child process run the cleanup.

The E2E tests are passing now, and I think this is a pragmatic solution to our problem 👍

@fredrikekelund fredrikekelund requested a review from a team January 16, 2026 11:18
@fredrikekelund fredrikekelund changed the title Send site events over socket CLI: Send site events over socket instead of through events-relay pm2 process Jan 16, 2026
Copy link
Contributor

@bcotrim bcotrim left a comment

Choose a reason for hiding this comment

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

Still reviewing the code but I noticed some deprecation warnings in the CLI now:

Image Image

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jan 16, 2026

📊 Performance Test Results

Comparing dbf3b88 vs trunk

site-editor

Metric trunk dbf3b88 Diff Change
load 2827.00 ms 2840.00 ms +13.00 ms 🔴 0.5%

site-startup

Metric trunk dbf3b88 Diff Change
siteCreation 7070.00 ms 7059.00 ms -11.00 ms 🟢 -0.2%
siteStartup 3945.00 ms 3942.00 ms -3.00 ms 🟢 -0.1%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change

@fredrikekelund
Copy link
Contributor Author

The performance tests are working, at least 🙂

Copy link
Contributor

@bcotrim bcotrim left a comment

Choose a reason for hiding this comment

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

Code LGTM 👍
Didn't find any regressions

@fredrikekelund
Copy link
Contributor Author

Thanks, @bcotrim. I just pushed a fix to mute the deprecation warning, too

@fredrikekelund fredrikekelund merged commit ca99991 into trunk Jan 16, 2026
7 of 9 checks passed
@fredrikekelund fredrikekelund deleted the f26d/cli-site-events-through-socket-suggestion branch January 16, 2026 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants