-
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.
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Remotely.Server.Services | ||
{ | ||
public class CleanupService : IHostedService, IDisposable | ||
{ | ||
public CleanupService(IServiceProvider serviceProvider) | ||
{ | ||
Services = serviceProvider; | ||
} | ||
|
||
private IServiceProvider Services { get; } | ||
private System.Timers.Timer CleanupTimer { get; set; } | ||
|
||
public void Dispose() | ||
{ | ||
CleanupTimer?.Dispose(); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
CleanupTimer?.Dispose(); | ||
CleanupTimer = new System.Timers.Timer(TimeSpan.FromDays(1).TotalMilliseconds); | ||
CleanupTimer.Elapsed += CleanupTimer_Elapsed; | ||
CleanupTimer.Start(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
CleanupTimer?.Dispose(); | ||
return Task.CompletedTask; | ||
} | ||
|
||
private void CleanupTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) | ||
{ | ||
using (var scope = Services.CreateScope()) | ||
{ | ||
var dataService = scope.ServiceProvider.GetRequiredService<DataService>(); | ||
|
||
dataService.CleanupOldRecords(); | ||
} | ||
} | ||
} | ||
} |
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