-
Notifications
You must be signed in to change notification settings - Fork 50
Studio: Implement studio site stop CLI command #2140
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
bcotrim
merged 7 commits into
trunk
from
stu-951-studio-implement-the-studio-site-stop-command
Nov 28, 2025
+508
−18
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd24a5f
Implement studio site stop CLI command with graceful shutdown
bcotrim 25bd8ce
Fix types and remove sendStopMessage
fredrikekelund e2bc082
stop proxy if needed
bcotrim fc01870
use getSiteByFolder
bcotrim 57a05ba
Fix issues from my previous commits
fredrikekelund 1a81ea8
Clean up test file
fredrikekelund a4b15b7
fix lint and unit tests
bcotrim 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 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
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { SiteCommandLoggerAction as LoggerAction } from 'common/logger-actions'; | ||
| import { clearSiteLatestCliPid, getSiteByFolder } 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'; | ||
| import { Logger, LoggerError } from 'cli/logger'; | ||
| import { StudioArgv } from 'cli/types'; | ||
|
|
||
| export async function runCommand( siteFolder: string ): Promise< void > { | ||
| const logger = new Logger< LoggerAction >(); | ||
|
|
||
| try { | ||
| const site = await getSiteByFolder( siteFolder, false ); | ||
|
|
||
| await connect(); | ||
|
|
||
| const runningProcess = await isServerRunning( site.id ); | ||
| if ( ! runningProcess ) { | ||
| logger.reportSuccess( __( 'WordPress site is not running' ) ); | ||
| return; | ||
| } | ||
|
|
||
| logger.reportStart( LoggerAction.STOP_SITE, __( 'Stopping WordPress site...' ) ); | ||
| try { | ||
| await stopWordPressServer( site.id ); | ||
| await clearSiteLatestCliPid( site.id ); | ||
| logger.reportSuccess( __( 'WordPress site stopped' ) ); | ||
| await stopProxyIfNoSitesNeedIt( site.id, logger ); | ||
| } catch ( error ) { | ||
| throw new LoggerError( __( 'Failed to stop WordPress server' ), error ); | ||
| } | ||
| } catch ( error ) { | ||
| if ( error instanceof LoggerError ) { | ||
| logger.reportError( error ); | ||
| } else { | ||
| const loggerError = new LoggerError( __( 'Failed to stop site infrastructure' ), error ); | ||
| logger.reportError( loggerError ); | ||
| } | ||
| } finally { | ||
| disconnect(); | ||
| } | ||
| } | ||
|
|
||
| export const registerCommand = ( yargs: StudioArgv ) => { | ||
| return yargs.command( { | ||
| command: 'stop', | ||
| describe: __( 'Stop local site' ), | ||
| builder: ( yargs ) => { | ||
| return yargs; | ||
| }, | ||
| handler: async ( argv ) => { | ||
| await runCommand( argv.path ); | ||
| }, | ||
| } ); | ||
| }; | ||
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 |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { SiteData, clearSiteLatestCliPid, getSiteByFolder } 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'; | ||
| import { Logger, LoggerError } from 'cli/logger'; | ||
|
|
||
| jest.mock( 'cli/lib/appdata', () => ( { | ||
| ...jest.requireActual( 'cli/lib/appdata' ), | ||
| getAppdataDirectory: jest.fn().mockReturnValue( '/test/appdata' ), | ||
| getSiteByFolder: jest.fn(), | ||
| clearSiteLatestCliPid: jest.fn(), | ||
| getSiteUrl: jest.fn( ( site ) => `http://localhost:${ site.port }` ), | ||
| } ) ); | ||
| jest.mock( 'cli/lib/pm2-manager' ); | ||
| jest.mock( 'cli/lib/site-utils' ); | ||
| jest.mock( 'cli/lib/wordpress-server-manager' ); | ||
| jest.mock( 'cli/logger' ); | ||
|
|
||
| describe( 'Site Stop Command', () => { | ||
| const mockSiteFolder = '/test/site/path'; | ||
| const mockSiteData: SiteData = { | ||
| id: 'test-site-id', | ||
| name: 'Test Site', | ||
| path: mockSiteFolder, | ||
| port: 8881, | ||
| adminUsername: 'admin', | ||
| adminPassword: 'password123', | ||
| running: true, | ||
| phpVersion: '8.0', | ||
| url: `http://localhost:8881`, | ||
| }; | ||
|
|
||
| const mockProcessDescription = { | ||
| name: 'test-site-id', | ||
| pmId: 0, | ||
| status: 'online', | ||
| pid: 12345, | ||
| }; | ||
|
|
||
| let mockLogger: { | ||
| reportStart: jest.Mock; | ||
| reportSuccess: jest.Mock; | ||
| reportError: jest.Mock; | ||
| }; | ||
|
|
||
| beforeEach( () => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| mockLogger = { | ||
| reportStart: jest.fn(), | ||
| reportSuccess: jest.fn(), | ||
| reportError: jest.fn(), | ||
| }; | ||
|
|
||
| ( Logger as jest.Mock ).mockReturnValue( mockLogger ); | ||
| ( connect as jest.Mock ).mockResolvedValue( undefined ); | ||
| ( disconnect as jest.Mock ).mockReturnValue( undefined ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); | ||
| ( stopWordPressServer as jest.Mock ).mockResolvedValue( undefined ); | ||
| ( clearSiteLatestCliPid as jest.Mock ).mockResolvedValue( undefined ); | ||
| ( stopProxyIfNoSitesNeedIt as jest.Mock ).mockResolvedValue( undefined ); | ||
| } ); | ||
|
|
||
| afterEach( () => { | ||
| jest.restoreAllMocks(); | ||
| } ); | ||
|
|
||
| describe( 'Error Handling', () => { | ||
| it( 'should handle site not found error', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockRejectedValue( new Error( 'Site not found' ) ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); | ||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should handle PM2 connection failure', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( connect as jest.Mock ).mockRejectedValue( new Error( 'PM2 connection failed' ) ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( mockLogger.reportError ).toHaveBeenCalled(); | ||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should handle WordPress server stop failure', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); | ||
| ( stopWordPressServer as jest.Mock ).mockRejectedValue( new Error( 'Server stop failed' ) ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( mockLogger.reportStart ).toHaveBeenCalledWith( | ||
| 'stopSite', | ||
| 'Stopping WordPress site...' | ||
| ); | ||
| expect( mockLogger.reportError ).toHaveBeenCalledWith( expect.any( LoggerError ) ); | ||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'Success Cases', () => { | ||
| it( 'should report that site is not running if server is not running', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( mockLogger.reportSuccess ).toHaveBeenCalledWith( 'WordPress site is not running' ); | ||
| expect( stopWordPressServer ).not.toHaveBeenCalled(); | ||
| expect( clearSiteLatestCliPid ).not.toHaveBeenCalled(); | ||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should stop a running site', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( connect ).toHaveBeenCalled(); | ||
| expect( isServerRunning ).toHaveBeenCalledWith( mockSiteData.id ); | ||
| expect( mockLogger.reportStart ).toHaveBeenCalledWith( | ||
| 'stopSite', | ||
| 'Stopping WordPress site...' | ||
| ); | ||
| expect( stopWordPressServer ).toHaveBeenCalledWith( mockSiteData.id ); | ||
| expect( clearSiteLatestCliPid ).toHaveBeenCalledWith( mockSiteData.id ); | ||
| expect( mockLogger.reportSuccess ).toHaveBeenCalledWith( 'WordPress site stopped' ); | ||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should call stopProxyIfNoSitesNeedIt after stopping a site', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( stopProxyIfNoSitesNeedIt ).toHaveBeenCalledWith( mockSiteData.id, mockLogger ); | ||
| } ); | ||
|
|
||
| it( 'should not call stopProxyIfNoSitesNeedIt if site is not running', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( stopProxyIfNoSitesNeedIt ).not.toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'Cleanup', () => { | ||
| it( 'should disconnect from PM2 even on error', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockRejectedValue( new Error( 'Site error' ) ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should disconnect from PM2 on success', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( mockProcessDescription ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
|
|
||
| it( 'should disconnect from PM2 even if server was not running', async () => { | ||
| ( getSiteByFolder as jest.Mock ).mockResolvedValue( mockSiteData ); | ||
| ( isServerRunning as jest.Mock ).mockResolvedValue( undefined ); | ||
|
|
||
| const { runCommand } = await import( '../stop' ); | ||
| await runCommand( mockSiteFolder ); | ||
|
|
||
| expect( disconnect ).toHaveBeenCalled(); | ||
| } ); | ||
| } ); | ||
| } ); |
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
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
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
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
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.