2
2
using Microsoft . Extensions . Hosting ;
3
3
using Microsoft . Extensions . Logging ;
4
4
using Microsoft . Extensions . Options ;
5
+ using RemotePythonExecution . Interface . RemotePythonExecutionServiceDependency . JsonModel ;
5
6
using System ;
6
7
using System . Collections . Generic ;
7
8
using System . Diagnostics ;
@@ -36,6 +37,9 @@ public class RemotePythonExecutionService : BackgroundService, IHostedService
36
37
private string mWorkingDirrectoryPath = string . Empty ;
37
38
private string mSourceCodeSavePath = string . Empty ;
38
39
40
+ private bool mIsProcessEnded = false ;
41
+ private bool mIsOutputEnded = false ;
42
+
39
43
#endregion
40
44
41
45
#region ~
@@ -173,10 +177,9 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
173
177
}
174
178
}
175
179
176
-
177
180
private void ProcessErrorDataReceived ( object sender , DataReceivedEventArgs e )
178
181
{
179
- if ( ! string . IsNullOrEmpty ( e . Data ) )
182
+ if ( e . Data != null )
180
183
{
181
184
try
182
185
{
@@ -194,12 +197,11 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
194
197
195
198
private async void ProcessOutputDataReceived ( object sender , DataReceivedEventArgs e )
196
199
{
197
-
198
- if ( ! ( e . Data == null ) )
200
+ if ( e . Data != null )
199
201
{
200
202
try
201
203
{
202
- await mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data , start : 0 ) ;
204
+ await mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data ) ;
203
205
mLogger . LogDebug ( "{data}" , e . Data ) ;
204
206
}
205
207
catch ( TaskCanceledException )
@@ -258,13 +260,24 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
258
260
IsOutputEnded = false ;
259
261
IsProcessEnded = false ;
260
262
261
- int exitCode = - 100 ;
263
+ ExitData exitData = new ( ) ;
262
264
263
265
try
264
266
{
265
267
if ( mProcess . HasExited )
266
268
{
267
- exitCode = mProcess . ExitCode ;
269
+ exitData = new ExitData
270
+ {
271
+ IsExitDataUpdated = true ,
272
+
273
+ ProcessId = mProcess . Id ,
274
+ StartTime = mProcess . StartTime ,
275
+ ExitTime = mProcess . ExitTime ,
276
+ TotalProcessorTime = mProcess . TotalProcessorTime ,
277
+ UserProcessorTime = mProcess . UserProcessorTime ,
278
+ ExitCode = mProcess . ExitCode
279
+ } ;
280
+
268
281
mProcess . Dispose ( ) ;
269
282
mProcess = null ;
270
283
}
@@ -274,11 +287,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
274
287
mLogger . LogError ( "Error when procces close {error}" , exception ) ;
275
288
}
276
289
277
- var exitData = new Dictionary < string , object > ( ) { { "exit" , exitCode } } ;
290
+ var exitJson = mTcpServer . SerializationHelper . SerializeJson ( exitData ) ;
291
+ var exitDictonary = new Dictionary < string , object > ( ) { { "exitData" , exitJson } } ;
278
292
279
293
if ( mTcpServer . IsClientConnected ( CurrentConnectionGuid ) )
280
294
{
281
- await mTcpServer . SendAsync ( CurrentConnectionGuid , "" , exitData , token : stoppingToken ) ;
295
+ await mTcpServer . SendAsync ( CurrentConnectionGuid , string . Empty , exitDictonary , token : stoppingToken ) ;
282
296
await mTcpServer . DisconnectClientAsync ( CurrentConnectionGuid , MessageStatus . Removed , true , stoppingToken ) ;
283
297
}
284
298
@@ -291,7 +305,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
291
305
292
306
#region Private fields
293
307
294
- private bool mIsOutputEnded = false ;
295
308
private bool IsOutputEnded
296
309
{
297
310
get { return mIsOutputEnded ; }
@@ -305,7 +318,7 @@ private bool IsOutputEnded
305
318
}
306
319
307
320
308
- private bool mIsProcessEnded = false ;
321
+
309
322
private bool IsProcessEnded
310
323
{
311
324
get { return mIsProcessEnded ; }
@@ -438,8 +451,8 @@ private void OnServerAddressChange()
438
451
{
439
452
if ( mTcpServer . Connections != 0 )
440
453
{
454
+ KillProcess ( ) ;
441
455
IsOutputEnded = true ;
442
- IsProcessEnded = true ;
443
456
}
444
457
445
458
mTcpServer . Stop ( ) ;
@@ -512,7 +525,6 @@ private void StartProcess(bool withDebug = false)
512
525
mProcess . Exited += ProcessExited ;
513
526
mProcess . OutputDataReceived += ProcessOutputDataReceived ;
514
527
mProcess . ErrorDataReceived += ProcessErrorDataReceived ;
515
- mProcess . Disposed += Disposed ;
516
528
mProcess . Start ( ) ;
517
529
518
530
mProcess . BeginOutputReadLine ( ) ;
@@ -521,11 +533,6 @@ private void StartProcess(bool withDebug = false)
521
533
mProcess . WaitForExit ( ) ;
522
534
}
523
535
524
- private void Disposed ( object sender , EventArgs e )
525
- {
526
- mLogger . LogDebug ( "Process disposed" ) ;
527
- }
528
-
529
536
protected virtual void Dispose ( bool disposing )
530
537
{
531
538
if ( ! mIsDisposed )
@@ -534,11 +541,10 @@ protected virtual void Dispose(bool disposing)
534
541
535
542
if ( disposing )
536
543
{
537
- UnSubscribe ( ) ;
538
- mProcess ? . Close ( ) ;
539
- mProcess ? . Dispose ( ) ;
544
+ KillProcess ( ) ;
545
+ IsOutputEnded = true ;
540
546
541
- mTcpServer . DisconnectClientsAsync ( ) ;
547
+ UnSubscribe ( ) ;
542
548
mTcpServer . Stop ( ) ;
543
549
mTcpServer . Dispose ( ) ;
544
550
mLogger . LogInformation ( "Service stop and dispose" ) ;
0 commit comments