-
Notifications
You must be signed in to change notification settings - Fork 0
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
69 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
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,68 @@ | ||
using Web.Server.Helpers; | ||
|
||
namespace Web.Server.Tasks; | ||
|
||
public sealed class FileCheckTask : IHostedService, IDisposable | ||
{ | ||
private readonly ILogger<AppReleasesTask> _logger; | ||
private readonly DatabaseContextFactory _dbContextFactory; | ||
private readonly HttpClient _httpClient; | ||
|
||
private Timer _timer; | ||
|
||
public FileCheckTask( | ||
ILogger<AppReleasesTask> logger, | ||
DatabaseContextFactory dbContextFactory, | ||
HttpClient httpClient | ||
) | ||
{ | ||
_logger = logger; | ||
_dbContextFactory = dbContextFactory; | ||
_httpClient = httpClient; | ||
} | ||
|
||
public Task StartAsync(CancellationToken stoppingToken) | ||
{ | ||
_timer = new Timer( | ||
DoWork, | ||
null, | ||
TimeSpan.Zero, | ||
TimeSpan.FromHours(6) | ||
); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private void DoWork(object? state) | ||
{ | ||
_logger.LogInformation("File check started"); | ||
|
||
using var dbContext = _dbContextFactory.Get(); | ||
var files = dbContext.Versions.Select(v => v.DownloadUrl).ToList(); | ||
|
||
foreach (var file in files) | ||
{ | ||
var result = _httpClient.GetAsync(file, HttpCompletionOption.ResponseHeadersRead).Result; | ||
|
||
if (result is null || !result.IsSuccessStatusCode) | ||
{ | ||
_logger.LogError($"File doesn't exist or unavailable: {file}"); | ||
continue; | ||
} | ||
} | ||
|
||
_logger.LogInformation("File check ended"); | ||
} | ||
|
||
public Task StopAsync(CancellationToken stoppingToken) | ||
{ | ||
_timer.Change(Timeout.Infinite, 0); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_timer.Dispose(); | ||
} | ||
} |