Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb84a4ed

Browse files
authored
chore: add logging to mutagen controller (#79)
Adds some logging to the mutagen controller.
1 parent7ca3af3 commitb84a4ed

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

‎App/Services/MutagenController.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
usingCoder.Desktop.Vpn.Utilities;
1818
usingGrpc.Core;
1919
usingMicrosoft.Extensions.Options;
20+
usingMicrosoft.Extensions.Logging;
21+
usingSerilog;
2022
usingDaemonTerminateRequest=Coder.Desktop.MutagenSdk.Proto.Service.Daemon.TerminateRequest;
2123
usingMutagenProtocol=Coder.Desktop.MutagenSdk.Proto.Url.Protocol;
2224
usingSynchronizationTerminateRequest=Coder.Desktop.MutagenSdk.Proto.Service.Synchronization.TerminateRequest;
25+
usingMicrosoft.Extensions.Hosting;
2326

2427
namespaceCoder.Desktop.App.Services;
2528

@@ -113,6 +116,8 @@ public sealed class MutagenController : ISyncSessionController
113116
// Protects all private non-readonly class members.
114117
privatereadonlyRaiiSemaphoreSlim_lock=new(1,1);
115118

119+
privatereadonlyILogger<MutagenController>_logger;
120+
116121
privatereadonlyCancellationTokenSource_stateUpdateCts=new();
117122
privateTask?_stateUpdateTask;
118123

@@ -142,15 +147,19 @@ public sealed class MutagenController : ISyncSessionController
142147

143148
privatestringMutagenDaemonLog=>Path.Combine(_mutagenDataDirectory,"daemon.log");
144149

145-
publicMutagenController(IOptions<MutagenControllerConfig>config)
150+
publicMutagenController(IOptions<MutagenControllerConfig>config,ILogger<MutagenController>logger)
146151
{
147152
_mutagenExecutablePath=config.Value.MutagenExecutablePath;
153+
_logger=logger;
148154
}
149155

150156
publicMutagenController(stringexecutablePath,stringdataDirectory)
151157
{
152158
_mutagenExecutablePath=executablePath;
153159
_mutagenDataDirectory=dataDirectory;
160+
varbuilder=Host.CreateApplicationBuilder();
161+
builder.Services.AddSerilog();
162+
_logger=(ILogger<MutagenController>)builder.Build().Services.GetService(typeof(ILogger<MutagenController>))!;
154163
}
155164

156165
publiceventEventHandler<SyncSessionControllerStateModel>?StateChanged;
@@ -447,9 +456,9 @@ private async Task<MutagenClient> EnsureDaemon(CancellationToken ct)
447456
{
448457
awaitStopDaemon(cts.Token);
449458
}
450-
catch
459+
catch(ExceptionstopEx)
451460
{
452-
// ignored
461+
_logger.LogError(stopEx,"failed to stop daemon");
453462
}
454463

455464
ReplaceState(newSyncSessionControllerStateModel
@@ -501,6 +510,8 @@ private async Task<MutagenClient> StartDaemon(CancellationToken ct)
501510
}
502511
catch(Exceptione)when(eis notOperationCanceledException)
503512
{
513+
_logger.LogWarning(e,"failed to start daemon process, attempt {attempt} of {maxAttempts}",attempts,
514+
maxAttempts);
504515
if(attempts==maxAttempts)
505516
throw;
506517
// back off a little and try again.
@@ -556,8 +567,11 @@ private void StartDaemonProcess()
556567
// https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.environment?view=net-8.0
557568
_daemonProcess.StartInfo.UseShellExecute=false;
558569
_daemonProcess.StartInfo.RedirectStandardError=true;
559-
// TODO: log exited process
560-
// _daemonProcess.Exited += ...
570+
_daemonProcess.EnableRaisingEvents=true;
571+
_daemonProcess.Exited+=(object?sender,EventArgse)=>
572+
{
573+
_logger.LogInformation("mutagen daemon exited with code {exitCode}",_daemonProcess?.ExitCode);
574+
};
561575
if(!_daemonProcess.Start())
562576
thrownewInvalidOperationException("Failed to start mutagen daemon process, Start returned false");
563577

@@ -572,6 +586,7 @@ private void StartDaemonProcess()
572586
/// </summary>
573587
privateasyncTaskStopDaemon(CancellationTokenct)
574588
{
589+
_logger.LogDebug("stopping mutagen daemon");
575590
varprocess=_daemonProcess;
576591
varclient=_mutagenClient;
577592
varwriter=_logWriter;
@@ -584,28 +599,34 @@ private async Task StopDaemon(CancellationToken ct)
584599
if(client==null)
585600
{
586601
if(process==null)return;
602+
_logger.LogDebug("no client; killing daemon process");
587603
process.Kill(true);
588604
}
589605
else
590606
{
591607
try
592608
{
609+
_logger.LogDebug("sending DaemonTerminateRequest");
593610
awaitclient.Daemon.TerminateAsync(newDaemonTerminateRequest(),cancellationToken:ct);
594611
}
595-
catch
612+
catch(Exceptione)
596613
{
614+
_logger.LogError(e,"failed to gracefully terminate agent");
597615
if(process==null)return;
616+
_logger.LogDebug("killing daemon process after failed graceful termination");
598617
process.Kill(true);
599618
}
600619
}
601620

602621
if(process==null)return;
603622
varcts=CancellationTokenSource.CreateLinkedTokenSource(ct);
604623
cts.CancelAfter(TimeSpan.FromSeconds(5));
624+
_logger.LogDebug("waiting for process to exit");
605625
awaitprocess.WaitForExitAsync(cts.Token);
606626
}
607627
finally
608628
{
629+
_logger.LogDebug("cleaning up daemon process objects");
609630
client?.Dispose();
610631
process?.Dispose();
611632
writer?.Dispose();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp