-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update DataCleanupService to also remove expired recordings.
- Loading branch information
Showing
3 changed files
with
86 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,113 @@ | ||
using Microsoft.Build.Framework; | ||
using Immense.RemoteControl.Shared.Services; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Identity.Client; | ||
using Remotely.Server.Services.RcImplementations; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
|
||
namespace Remotely.Server.Services | ||
{ | ||
public class DataCleanupService : IHostedService, IDisposable | ||
public class DataCleanupService : BackgroundService, IDisposable | ||
{ | ||
private readonly ILogger<DataCleanupService> _logger; | ||
|
||
private readonly IServiceProvider _services; | ||
|
||
private System.Timers.Timer _cleanupTimer = new(TimeSpan.FromDays(1)); | ||
|
||
private readonly IServiceScopeFactory _scopeFactory; | ||
private readonly ISystemTime _systemTime; | ||
private readonly IApplicationConfig _appConfig; | ||
|
||
public DataCleanupService( | ||
IServiceProvider serviceProvider, | ||
IServiceScopeFactory scopeFactory, | ||
ISystemTime systemTime, | ||
IApplicationConfig appConfig, | ||
ILogger<DataCleanupService> logger) | ||
{ | ||
_services = serviceProvider; | ||
_scopeFactory = scopeFactory; | ||
_systemTime = systemTime; | ||
_appConfig = appConfig; | ||
_logger = logger; | ||
|
||
_cleanupTimer.Elapsed += CleanupTimer_Elapsed; | ||
} | ||
public void Dispose() | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
_cleanupTimer?.Dispose(); | ||
GC.SuppressFinalize(this); | ||
await Task.Yield(); | ||
|
||
await PerformCleanup(); | ||
|
||
using var timer = new PeriodicTimer(TimeSpan.FromDays(1)); | ||
|
||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
try | ||
{ | ||
_ = await timer.WaitForNextTickAsync(stoppingToken); | ||
await PerformCleanup(); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
_logger.LogInformation("Application is shutting down. Stopping data cleanup service."); | ||
} | ||
|
||
} | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
private async Task PerformCleanup() | ||
{ | ||
_cleanupTimer.Start(); | ||
return Task.CompletedTask; | ||
try | ||
{ | ||
await RemoveExpiredDbRecords(); | ||
await RemoveExpiredRecordings(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error during data cleanup."); | ||
} | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
private async Task RemoveExpiredDbRecords() | ||
{ | ||
_cleanupTimer.Stop(); | ||
_cleanupTimer.Dispose(); | ||
return Task.CompletedTask; | ||
using var scope = _scopeFactory.CreateScope(); | ||
var dataService = scope.ServiceProvider.GetRequiredService<IDataService>(); | ||
await dataService.CleanupOldRecords(); | ||
} | ||
|
||
private void CleanupTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) | ||
private Task RemoveExpiredRecordings() | ||
{ | ||
try | ||
if (!Directory.Exists(SessionRecordingSink.RecordingsDirectory)) | ||
{ | ||
using var scope = _services.CreateScope(); | ||
var dataService = scope.ServiceProvider.GetRequiredService<IDataService>(); | ||
dataService.CleanupOldRecords(); | ||
return Task.CompletedTask; | ||
} | ||
catch (Exception ex) | ||
|
||
var expirationDate = _systemTime.Now.UtcDateTime - TimeSpan.FromDays(_appConfig.DataRetentionInDays); | ||
|
||
var files = Directory | ||
.GetFiles( | ||
SessionRecordingSink.RecordingsDirectory, | ||
"*.webm", | ||
SearchOption.AllDirectories) | ||
.Select(x => new FileInfo(x)) | ||
.Where(x => x.CreationTimeUtc < expirationDate) | ||
.ToList(); | ||
|
||
foreach (var file in files) | ||
{ | ||
_logger.LogError(ex, "Error during data cleanup."); | ||
try | ||
{ | ||
file.Delete(); | ||
_logger.LogInformation("Expired recording deleted: {file}", file.FullName); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error while deleting expired recording: {file}", file); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters