1
1
using Microsoft . Extensions . DependencyInjection ;
2
2
using Microsoft . Extensions . Hosting ;
3
3
using Microsoft . Extensions . Logging ;
4
+ using Microsoft . Extensions . Options ;
4
5
using RemotePythonExecution . Interface ;
5
6
using System ;
6
7
using System . Collections . Generic ;
@@ -18,7 +19,7 @@ public class RemotePythonExecutionService : BackgroundService
18
19
#region Services
19
20
20
21
private readonly ILogger < RemotePythonExecutionService > mLogger ;
21
- private readonly IAppSettingService mAppSettingService ;
22
+ private readonly IOptionsMonitor < AppSettings > mAppSettingsMonitor ;
22
23
23
24
#endregion
24
25
@@ -29,11 +30,6 @@ public class RemotePythonExecutionService : BackgroundService
29
30
private bool mIsOutputEnded = false ;
30
31
private bool mIsProcessEnded = false ;
31
32
public Guid CurrentConnectionGuid ;
32
-
33
- private string InterpreterPath = "" ;
34
- private string WorkingDirrectoryPath = "" ;
35
- private string SourceCodeSavePaths = "" ;
36
-
37
33
private bool mIsDisposed ;
38
34
39
35
#endregion
@@ -43,18 +39,20 @@ public class RemotePythonExecutionService : BackgroundService
43
39
public RemotePythonExecutionService ( IServiceProvider serviceProvider )
44
40
{
45
41
mLogger = serviceProvider . GetRequiredService < ILogger < RemotePythonExecutionService > > ( ) ;
46
- mAppSettingService = serviceProvider . GetRequiredService < IAppSettingService > ( ) ;
42
+ mAppSettingsMonitor = serviceProvider . GetRequiredService < IOptionsMonitor < AppSettings > > ( ) ;
47
43
48
- string ip = mAppSettingService . ServerSettings . Ip ;
49
- int port = mAppSettingService . ServerSettings . Port ;
44
+
45
+ string ip = mAppSettingsMonitor . CurrentValue . ServerSettings . Ip ;
46
+ int port = mAppSettingsMonitor . CurrentValue . ServerSettings . Port ;
50
47
51
48
mTcpServer = new WatsonTcpServer ( ip , port ) ;
49
+ mAppSettingsMonitor . OnChange ( OnChangeSettings ) ;
52
50
53
51
Subscribe ( ) ;
54
52
55
53
mTcpServer . Start ( ) ;
56
54
57
- SetPath ( ) ;
55
+ SetPath ( mAppSettingsMonitor . CurrentValue ) ;
58
56
59
57
mLogger . LogInformation ( "Server runing on {ip}:{port}" , ip , port ) ;
60
58
}
@@ -82,6 +80,10 @@ private void UnSubscribe()
82
80
#endregion
83
81
84
82
#region Events
83
+ private void OnChangeSettings ( AppSettings settings , string arg2 )
84
+ {
85
+ SetPath ( settings ) ;
86
+ }
85
87
86
88
private void ClientConnected ( object sender , ConnectionEventArgs e )
87
89
{
@@ -167,13 +169,13 @@ private void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
167
169
}
168
170
}
169
171
170
- private void ProcessOutputDataReceived ( object sender , DataReceivedEventArgs e )
172
+ private async void ProcessOutputDataReceived ( object sender , DataReceivedEventArgs e )
171
173
{
172
- if ( ! string . IsNullOrEmpty ( e . Data ) )
174
+ if ( ! ( e . Data == null ) )
173
175
{
174
176
try
175
177
{
176
- mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data ) ;
178
+ await mTcpServer . SendAsync ( CurrentConnectionGuid , e . Data , start : 0 ) ;
177
179
mLogger . LogDebug ( "{data}" , e . Data ) ;
178
180
mIsOutputEnded = false ;
179
181
}
@@ -242,35 +244,91 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
242
244
243
245
#endregion
244
246
247
+
248
+ #region Private fields
249
+
250
+ private string mInterpreterPath = string . Empty ;
251
+ private string InterpreterPath
252
+ {
253
+ get { return mInterpreterPath ; }
254
+ set
255
+ {
256
+ if ( string . IsNullOrEmpty ( value ) )
257
+ return ;
258
+
259
+ if ( value . Equals ( mInterpreterPath ) )
260
+ return ;
261
+
262
+ mInterpreterPath = value ;
263
+ mLogger . LogDebug ( "New path for {name} register with values {value}" , nameof ( InterpreterPath ) , InterpreterPath ) ;
264
+ }
265
+ }
266
+
267
+ private string mWorkingDirrectoryPath = string . Empty ;
268
+ private string WorkingDirrectoryPath
269
+ {
270
+ get { return mWorkingDirrectoryPath ; }
271
+ set
272
+ {
273
+ if ( string . IsNullOrEmpty ( value ) )
274
+ return ;
275
+
276
+ if ( value . Equals ( mWorkingDirrectoryPath ) )
277
+ return ;
278
+
279
+ mWorkingDirrectoryPath = value ;
280
+ mLogger . LogDebug ( "New path for {name} register with values {value}" , nameof ( WorkingDirrectoryPath ) , WorkingDirrectoryPath ) ;
281
+ }
282
+ }
283
+
284
+ private string mSourceCodeSavePath = string . Empty ;
285
+ private string SourceCodeSavePath
286
+ {
287
+ get { return mSourceCodeSavePath ; }
288
+ set
289
+ {
290
+ if ( string . IsNullOrEmpty ( value ) )
291
+ return ;
292
+
293
+ if ( value . Equals ( mSourceCodeSavePath ) )
294
+ return ;
295
+
296
+ mSourceCodeSavePath = value ;
297
+ mLogger . LogDebug ( "New path for {name} register with values {value}" , nameof ( SourceCodeSavePath ) , SourceCodeSavePath ) ;
298
+ }
299
+
300
+ }
301
+
302
+ #endregion
245
303
#region Private methods
246
304
247
- private void SetPath ( )
305
+ private void SetPath ( AppSettings appSettings )
248
306
{
249
307
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
250
308
{
251
- SourceCodeSavePaths = mAppSettingService . SourceCodeSavePaths . Windows ;
252
- InterpreterPath = mAppSettingService . PythonPaths . InterpreterPath . Windows ;
253
- WorkingDirrectoryPath = mAppSettingService . PythonPaths . WorkingDirrectoryPath . Windows ;
309
+ SourceCodeSavePath = appSettings . SourceCodeSavePath . Windows ;
310
+ InterpreterPath = appSettings . PythonPaths . InterpreterPath . Windows ;
311
+ WorkingDirrectoryPath = appSettings . PythonPaths . WorkingDirrectoryPath . Windows ;
254
312
return ;
255
313
}
256
314
257
- SourceCodeSavePaths = mAppSettingService . SourceCodeSavePaths . Linux ;
258
- InterpreterPath = mAppSettingService . PythonPaths . InterpreterPath . Linux ;
259
- WorkingDirrectoryPath = mAppSettingService . PythonPaths . WorkingDirrectoryPath . Linux ;
315
+ SourceCodeSavePath = appSettings . SourceCodeSavePath . Linux ;
316
+ InterpreterPath = appSettings . PythonPaths . InterpreterPath . Linux ;
317
+ WorkingDirrectoryPath = appSettings . PythonPaths . WorkingDirrectoryPath . Linux ;
260
318
}
261
319
262
320
private void SaveCodeAndStartProcess ( string code , bool withDebug )
263
321
{
264
- File . WriteAllText ( SourceCodeSavePaths , code ) ;
322
+ File . WriteAllText ( SourceCodeSavePath , code ) ;
265
323
Task . Run ( ( ) => StartProcess ( withDebug : withDebug ) ) ;
266
324
}
267
325
268
326
private void StartProcess ( bool withDebug = false )
269
327
{
270
- string arg = string . Format ( $ "-u -m { Path . GetFileNameWithoutExtension ( SourceCodeSavePaths ) } ") ;
328
+ string arg = string . Format ( $ "-u -m { Path . GetFileNameWithoutExtension ( SourceCodeSavePath ) } ") ;
271
329
272
330
if ( withDebug )
273
- arg = string . Format ( $ "-u -m pdb { SourceCodeSavePaths } ") ;
331
+ arg = string . Format ( $ "-u -m pdb { SourceCodeSavePath } ") ;
274
332
275
333
ProcessStartInfo proccesInfo = new ( )
276
334
{
0 commit comments