Skip to content

Conversation

@fluiddot
Copy link
Contributor

@fluiddot fluiddot commented Jun 25, 2024

Fixes 7907-gh-Automattic/dotcom-forge.

Proposed Changes

This PR aims to solve two cases related to executing wp-cli commands at the same time a site is being deleted:

  • Problem: Try to mount a PHP instance on a deleted site.
    • Solution: Identify deleted sites and avoid running the command if the site is deleted.
    • Changes:
      • Add a list of deleted sites to determine when a site has been deleted. Note that this list won't persist across sessions.
      • Since the project path is not a unique value, we need to use site ID as the key for the deleted list when executing commands.
  • Problem: If a command is executed and the site is deleted, the file deletion makes wp-cli fail due to the inability to access site files.
    • Solution: Cancel wp-cli processes before deleting the site.
    • Changes:
      • Attach wp-cli process to sites. This way we can kill the process before deletion.
      • To simplify the process, we'll instantiate the process per site. This implies refactoring wp-cli process.

Testing Instructions

Preparation:

  • Apply the following patch to log wp-cli commands:
diff --git forkSrcPrefix/src/ipc-handlers.ts forkDstPrefix/src/ipc-handlers.ts
index 3a48313ca9401d63e2608fb2f22a254a1d5802cb..0401b67bb315d7287714b631e7c8683e7d0b0325 100644
--- forkSrcPrefix/src/ipc-handlers.ts
+++ forkDstPrefix/src/ipc-handlers.ts
@@ -573,6 +573,7 @@ export async function executeWPCLiInline(
 	{ siteId, args }: { siteId: string; args: string }
 ): Promise< WpCliResult > {
 	if ( SiteServer.isDeleted( siteId ) ) {
+		console.log( '==== skipping wp-cli command because site is deleted', siteId, args );
 		return { stdout: '', stderr: `can't execute command on deleted site ${ siteId }` };
 	}
 	const server = SiteServer.get( siteId );
diff --git forkSrcPrefix/src/site-server.ts forkDstPrefix/src/site-server.ts
index 2c82b755e1550e455fd5cb628361352bfe8975ee..f94d193f3050e34c5c5c8612e242c182f9622ace 100644
--- forkSrcPrefix/src/site-server.ts
+++ forkDstPrefix/src/site-server.ts
@@ -179,9 +179,13 @@ export class SiteServer {
 		}
 
 		try {
-			return await this.wpCliExecutor.execute( wpCliArgs as string[] );
+			console.log( '==== executing wp-cli command', args );
+			const response = await this.wpCliExecutor.execute( wpCliArgs as string[] );
+			console.log( '==== response wp-cli command', response );
+			return response;
 		} catch ( error ) {
 			if ( ( error as MessageCanceled )?.canceled ) {
+				console.log( '==== wp-cli command canceled', args );
 				return { stdout: '', stderr: 'wp-cli command canceled' };
 			}
 

Chat context fetches plugins and themes

  • Open the app.
  • Observe that wp-cli command plugin list is executed successfully for the default selected site.
  • Observe that wp-cli command theme list is executed successfully for the default selected site.
  • Add a site or start a site.
  • Switch to another app and focus on the Studio again to make the app gain focus.
  • Observe that wp-cli command plugin list is executed successfully for the default selected site.
  • Observe that wp-cli command theme list is executed successfully for the default selected site.

Delete running site doesn't crash

  • Open the app.
  • Add a site or start a site.
  • Navigate to the Settings tab.
  • Click on Delete a site.
  • Check Delete site files from my computer.
  • Click Delete site.
  • Observe the site is deleted and no crash happens.
  • Observe that wp-cli command plugin list is invoked but canceled.
  • Observe that wp-cli command theme list is skipped because the site is deleted.

Check open processes

  • Open the Activity Monitor app on macOS (or an application to list currently running processes).
  • Filter by the text electron.
  • Open the app.
  • Observe that there are only two processes with the name Electron Helper.
  • Add a site or start a site.
  • Observe two new Electron Helper processes are created. One is the site server and the other one is wp-cli executor.
  • Stop the site.
  • Observe one of the Electron Helper processes is removed.
  • Delete the site.
  • Observe one of the Electron Helper processes is removed.

Pre-merge Checklist

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

@fluiddot fluiddot self-assigned this Jun 25, 2024
@fluiddot fluiddot marked this pull request as ready for review June 25, 2024 14:19
@fluiddot fluiddot requested a review from a team June 25, 2024 14:19
Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

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

I tested it and the site context is still working while it doesn't crash when deleting sites.
I also tested executing wp-cli from the terminal and it worked as expected.

Screenshot 2024-06-26 at 17 48 10

Here is the output of canceled and skipped wp-cli commands for a deleted site.

directory: /Users/macbookpro/Studio/dotcom-local-site
mode: wordpress
php: 8.1
wp: latest
WordPress latest folder already exists. Skipping download.
SQLite folder already exists. Skipping download.
Server running at http://localhost:8881
Saved user data to /Users/macbookpro/Library/Application Support/Studio/appdata-v1.json
==== executing wp-cli command theme list --format=json
==== executing wp-cli command plugin list --format=json --status=active
==== response wp-cli command { stdout: '[]', stderr: '' }
==== response wp-cli command {
  stdout: '[{"name":"twentytwentyfour","status":"active","update":"none","version":"1.1","update_version":"","auto_update":"off"},{"name":"twentytwentythree","status":"inactive","update":"none","version":"1.4","update_version":"","auto_update":"off"},{"name":"twentytwentytwo","status":"inactive","update":"none","version":"1.7","update_version":"","auto_update":"off"}]',
  stderr: ''
}
==== executing wp-cli command plugin list --format=json --status=active
==== response wp-cli command { stdout: '[]', stderr: '' }
==== executing wp-cli command theme list --format=json
==== response wp-cli command {
  stdout: '[{"name":"twentytwentyfour","status":"active","update":"none","version":"1.1","update_version":"","auto_update":"off"},{"name":"twentytwentythree","status":"inactive","update":"none","version":"1.4","update_version":"","auto_update":"off"},{"name":"twentytwentytwo","status":"inactive","update":"none","version":"1.7","update_version":"","auto_update":"off"}]',
  stderr: ''
}
==== executing wp-cli command plugin list --format=json --status=active
Deleting site 668a9fe8-875f-46db-9306-71cb8f74377a
Loaded user data from /Users/macbookpro/Library/Application Support/Studio/appdata-v1.json
Stopping server with ID 668a9fe8-875f-46db-9306-71cb8f74377a
Server stopped
==== wp-cli command canceled plugin list --format=json --status=active
==== skipping wp-cli command because site is deleted 668a9fe8-875f-46db-9306-71cb8f74377a theme list --format=json
Saved user data to /Users/macbookpro/Library/Application Support/Studio/appdata-v1.json
Killed processes using port 8881

Screenshots when running sites:

Screenshot 2024-06-27 at 17 34 30

Screenshot without sites:

Screenshot 2024-06-27 at 17 35 24

Comment on lines -85 to 86

const messageId = +this.lastMessageId;
const messageId = this.lastMessageId++;
process.postMessage( { message, messageId, data } );
Copy link
Member

Choose a reason for hiding this comment

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

Good catch.

if ( ! server ) {
throw new Error( 'Site not found.' );
}
return server.executeWpCliCommand( args );
Copy link
Member

Choose a reason for hiding this comment

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

I like this way of executing wp-cli commands

Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

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

Fantastic work! Very elegant solution. 👏🏻 Thank you for addressing this.

The plan succeeded for me.

My suggestions are for my own education and small preferences. They should not block this PR. 🚀

@fluiddot fluiddot force-pushed the fix/crash-delete-site branch from 817ea71 to 3b85c67 Compare June 27, 2024 16:24
@fluiddot
Copy link
Contributor Author

Heads up that I had to rebase this branch after an issue when merging with trunk.

@fluiddot fluiddot merged commit 16052c9 into trunk Jun 27, 2024
@fluiddot fluiddot deleted the fix/crash-delete-site branch June 27, 2024 17:00
@dcalhoun dcalhoun mentioned this pull request Jun 27, 2024
1 task
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.

4 participants