|
16 | 16 | usingCoder.Desktop.MutagenSdk.Proto.Url;
|
17 | 17 | usingCoder.Desktop.Vpn.Utilities;
|
18 | 18 | usingGrpc.Core;
|
19 |
| -usingMicrosoft.Extensions.Options; |
| 19 | +usingMicrosoft.Extensions.Hosting; |
20 | 20 | usingMicrosoft.Extensions.Logging;
|
| 21 | +usingMicrosoft.Extensions.Options; |
21 | 22 | usingSerilog;
|
22 | 23 | usingDaemonTerminateRequest=Coder.Desktop.MutagenSdk.Proto.Service.Daemon.TerminateRequest;
|
23 | 24 | usingMutagenProtocol=Coder.Desktop.MutagenSdk.Proto.Url.Protocol;
|
24 | 25 | usingSynchronizationTerminateRequest=Coder.Desktop.MutagenSdk.Proto.Service.Synchronization.TerminateRequest;
|
25 |
| -usingMicrosoft.Extensions.Hosting; |
26 | 26 |
|
27 | 27 | namespaceCoder.Desktop.App.Services;
|
28 | 28 |
|
@@ -556,25 +556,62 @@ private void StartDaemonProcess()
|
556 | 556 | varlogPath=Path.Combine(_mutagenDataDirectory,"daemon.log");
|
557 | 557 | varlogStream=newStreamWriter(logPath,true);
|
558 | 558 |
|
559 |
| -_daemonProcess=newProcess(); |
560 |
| -_daemonProcess.StartInfo.FileName=_mutagenExecutablePath; |
561 |
| -_daemonProcess.StartInfo.Arguments="daemon run"; |
562 |
| -_daemonProcess.StartInfo.Environment.Add("MUTAGEN_DATA_DIRECTORY",_mutagenDataDirectory); |
563 |
| -_daemonProcess.StartInfo.Environment.Add("MUTAGEN_SSH_CONFIG_PATH","none");// do not use ~/.ssh/config |
| 559 | +_logger.LogInformation("starting mutagen daemon process with executable path '{path}'",_mutagenExecutablePath); |
| 560 | +_logger.LogInformation("mutagen data directory '{path}'",_mutagenDataDirectory); |
| 561 | +_logger.LogInformation("mutagen daemon log path '{path}'",logPath); |
| 562 | + |
| 563 | +vardaemonProcess=newProcess(); |
| 564 | +daemonProcess.StartInfo.FileName=_mutagenExecutablePath; |
| 565 | +daemonProcess.StartInfo.Arguments="daemon run"; |
| 566 | +daemonProcess.StartInfo.Environment.Add("MUTAGEN_DATA_DIRECTORY",_mutagenDataDirectory); |
| 567 | +daemonProcess.StartInfo.Environment.Add("MUTAGEN_SSH_CONFIG_PATH","none");// do not use ~/.ssh/config |
564 | 568 | // hide the console window
|
565 |
| -_daemonProcess.StartInfo.CreateNoWindow=true; |
| 569 | +daemonProcess.StartInfo.CreateNoWindow=true; |
566 | 570 | // shell needs to be disabled since we set the environment
|
567 | 571 | // https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.environment?view=net-8.0
|
568 |
| -_daemonProcess.StartInfo.UseShellExecute=false; |
569 |
| -_daemonProcess.StartInfo.RedirectStandardError=true; |
570 |
| -_daemonProcess.EnableRaisingEvents=true; |
571 |
| -_daemonProcess.Exited+=(object?sender,EventArgse)=> |
| 572 | +daemonProcess.StartInfo.UseShellExecute=false; |
| 573 | +daemonProcess.StartInfo.RedirectStandardError=true; |
| 574 | +daemonProcess.EnableRaisingEvents=true; |
| 575 | +daemonProcess.Exited+=(_,_)=> |
572 | 576 | {
|
573 |
| -_logger.LogInformation("mutagen daemon exited with code {exitCode}",_daemonProcess?.ExitCode); |
| 577 | +varexitCode=-1; |
| 578 | +try |
| 579 | +{ |
| 580 | +// ReSharper disable once AccessToDisposedClosure |
| 581 | +exitCode=daemonProcess.ExitCode; |
| 582 | +} |
| 583 | +catch |
| 584 | +{ |
| 585 | +// ignored |
| 586 | +} |
| 587 | + |
| 588 | +_logger.LogInformation("mutagen daemon exited with code {exitCode}",exitCode); |
574 | 589 | };
|
575 |
| -if(!_daemonProcess.Start()) |
576 |
| -thrownewInvalidOperationException("Failed to start mutagen daemon process, Start returned false"); |
577 | 590 |
|
| 591 | +try |
| 592 | +{ |
| 593 | +if(!daemonProcess.Start()) |
| 594 | +thrownewInvalidOperationException("Failed to start mutagen daemon process, Start returned false"); |
| 595 | +} |
| 596 | +catch(Exceptione) |
| 597 | +{ |
| 598 | +_logger.LogWarning(e,"mutagen daemon failed to start"); |
| 599 | + |
| 600 | +logStream.Dispose(); |
| 601 | +try |
| 602 | +{ |
| 603 | +daemonProcess.Kill(); |
| 604 | +} |
| 605 | +catch |
| 606 | +{ |
| 607 | +// ignored, the process likely doesn't exist |
| 608 | +} |
| 609 | + |
| 610 | +daemonProcess.Dispose(); |
| 611 | +throw; |
| 612 | +} |
| 613 | + |
| 614 | +_daemonProcess=daemonProcess; |
578 | 615 | varwriter=newLogWriter(_daemonProcess.StandardError,logStream);
|
579 | 616 | Task.Run(()=>{_=writer.Run();});
|
580 | 617 | _logWriter=writer;
|
|