@@ -15,23 +15,57 @@ internal sealed class StopCommand : Command
1515 Description = "Force stop the proxy by killing the process"
1616 } ;
1717
18- public StopCommand ( ) : base ( "stop" , "Stop running Dev Proxy instance" )
18+ private readonly Option < int ? > _pidOption = new ( "--pid" )
19+ {
20+ Description = "Stop a specific Dev Proxy instance by PID"
21+ } ;
22+
23+ public StopCommand ( ) : base ( "stop" , "Stop running Dev Proxy instances" )
1924 {
2025 Add ( _forceOption ) ;
26+ Add ( _pidOption ) ;
2127 SetAction ( RunAsync ) ;
2228 }
2329
2430 private async Task < int > RunAsync ( ParseResult parseResult , CancellationToken cancellationToken )
2531 {
2632 var force = parseResult . GetValue ( _forceOption ) ;
27- var state = await StateManager . LoadStateAsync ( cancellationToken ) ;
33+ var pid = parseResult . GetValue ( _pidOption ) ;
34+
35+ if ( pid is not null )
36+ {
37+ var state = await StateManager . LoadStateByPidAsync ( pid . Value , cancellationToken ) ;
38+ if ( state is null )
39+ {
40+ Console . WriteLine ( $ "No running Dev Proxy instance with PID { pid . Value } .") ;
41+ return 1 ;
42+ }
43+
44+ return await StopInstanceAsync ( state , force , cancellationToken ) ;
45+ }
2846
29- if ( state == null )
47+ var states = await StateManager . LoadAllStatesAsync ( cancellationToken ) ;
48+ if ( states . Count == 0 )
3049 {
3150 Console . WriteLine ( "Dev Proxy is not running." ) ;
3251 return 1 ;
3352 }
3453
54+ var exitCode = 0 ;
55+ foreach ( var state in states )
56+ {
57+ var result = await StopInstanceAsync ( state , force , cancellationToken ) ;
58+ if ( result != 0 )
59+ {
60+ exitCode = result ;
61+ }
62+ }
63+
64+ return exitCode ;
65+ }
66+
67+ private static async Task < int > StopInstanceAsync ( ProxyInstanceState state , bool force , CancellationToken cancellationToken )
68+ {
3569 if ( force )
3670 {
3771 return await ForceStopAsync ( state , cancellationToken ) ;
@@ -45,7 +79,7 @@ private async Task<int> RunAsync(ParseResult parseResult, CancellationToken canc
4579
4680 if ( response . IsSuccessStatusCode || response . StatusCode == System . Net . HttpStatusCode . Accepted )
4781 {
48- Console . WriteLine ( "Stopping Dev Proxy..." ) ;
82+ Console . WriteLine ( $ "Stopping Dev Proxy (PID: { state . Pid } ) ...") ;
4983
5084 // Wait for process to exit
5185 var stopwatch = Stopwatch . StartNew ( ) ;
@@ -82,30 +116,30 @@ private async Task<int> RunAsync(ParseResult parseResult, CancellationToken canc
82116
83117 if ( ! exited )
84118 {
85- Console . WriteLine ( "Dev Proxy did not stop in time." ) ;
86- Console . WriteLine ( "Use --force to forcefully terminate the process." ) ;
119+ Console . WriteLine ( $ "Dev Proxy (PID: { state . Pid } ) did not stop in time.") ;
120+ Console . WriteLine ( $ "Use --force --pid { state . Pid } to forcefully terminate the process.") ;
87121 return 1 ;
88122 }
89123
90- await StateManager . DeleteStateAsync ( cancellationToken ) ;
91- Console . WriteLine ( "Dev Proxy stopped." ) ;
124+ await StateManager . DeleteStateAsync ( state . Pid , cancellationToken ) ;
125+ Console . WriteLine ( $ "Dev Proxy (PID: { state . Pid } ) stopped.") ;
92126 return 0 ;
93127 }
94128
95- Console . WriteLine ( $ "Failed to stop Dev Proxy: { response . StatusCode } ") ;
96- Console . WriteLine ( "Use --force to forcefully terminate the process." ) ;
129+ Console . WriteLine ( $ "Failed to stop Dev Proxy (PID: { state . Pid } ) : { response . StatusCode } ") ;
130+ Console . WriteLine ( $ "Use --force --pid { state . Pid } to forcefully terminate the process.") ;
97131 return 1 ;
98132 }
99133 catch ( HttpRequestException ex )
100134 {
101- Console . WriteLine ( $ "Failed to connect to Dev Proxy API: { ex . Message } ") ;
102- Console . WriteLine ( "Use --force to forcefully terminate the process." ) ;
135+ Console . WriteLine ( $ "Failed to connect to Dev Proxy API (PID: { state . Pid } ) : { ex . Message } ") ;
136+ Console . WriteLine ( $ "Use --force --pid { state . Pid } to forcefully terminate the process.") ;
103137 return 1 ;
104138 }
105139 catch ( TaskCanceledException )
106140 {
107- Console . WriteLine ( "Timeout waiting for Dev Proxy to respond." ) ;
108- Console . WriteLine ( "Use --force to forcefully terminate the process." ) ;
141+ Console . WriteLine ( $ "Timeout waiting for Dev Proxy (PID: { state . Pid } ) to respond.") ;
142+ Console . WriteLine ( $ "Use --force --pid { state . Pid } to forcefully terminate the process.") ;
109143 return 1 ;
110144 }
111145 }
@@ -135,7 +169,7 @@ private static async Task<int> ForceStopAsync(ProxyInstanceState state, Cancella
135169 return 1 ;
136170 }
137171
138- await StateManager . DeleteStateAsync ( cancellationToken ) ;
172+ await StateManager . DeleteStateAsync ( state . Pid , cancellationToken ) ;
139173 return 0 ;
140174 }
141175
0 commit comments