2
2
using Microsoft . Extensions . Hosting ;
3
3
using Microsoft . Extensions . Logging ;
4
4
using RemotePythonExecution . Interface ;
5
- using RemotePythonExecution . Interface . RemotePythonExecutionServiceDependency . JsonModel ;
6
5
using System ;
7
6
using System . Collections . Generic ;
8
7
using System . Diagnostics ;
9
8
using System . IO ;
10
- using System . Linq ;
11
9
using System . Runtime . InteropServices ;
12
- using System . Text . Json ;
13
10
using System . Threading ;
14
11
using System . Threading . Tasks ;
15
12
using WatsonTcp ;
@@ -37,6 +34,8 @@ public class RemotePythonExecutionService : BackgroundService
37
34
private string WorkingDirrectoryPath = "" ;
38
35
private string SourceCodeSavePaths = "" ;
39
36
37
+ private bool mIsDisposed ;
38
+
40
39
#endregion
41
40
42
41
#region ~
@@ -46,32 +45,22 @@ public RemotePythonExecutionService(IServiceProvider serviceProvider)
46
45
mLogger = serviceProvider . GetRequiredService < ILogger < RemotePythonExecutionService > > ( ) ;
47
46
mAppSettingService = serviceProvider . GetRequiredService < IAppSettingService > ( ) ;
48
47
49
- mTcpServer = new WatsonTcpServer ( "0.0.0.0" , 19000 ) ;
48
+ string ip = mAppSettingService . ServerSettings . Ip ;
49
+ int port = mAppSettingService . ServerSettings . Port ;
50
+
51
+ mTcpServer = new WatsonTcpServer ( ip , port ) ;
50
52
51
53
Subscribe ( ) ;
52
54
53
55
mTcpServer . Start ( ) ;
54
56
55
- mLogger . LogInformation ( "Load RemotePythonExecutionService ~" ) ;
56
-
57
57
SetPath ( ) ;
58
- }
59
58
60
- private void SetPath ( )
61
- {
62
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
63
- {
64
- SourceCodeSavePaths = mAppSettingService . SourceCodeSavePaths . Windows ;
65
- InterpreterPath = mAppSettingService . PythonPaths . InterpreterPath . Windows ;
66
- WorkingDirrectoryPath = mAppSettingService . PythonPaths . WorkingDirrectoryPath . Windows ;
67
- return ;
68
- }
69
-
70
- SourceCodeSavePaths = mAppSettingService . SourceCodeSavePaths . Linux ;
71
- InterpreterPath = mAppSettingService . PythonPaths . InterpreterPath . Linux ;
72
- WorkingDirrectoryPath = mAppSettingService . PythonPaths . WorkingDirrectoryPath . Linux ;
59
+ mLogger . LogInformation ( "Server runing on {ip}:{port}" , ip , port ) ;
73
60
}
74
61
62
+ #endregion
63
+
75
64
#region Subscribe/Unsubscribe
76
65
77
66
private void Subscribe ( )
@@ -161,18 +150,60 @@ private void MessageReceived(object sender, MessageReceivedEventArgs e)
161
150
}
162
151
}
163
152
153
+ private void ProcessErrorDataReceived ( object sender , DataReceivedEventArgs e )
154
+ {
155
+ if ( ! string . IsNullOrEmpty ( e . Data ) )
156
+ {
157
+ try
158
+ {
159
+ mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data ) ;
160
+ mIsOutputEnded = false ;
161
+ }
162
+ catch ( Exception exp )
163
+ {
164
+ mLogger . LogError ( "Catch happened {exp}" , exp ) ;
165
+ mIsOutputEnded = true ;
166
+ }
167
+ }
168
+ }
169
+
170
+ private void ProcessOutputDataReceived ( object sender , DataReceivedEventArgs e )
171
+ {
172
+ if ( ! string . IsNullOrEmpty ( e . Data ) )
173
+ {
174
+ try
175
+ {
176
+ mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data ) ;
177
+ mLogger . LogDebug ( "{data}" , e . Data ) ;
178
+ mIsOutputEnded = false ;
179
+ }
180
+ catch ( Exception exp )
181
+ {
182
+ mLogger . LogError ( "Catch happened {exp}" , exp ) ;
183
+ mIsOutputEnded = true ;
184
+ }
185
+ }
186
+ else
187
+ {
188
+ mLogger . LogDebug ( "Output ended happened" ) ;
189
+ mIsOutputEnded = true ;
190
+ }
191
+ }
192
+
193
+ private void ProcessExited ( object sender , EventArgs e )
194
+ {
195
+ mLogger . LogDebug ( "Process ended happened" ) ;
196
+ mIsProcessEnded = true ;
197
+ }
198
+
164
199
#endregion
165
200
201
+ #region Public methods
202
+
166
203
public override void Dispose ( )
167
204
{
168
- UnSubscribe ( ) ;
169
- mProcess ? . Close ( ) ;
170
- mProcess ? . Dispose ( ) ;
171
-
172
- mTcpServer . DisconnectClientsAsync ( ) ;
173
- mTcpServer . Stop ( ) ;
174
- mTcpServer . Dispose ( ) ;
175
- mLogger . LogInformation ( "Service stop and dispose" ) ;
205
+ Dispose ( true ) ;
206
+ GC . SuppressFinalize ( this ) ;
176
207
177
208
base . Dispose ( ) ;
178
209
}
@@ -201,7 +232,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
201
232
mLogger . LogError ( "Error when procces close {error}" , exception . Message ) ;
202
233
}
203
234
204
-
205
235
if ( mTcpServer . IsClientConnected ( CurrentConnectionGuid ) )
206
236
await mTcpServer . DisconnectClientAsync ( CurrentConnectionGuid , MessageStatus . Removed , true , stoppingToken ) ;
207
237
@@ -214,6 +244,21 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
214
244
215
245
#region Private methods
216
246
247
+ private void SetPath ( )
248
+ {
249
+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
250
+ {
251
+ SourceCodeSavePaths = mAppSettingService . SourceCodeSavePaths . Windows ;
252
+ InterpreterPath = mAppSettingService . PythonPaths . InterpreterPath . Windows ;
253
+ WorkingDirrectoryPath = mAppSettingService . PythonPaths . WorkingDirrectoryPath . Windows ;
254
+ return ;
255
+ }
256
+
257
+ SourceCodeSavePaths = mAppSettingService . SourceCodeSavePaths . Linux ;
258
+ InterpreterPath = mAppSettingService . PythonPaths . InterpreterPath . Linux ;
259
+ WorkingDirrectoryPath = mAppSettingService . PythonPaths . WorkingDirrectoryPath . Linux ;
260
+ }
261
+
217
262
private void SaveCodeAndStartProcess ( string code , bool withDebug )
218
263
{
219
264
File . WriteAllText ( SourceCodeSavePaths , code ) ;
@@ -238,13 +283,13 @@ private void StartProcess(bool withDebug = false)
238
283
RedirectStandardError = true ,
239
284
RedirectStandardInput = true ,
240
285
} ;
286
+
241
287
mProcess = new ( )
242
288
{
243
289
StartInfo = proccesInfo ,
244
290
EnableRaisingEvents = true ,
245
291
} ;
246
292
247
-
248
293
mProcess . Exited += ProcessExited ;
249
294
mProcess . OutputDataReceived += ProcessOutputDataReceived ;
250
295
mProcess . ErrorDataReceived += ProcessErrorDataReceived ;
@@ -254,49 +299,24 @@ private void StartProcess(bool withDebug = false)
254
299
mProcess . WaitForExit ( ) ;
255
300
}
256
301
257
- private void ProcessErrorDataReceived ( object sender , DataReceivedEventArgs e )
302
+ protected virtual void Dispose ( bool disposing )
258
303
{
259
- if ( ! string . IsNullOrEmpty ( e . Data ) )
304
+ if ( ! mIsDisposed )
260
305
{
261
- try
262
- {
263
- mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data ) ;
264
- mIsOutputEnded = false ;
265
- }
266
- catch
267
- {
268
- mIsOutputEnded = true ;
269
- }
270
- }
271
- }
306
+ mIsDisposed = true ;
272
307
273
- private void ProcessOutputDataReceived ( object sender , DataReceivedEventArgs e )
274
- {
275
- if ( ! string . IsNullOrEmpty ( e . Data ) )
276
- {
277
- try
278
- {
279
- mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data ) ;
280
- mLogger . LogDebug ( "{data}" , e . Data ) ;
281
- mIsOutputEnded = false ;
282
- }
283
- catch ( Exception exp )
308
+ if ( disposing )
284
309
{
285
- mLogger . LogDebug ( "Catch happened {exp}" , exp ) ;
286
- mIsOutputEnded = true ;
310
+ UnSubscribe ( ) ;
311
+ mProcess ? . Close ( ) ;
312
+ mProcess ? . Dispose ( ) ;
313
+
314
+ mTcpServer . DisconnectClientsAsync ( ) ;
315
+ mTcpServer . Stop ( ) ;
316
+ mTcpServer . Dispose ( ) ;
317
+ mLogger . LogInformation ( "Service stop and dispose" ) ;
287
318
}
288
319
}
289
- else
290
- {
291
- mLogger . LogDebug ( "Output ended happened" ) ;
292
- mIsOutputEnded = true ;
293
- }
294
- }
295
-
296
- private void ProcessExited ( object sender , EventArgs e )
297
- {
298
- mLogger . LogDebug ( "Process ended happened" ) ;
299
- mIsProcessEnded = true ;
300
320
}
301
321
302
322
#endregion
0 commit comments