Skip to content

Commit 9710988

Browse files
CLI: Refine site stop --all implementation (#2374)
* Moved logic inside stop command and added flag * Simplify `site stop --all` implementation * Use `site stop --all` from Studio * Fix tests * Address review comments --------- Co-authored-by: Volodymyr Makukha <[email protected]>
1 parent f1f0731 commit 9710988

File tree

6 files changed

+421
-458
lines changed

6 files changed

+421
-458
lines changed

cli/commands/site/stop-all.ts

Lines changed: 0 additions & 143 deletions
This file was deleted.

cli/commands/site/stop.ts

Lines changed: 129 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { __ } from '@wordpress/i18n';
1+
import { __, _n, sprintf } from '@wordpress/i18n';
22
import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions';
3-
import { clearSiteLatestCliPid, getSiteByFolder, updateSiteAutoStart } from 'cli/lib/appdata';
3+
import {
4+
clearSiteLatestCliPid,
5+
getSiteByFolder,
6+
readAppdata,
7+
updateSiteAutoStart,
8+
type SiteData,
9+
} from 'cli/lib/appdata';
410
import { connect, disconnect } from 'cli/lib/pm2-manager';
511
import { stopProxyIfNoSitesNeedIt } from 'cli/lib/site-utils';
612
import { isServerRunning, stopWordPressServer } from 'cli/lib/wordpress-server-manager';
@@ -9,27 +15,112 @@ import { StudioArgv } from 'cli/types';
915

1016
const logger = new Logger< LoggerAction >();
1117

12-
export async function runCommand( siteFolder: string, autoStart: boolean ): Promise< void > {
13-
try {
14-
const site = await getSiteByFolder( siteFolder );
18+
export enum Mode {
19+
STOP_SINGLE_SITE,
20+
STOP_ALL_SITES,
21+
}
1522

23+
export async function runCommand(
24+
target: Mode.STOP_SINGLE_SITE,
25+
siteFolder: string,
26+
autoStart: boolean
27+
): Promise< void >;
28+
export async function runCommand(
29+
target: Mode.STOP_ALL_SITES,
30+
siteFolder: undefined,
31+
autoStart: boolean
32+
): Promise< void >;
33+
export async function runCommand(
34+
target: Mode,
35+
siteFolder: string | undefined,
36+
autoStart: boolean
37+
): Promise< void > {
38+
try {
1639
await connect();
1740

18-
const runningProcess = await isServerRunning( site.id );
19-
if ( ! runningProcess ) {
20-
logger.reportSuccess( __( 'WordPress site is not running' ) );
21-
return;
41+
const sitesToTarget: SiteData[] = [];
42+
43+
if ( target === Mode.STOP_SINGLE_SITE && siteFolder ) {
44+
const site = await getSiteByFolder( siteFolder );
45+
sitesToTarget.push( site );
46+
47+
const runningProcess = await isServerRunning( site.id );
48+
if ( ! runningProcess ) {
49+
logger.reportSuccess( __( 'WordPress site is not running' ) );
50+
return;
51+
}
52+
53+
logger.reportStart( LoggerAction.STOP_SITE, __( 'Stopping WordPress site…' ) );
54+
} else {
55+
const appdata = await readAppdata();
56+
57+
for ( const site of appdata.sites ) {
58+
const runningProcess = await isServerRunning( site.id );
59+
60+
if ( runningProcess ) {
61+
sitesToTarget.push( site );
62+
}
63+
}
64+
65+
if ( ! sitesToTarget.length ) {
66+
logger.reportSuccess( __( 'No sites are currently running' ) );
67+
return;
68+
}
69+
70+
logger.reportStart( LoggerAction.STOP_ALL_SITES, __( 'Stopping all WordPress sites...' ) );
71+
}
72+
73+
const stoppedSiteIds: string[] = [];
74+
75+
for ( const site of sitesToTarget ) {
76+
try {
77+
logger.reportProgress(
78+
sprintf(
79+
__( 'Stopping site "%s" (%d/%d)…' ),
80+
site.name,
81+
stoppedSiteIds.length + 1,
82+
sitesToTarget.length
83+
)
84+
);
85+
await stopWordPressServer( site.id );
86+
await clearSiteLatestCliPid( site.id );
87+
await updateSiteAutoStart( site.id, autoStart );
88+
89+
stoppedSiteIds.push( site.id );
90+
} catch ( error ) {
91+
logger.reportError(
92+
new LoggerError( sprintf( __( 'Failed to stop site %s' ), site.name ) )
93+
);
94+
}
2295
}
2396

24-
logger.reportStart( LoggerAction.STOP_SITE, __( 'Stopping WordPress site…' ) );
2597
try {
26-
await stopWordPressServer( site.id );
27-
await clearSiteLatestCliPid( site.id );
28-
await updateSiteAutoStart( site.id, autoStart );
29-
logger.reportSuccess( __( 'WordPress site stopped' ) );
30-
await stopProxyIfNoSitesNeedIt( site.id, logger );
98+
await stopProxyIfNoSitesNeedIt( stoppedSiteIds, logger );
3199
} catch ( error ) {
32-
throw new LoggerError( __( 'Failed to stop WordPress server' ), error );
100+
throw new LoggerError( __( 'Failed to stop proxy server' ), error );
101+
}
102+
103+
if ( stoppedSiteIds.length === sitesToTarget.length ) {
104+
logger.reportSuccess(
105+
sprintf(
106+
_n(
107+
'Successfully stopped %d site',
108+
'Successfully stopped %d sites',
109+
sitesToTarget.length
110+
),
111+
sitesToTarget.length
112+
)
113+
);
114+
} else if ( stoppedSiteIds.length === 0 && sitesToTarget.length === 0 ) {
115+
throw new LoggerError( __( 'Failed to stop site' ) );
116+
} else {
117+
throw new LoggerError(
118+
sprintf(
119+
_n( 'Stopped %d site out of %d', 'Stopped %d sites out of %d', stoppedSiteIds.length ),
120+
stoppedSiteIds.length,
121+
sitesToTarget.length
122+
)
123+
);
33124
}
34125
} finally {
35126
disconnect();
@@ -39,23 +130,36 @@ export async function runCommand( siteFolder: string, autoStart: boolean ): Prom
39130
export const registerCommand = ( yargs: StudioArgv ) => {
40131
return yargs.command( {
41132
command: 'stop',
42-
describe: __( 'Stop local site' ),
133+
describe: __( 'Stop local site(s)' ),
43134
builder: ( yargs ) => {
44-
return yargs.option( 'auto-start', {
45-
type: 'boolean',
46-
describe: __( 'Set auto-start flag for the site' ),
47-
default: false,
48-
hidden: true,
49-
} );
135+
return yargs
136+
.option( 'all', {
137+
type: 'boolean',
138+
describe: __( 'Stop all local sites' ),
139+
default: false,
140+
} )
141+
.option( 'auto-start', {
142+
type: 'boolean',
143+
describe: __( 'Set auto-start flag for the site(s)' ),
144+
default: false,
145+
hidden: true,
146+
} );
50147
},
51148
handler: async ( argv ) => {
52149
try {
53-
await runCommand( argv.path, argv.autoStart );
150+
if ( argv.all ) {
151+
await runCommand( Mode.STOP_ALL_SITES, undefined, argv.autoStart );
152+
} else {
153+
await runCommand( Mode.STOP_SINGLE_SITE, argv.path, argv.autoStart );
154+
}
54155
} catch ( error ) {
55156
if ( error instanceof LoggerError ) {
56157
logger.reportError( error );
57158
} else {
58-
const loggerError = new LoggerError( __( 'Failed to stop site' ), error );
159+
const loggerError = new LoggerError(
160+
argv.all ? __( 'Failed to stop sites' ) : __( 'Failed to stop site' ),
161+
error
162+
);
59163
logger.reportError( loggerError );
60164
}
61165
}

0 commit comments

Comments
 (0)