Skip to content

Commit

Permalink
Add cancellation token to FileLogsManager methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Jul 31, 2023
1 parent 336610c commit 7a362c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
10 changes: 7 additions & 3 deletions Agent/Services/AgentHubConnection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Immense.RemoteControl.Desktop.Native.Windows;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Remotely.Agent.Extensions;
using Remotely.Agent.Interfaces;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class AgentHubConnection : IAgentHubConnection, IDisposable
private readonly IWakeOnLanService _wakeOnLanService;
private readonly ILogger<AgentHubConnection> _logger;
private readonly IFileLogsManager _fileLogsManager;
private readonly IHostApplicationLifetime _appLifetime;
private readonly IScriptExecutor _scriptExecutor;
private readonly IUninstaller _uninstaller;
private readonly IUpdater _updater;
Expand All @@ -60,6 +62,7 @@ public AgentHubConnection(
IHttpClientFactory httpFactory,
IWakeOnLanService wakeOnLanService,
IFileLogsManager fileLogsManager,
IHostApplicationLifetime appLifetime,
ILogger<AgentHubConnection> logger)
{
_configService = configService;
Expand All @@ -73,6 +76,7 @@ public AgentHubConnection(
_wakeOnLanService = wakeOnLanService;
_logger = logger;
_fileLogsManager = fileLogsManager;
_appLifetime = appLifetime;
}

public bool IsConnected => _hubConnection?.State == HubConnectionState.Connected;
Expand Down Expand Up @@ -302,7 +306,7 @@ private void RegisterMessageHandlers()

_hubConnection.On("DeleteLogs", () =>
{
_fileLogsManager.DeleteLogs();
_fileLogsManager.DeleteLogs(_appLifetime.ApplicationStopping);
});


Expand Down Expand Up @@ -367,14 +371,14 @@ private void RegisterMessageHandlers()
{
try
{
if (!await _fileLogsManager.AnyLogsExist())
if (!await _fileLogsManager.AnyLogsExist(_appLifetime.ApplicationStopping))
{
var message = "There are no log entries written.";
await _hubConnection.InvokeAsync("SendLogs", message, senderConnectionId).ConfigureAwait(false);
return;
}

await foreach (var chunk in _fileLogsManager.ReadAllBytes())
await foreach (var chunk in _fileLogsManager.ReadAllBytes(_appLifetime.ApplicationStopping))
{
var lines = Encoding.UTF8.GetString(chunk);
await _hubConnection.InvokeAsync("SendLogs", lines, senderConnectionId).ConfigureAwait(false);
Expand Down
33 changes: 24 additions & 9 deletions Agent/Services/FileLogsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Remotely.Shared.Services;

public interface IFileLogsManager
{
Task<bool> AnyLogsExist();
Task DeleteLogs();
IAsyncEnumerable<byte[]> ReadAllBytes();
Task<bool> AnyLogsExist(CancellationToken cancellationToken);
Task DeleteLogs(CancellationToken cancellationToken);
IAsyncEnumerable<byte[]> ReadAllBytes(CancellationToken cancellationToken);
}

public class FileLogsManager : IFileLogsManager
{
public async Task<bool> AnyLogsExist()
public async Task<bool> AnyLogsExist(CancellationToken cancellationToken)
{
using var logLock = await FileLoggerDefaults.AcquireLock();
using var logLock = await FileLoggerDefaults.AcquireLock(cancellationToken);

var componentName = Assembly.GetExecutingAssembly().GetName().Name;
var directory = Path.Combine(FileLoggerDefaults.LogsFolderPath, $"{componentName}");
Expand All @@ -29,6 +31,11 @@ public async Task<bool> AnyLogsExist()
{
foreach (var file in Directory.GetFiles(directory))
{
if (cancellationToken.IsCancellationRequested)
{
break;
}

if (new FileInfo(file).Length > 0)
{
return true;
Expand All @@ -38,9 +45,9 @@ public async Task<bool> AnyLogsExist()
return false;
}

public async Task DeleteLogs()
public async Task DeleteLogs(CancellationToken cancellationToken)
{
using var logLock = await FileLoggerDefaults.AcquireLock();
using var logLock = await FileLoggerDefaults.AcquireLock(cancellationToken);

var componentName = Assembly.GetExecutingAssembly().GetName().Name;
var directory = Path.Combine(FileLoggerDefaults.LogsFolderPath, $"{componentName}");
Expand All @@ -49,6 +56,10 @@ public async Task DeleteLogs()
{
foreach (var file in Directory.GetFiles(directory))
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
try
{
File.Delete(file);
Expand All @@ -58,9 +69,9 @@ public async Task DeleteLogs()
}
}

public async IAsyncEnumerable<byte[]> ReadAllBytes()
public async IAsyncEnumerable<byte[]> ReadAllBytes([EnumeratorCancellation] CancellationToken cancellationToken)
{
using var logLock = await FileLoggerDefaults.AcquireLock();
using var logLock = await FileLoggerDefaults.AcquireLock(cancellationToken);

var componentName = Assembly.GetExecutingAssembly().GetName().Name;
var directory = Path.Combine(FileLoggerDefaults.LogsFolderPath, $"{componentName}");
Expand All @@ -78,6 +89,10 @@ public async IAsyncEnumerable<byte[]> ReadAllBytes()
{
foreach (var chunk in File.ReadAllBytes(file).Chunk(50_000))
{
if (cancellationToken.IsCancellationRequested)
{
yield break;
}
yield return File.ReadAllBytes(file);
}
}
Expand Down

0 comments on commit 7a362c9

Please sign in to comment.