Skip to content

Commit 281970e

Browse files
committed
Close #8
1 parent 7525efc commit 281970e

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace RemotePythonExecution.Interface.RemotePythonExecutionServiceDependency.JsonModel
4+
{
5+
public class ExitData
6+
{
7+
public bool IsExitDataUpdated { get; set; } = false;
8+
public int ProcessId { get; set; }
9+
public DateTime StartTime { get; set; }
10+
public DateTime ExitTime { get; set; }
11+
public TimeSpan TotalProcessorTime { get; set; }
12+
public TimeSpan UserProcessorTime { get; set; }
13+
public int ExitCode { get; set; }
14+
}
15+
}

src/RemotePythonExecution.Services/RemotePythonExecutionService.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
44
using Microsoft.Extensions.Options;
5+
using RemotePythonExecution.Interface.RemotePythonExecutionServiceDependency.JsonModel;
56
using System;
67
using System.Collections.Generic;
78
using System.Diagnostics;
@@ -36,6 +37,9 @@ public class RemotePythonExecutionService : BackgroundService, IHostedService
3637
private string mWorkingDirrectoryPath = string.Empty;
3738
private string mSourceCodeSavePath = string.Empty;
3839

40+
private bool mIsProcessEnded = false;
41+
private bool mIsOutputEnded = false;
42+
3943
#endregion
4044

4145
#region ~
@@ -173,10 +177,9 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
173177
}
174178
}
175179

176-
177180
private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
178181
{
179-
if (!string.IsNullOrEmpty(e.Data))
182+
if (e.Data != null)
180183
{
181184
try
182185
{
@@ -194,12 +197,11 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
194197

195198
private async void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
196199
{
197-
198-
if (!(e.Data == null))
200+
if (e.Data != null)
199201
{
200202
try
201203
{
202-
await mTcpServer.SendAsync(CurrentConnectionGuid, e.Data, start: 0);
204+
await mTcpServer.SendAsync(CurrentConnectionGuid, e.Data);
203205
mLogger.LogDebug("{data}", e.Data);
204206
}
205207
catch (TaskCanceledException)
@@ -258,13 +260,24 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
258260
IsOutputEnded = false;
259261
IsProcessEnded = false;
260262

261-
int exitCode = -100;
263+
ExitData exitData = new();
262264

263265
try
264266
{
265267
if (mProcess.HasExited)
266268
{
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+
268281
mProcess.Dispose();
269282
mProcess = null;
270283
}
@@ -274,11 +287,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
274287
mLogger.LogError("Error when procces close {error}", exception);
275288
}
276289

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 } };
278292

279293
if (mTcpServer.IsClientConnected(CurrentConnectionGuid))
280294
{
281-
await mTcpServer.SendAsync(CurrentConnectionGuid, "", exitData, token: stoppingToken);
295+
await mTcpServer.SendAsync(CurrentConnectionGuid, string.Empty, exitDictonary, token: stoppingToken);
282296
await mTcpServer.DisconnectClientAsync(CurrentConnectionGuid, MessageStatus.Removed, true, stoppingToken);
283297
}
284298

@@ -291,7 +305,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
291305

292306
#region Private fields
293307

294-
private bool mIsOutputEnded = false;
295308
private bool IsOutputEnded
296309
{
297310
get { return mIsOutputEnded; }
@@ -305,7 +318,7 @@ private bool IsOutputEnded
305318
}
306319

307320

308-
private bool mIsProcessEnded = false;
321+
309322
private bool IsProcessEnded
310323
{
311324
get { return mIsProcessEnded; }
@@ -438,8 +451,8 @@ private void OnServerAddressChange()
438451
{
439452
if(mTcpServer.Connections != 0)
440453
{
454+
KillProcess();
441455
IsOutputEnded = true;
442-
IsProcessEnded = true;
443456
}
444457

445458
mTcpServer.Stop();
@@ -512,7 +525,6 @@ private void StartProcess(bool withDebug = false)
512525
mProcess.Exited += ProcessExited;
513526
mProcess.OutputDataReceived += ProcessOutputDataReceived;
514527
mProcess.ErrorDataReceived += ProcessErrorDataReceived;
515-
mProcess.Disposed += Disposed;
516528
mProcess.Start();
517529

518530
mProcess.BeginOutputReadLine();
@@ -521,11 +533,6 @@ private void StartProcess(bool withDebug = false)
521533
mProcess.WaitForExit();
522534
}
523535

524-
private void Disposed(object sender, EventArgs e)
525-
{
526-
mLogger.LogDebug("Process disposed");
527-
}
528-
529536
protected virtual void Dispose(bool disposing)
530537
{
531538
if (!mIsDisposed)
@@ -534,11 +541,10 @@ protected virtual void Dispose(bool disposing)
534541

535542
if (disposing)
536543
{
537-
UnSubscribe();
538-
mProcess?.Close();
539-
mProcess?.Dispose();
544+
KillProcess();
545+
IsOutputEnded = true;
540546

541-
mTcpServer.DisconnectClientsAsync();
547+
UnSubscribe();
542548
mTcpServer.Stop();
543549
mTcpServer.Dispose();
544550
mLogger.LogInformation("Service stop and dispose");

src/RemotePythonExecution/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"Serilog": {
33
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
44
"MinimumLevel": {
5-
"Default": "Debug",
5+
"Default": "Information",
66
"Override": {
77
"Microsoft": "Warning",
88
"System": "Warning"

0 commit comments

Comments
 (0)