-
Notifications
You must be signed in to change notification settings - Fork 57
CLI: Refine site stop --all implementation
#2374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fredrikekelund
merged 5 commits into
dev/studio-cli-i2
from
f26d/site-stop-all-refactor-refinements
Jan 12, 2026
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1a6ffe
Moved logic inside stop command and added flag
nightnei baa017d
Simplify `site stop --all` implementation
fredrikekelund f3cefad
Use `site stop --all` from Studio
fredrikekelund 4392224
Fix tests
fredrikekelund b8cee5b
Address review comments
fredrikekelund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,12 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { __, _n, sprintf } from '@wordpress/i18n'; | ||
| import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions'; | ||
| import { clearSiteLatestCliPid, getSiteByFolder, updateSiteAutoStart } from 'cli/lib/appdata'; | ||
| import { | ||
| clearSiteLatestCliPid, | ||
| getSiteByFolder, | ||
| readAppdata, | ||
| updateSiteAutoStart, | ||
| type SiteData, | ||
| } from 'cli/lib/appdata'; | ||
| import { connect, disconnect } from 'cli/lib/pm2-manager'; | ||
| import { stopProxyIfNoSitesNeedIt } from 'cli/lib/site-utils'; | ||
| import { isServerRunning, stopWordPressServer } from 'cli/lib/wordpress-server-manager'; | ||
|
|
@@ -9,27 +15,107 @@ import { StudioArgv } from 'cli/types'; | |
|
|
||
| const logger = new Logger< LoggerAction >(); | ||
|
|
||
| export async function runCommand( siteFolder: string, autoStart: boolean ): Promise< void > { | ||
| try { | ||
| const site = await getSiteByFolder( siteFolder ); | ||
| export enum Mode { | ||
| STOP_SINGLE_SITE, | ||
| STOP_ALL_SITES, | ||
| } | ||
|
|
||
| export async function runCommand( | ||
| target: Mode.STOP_SINGLE_SITE, | ||
| siteFolder: string, | ||
| autoStart: boolean | ||
| ): Promise< void >; | ||
| export async function runCommand( | ||
| target: Mode.STOP_ALL_SITES, | ||
| siteFolder: undefined, | ||
| autoStart: boolean | ||
| ): Promise< void >; | ||
| export async function runCommand( | ||
| target: Mode, | ||
| siteFolder: string | undefined, | ||
| autoStart: boolean | ||
| ): Promise< void > { | ||
| try { | ||
| await connect(); | ||
|
|
||
| const runningProcess = await isServerRunning( site.id ); | ||
| if ( ! runningProcess ) { | ||
| logger.reportSuccess( __( 'WordPress site is not running' ) ); | ||
| return; | ||
| const SITES_TO_TARGET: SiteData[] = []; | ||
|
|
||
| if ( target === Mode.STOP_SINGLE_SITE && siteFolder ) { | ||
| const site = await getSiteByFolder( siteFolder ); | ||
| SITES_TO_TARGET.push( site ); | ||
|
|
||
| const runningProcess = await isServerRunning( site.id ); | ||
| if ( ! runningProcess ) { | ||
| logger.reportSuccess( __( 'WordPress site is not running' ) ); | ||
| return; | ||
| } | ||
| } else { | ||
| const appdata = await readAppdata(); | ||
|
|
||
| for ( const site of appdata.sites ) { | ||
| const runningProcess = await isServerRunning( site.id ); | ||
|
|
||
| if ( runningProcess ) { | ||
| SITES_TO_TARGET.push( site ); | ||
| } | ||
| } | ||
|
|
||
| if ( ! SITES_TO_TARGET.length ) { | ||
| logger.reportSuccess( __( 'No sites are currently running' ) ); | ||
| return; | ||
| } | ||
|
|
||
| logger.reportStart( | ||
| LoggerAction.STOP_ALL_SITES, | ||
| sprintf( __( 'Stopping all WordPress sites... (%d/%d)' ), 0, SITES_TO_TARGET.length ) | ||
| ); | ||
| } | ||
|
|
||
| const stoppedSiteIds: string[] = []; | ||
|
|
||
| for ( const site of SITES_TO_TARGET ) { | ||
| try { | ||
| logger.reportProgress( | ||
| sprintf( | ||
| __( 'Stopping site "%s" (%d/%d)…' ), | ||
| site.name, | ||
| stoppedSiteIds.length + 1, | ||
| SITES_TO_TARGET.length | ||
| ) | ||
| ); | ||
| await stopWordPressServer( site.id ); | ||
| await clearSiteLatestCliPid( site.id ); | ||
| await updateSiteAutoStart( site.id, autoStart ); | ||
|
|
||
| stoppedSiteIds.push( site.id ); | ||
| } catch ( error ) { | ||
| logger.reportError( | ||
| new LoggerError( sprintf( __( 'Failed to stop site %s' ), site.name ) ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| logger.reportStart( LoggerAction.STOP_SITE, __( 'Stopping WordPress site…' ) ); | ||
| try { | ||
| await stopWordPressServer( site.id ); | ||
| await clearSiteLatestCliPid( site.id ); | ||
| await updateSiteAutoStart( site.id, autoStart ); | ||
| logger.reportSuccess( __( 'WordPress site stopped' ) ); | ||
| await stopProxyIfNoSitesNeedIt( site.id, logger ); | ||
| await stopProxyIfNoSitesNeedIt( stoppedSiteIds, logger ); | ||
| } catch ( error ) { | ||
| throw new LoggerError( __( 'Failed to stop WordPress server' ), error ); | ||
| throw new LoggerError( __( 'Failed to stop proxy server' ), error ); | ||
| } | ||
|
|
||
| if ( stoppedSiteIds.length === SITES_TO_TARGET.length ) { | ||
| logger.reportSuccess( | ||
| sprintf( | ||
| _n( | ||
| 'Successfully stopped %d site', | ||
| 'Successfully stopped %d sites', | ||
| SITES_TO_TARGET.length | ||
| ), | ||
| SITES_TO_TARGET.length | ||
| ) | ||
| ); | ||
| } else { | ||
| throw new LoggerError( | ||
| sprintf( __( 'Stopped %d sites out of %d' ), stoppedSiteIds.length, SITES_TO_TARGET.length ) | ||
|
||
| ); | ||
| } | ||
| } finally { | ||
| disconnect(); | ||
|
|
@@ -39,23 +125,36 @@ export async function runCommand( siteFolder: string, autoStart: boolean ): Prom | |
| export const registerCommand = ( yargs: StudioArgv ) => { | ||
| return yargs.command( { | ||
| command: 'stop', | ||
| describe: __( 'Stop local site' ), | ||
| describe: __( 'Stop local site(s)' ), | ||
| builder: ( yargs ) => { | ||
| return yargs.option( 'auto-start', { | ||
| type: 'boolean', | ||
| describe: __( 'Set auto-start flag for the site' ), | ||
| default: false, | ||
| hidden: true, | ||
| } ); | ||
| return yargs | ||
| .option( 'all', { | ||
| type: 'boolean', | ||
| describe: __( 'Stop all local sites' ), | ||
| default: false, | ||
| } ) | ||
| .option( 'auto-start', { | ||
| type: 'boolean', | ||
| describe: __( 'Set auto-start flag for the site(s)' ), | ||
| default: false, | ||
| hidden: true, | ||
| } ); | ||
| }, | ||
| handler: async ( argv ) => { | ||
| try { | ||
| await runCommand( argv.path, argv.autoStart ); | ||
| if ( argv.all ) { | ||
| await runCommand( Mode.STOP_ALL_SITES, undefined, argv.autoStart ); | ||
| } else { | ||
| await runCommand( Mode.STOP_SINGLE_SITE, argv.path, argv.autoStart ); | ||
| } | ||
| } catch ( error ) { | ||
| if ( error instanceof LoggerError ) { | ||
| logger.reportError( error ); | ||
| } else { | ||
| const loggerError = new LoggerError( __( 'Failed to stop site' ), error ); | ||
| const loggerError = new LoggerError( | ||
| argv.all ? __( 'Failed to stop sites' ) : __( 'Failed to stop site' ), | ||
| error | ||
| ); | ||
| logger.reportError( loggerError ); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Should we rename it to
sitesToTarget?