33using System . Diagnostics ;
44using System . Diagnostics . CodeAnalysis ;
55using System . Net ;
6+ using JetBrains . Annotations ;
67using JetBrains . Application . Threading ;
78using JetBrains . Collections . Viewable ;
89using JetBrains . Lifetimes ;
1112using JetBrains . Threading ;
1213using JetBrains . Util ;
1314using JetBrains . Util . Logging ;
15+ using UtBot . Rd ;
1416using UtBot . Rd . Generated ;
1517
1618namespace UtBot ;
@@ -20,14 +22,14 @@ public class ProcessWithRdServer
2022{
2123 public Lifetime Lifetime => _ldef . Lifetime ;
2224 public Protocol Protocol ;
23- public VSharpModel VSharpModel { get ; private set ; }
24-
25+ [ CanBeNull ] public VSharpModel VSharpModel { get ; private set ; }
26+
2527 private readonly LifetimeDefinition _ldef ;
26- private Process _process ;
27- private ILogger _logger = Logger . GetLogger < ProcessWithRdServer > ( ) ;
28+ [ CanBeNull ] private Process _process ;
2829
29- public ProcessWithRdServer ( string name , string workingDir , int port , string exePath , IShellLocks shellLocks , Lifetime ? parent = null )
30+ public ProcessWithRdServer ( string name , string workingDir , int port , string exePath , IShellLocks shellLocks , Lifetime ? parent = null , [ CanBeNull ] ILogger logger = null )
3031 {
32+ logger ??= Logger . GetLogger < ProcessWithRdServer > ( ) ;
3133 using var blockingCollection = new BlockingCollection < String > ( 2 ) ;
3234 shellLocks . AssertNonMainThread ( ) ;
3335 _ldef = ( parent ?? Lifetime . Eternal ) . CreateNested ( ) ;
@@ -41,9 +43,11 @@ public ProcessWithRdServer(string name, string workingDir, int port, string exeP
4143 var wire = new SocketWire . Server ( Lifetime , scheduler , socket ) ;
4244 var serializers = new Serializers ( ) ;
4345 var identities = new Identities ( IdKind . Server ) ;
44- var startInfo = new ProcessStartInfo ( "dotnet" , $ "\" { exePath } \" { port } ") ;
45-
46- startInfo . WorkingDirectory = workingDir ;
46+ var startInfo = new ProcessStartInfo ( "dotnet" , $ "\" { exePath } \" { port } ")
47+ {
48+ WorkingDirectory = workingDir
49+ } ;
50+
4751 Protocol = new Protocol ( name , serializers , identities , scheduler , wire , Lifetime ) ;
4852 scheduler . Queue ( ( ) =>
4953 {
@@ -55,6 +59,7 @@ public ProcessWithRdServer(string name, string workingDir, int port, string exeP
5559 blockingCollection . TryAdd ( s ) ;
5660 }
5761 } ) ;
62+ VSharpModel . Log . Advise ( Lifetime , s => logger . Info ( $ "V#: { s } ") ) ;
5863 } ) ;
5964 _process = new Process ( ) ;
6065 _process . StartInfo = startInfo ;
@@ -64,20 +69,26 @@ public ProcessWithRdServer(string name, string workingDir, int port, string exeP
6469 else
6570 _ldef . Terminate ( ) ;
6671 } ) ;
67- if ( _process ? . HasExited == false )
72+
73+ if ( _process ? . HasExited == true ) return ;
74+
75+ SpinWaitEx . SpinUntil ( pingLdef . Lifetime , ( ) =>
6876 {
69- SpinWaitEx . SpinUntil ( pingLdef . Lifetime , ( ) =>
77+ if ( _process ? . HasExited == true )
7078 {
71- VSharpModel ? . Ping . Fire ( "UtBot" ) ;
72- return blockingCollection . TryTake ( out _ ) ;
73- } ) ;
74- pingLdef . Terminate ( ) ;
75- }
79+ VSharpModel = null ;
80+ _ldef . Terminate ( ) ;
81+ }
82+
83+ VSharpModel ? . Ping . Fire ( RdUtil . MainProcessName ) ;
84+ return blockingCollection . TryTake ( out _ ) ;
85+ } ) ;
86+ pingLdef . Terminate ( ) ;
7687 }
7788 catch ( Exception )
7889 {
7990 _ldef . Terminate ( ) ;
8091 throw ;
8192 }
8293 }
83- }
94+ }
0 commit comments