Skip to content

Commit 8653c81

Browse files
committed
added file check task
1 parent 7817ea8 commit 8653c81

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

Web.Server/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void Main(string[] args)
2626
builder.Services.AddHostedService<AppReleasesTask>();
2727
builder.Services.AddHostedService<PortsReleasesTask>();
2828
builder.Services.AddHostedService<ToolsReleasesTask>();
29+
builder.Services.AddHostedService<FileCheckTask>();
2930
}
3031

3132
builder.Services.AddSingleton<AppReleasesProvider>();

Web.Server/Tasks/FileCheckTask.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Web.Server.Helpers;
2+
3+
namespace Web.Server.Tasks;
4+
5+
public sealed class FileCheckTask : IHostedService, IDisposable
6+
{
7+
private readonly ILogger<AppReleasesTask> _logger;
8+
private readonly DatabaseContextFactory _dbContextFactory;
9+
private readonly HttpClient _httpClient;
10+
11+
private Timer _timer;
12+
13+
public FileCheckTask(
14+
ILogger<AppReleasesTask> logger,
15+
DatabaseContextFactory dbContextFactory,
16+
HttpClient httpClient
17+
)
18+
{
19+
_logger = logger;
20+
_dbContextFactory = dbContextFactory;
21+
_httpClient = httpClient;
22+
}
23+
24+
public Task StartAsync(CancellationToken stoppingToken)
25+
{
26+
_timer = new Timer(
27+
DoWork,
28+
null,
29+
TimeSpan.Zero,
30+
TimeSpan.FromHours(6)
31+
);
32+
33+
return Task.CompletedTask;
34+
}
35+
36+
private void DoWork(object? state)
37+
{
38+
_logger.LogInformation("File check started");
39+
40+
using var dbContext = _dbContextFactory.Get();
41+
var files = dbContext.Versions.Select(v => v.DownloadUrl).ToList();
42+
43+
foreach (var file in files)
44+
{
45+
var result = _httpClient.GetAsync(file, HttpCompletionOption.ResponseHeadersRead).Result;
46+
47+
if (result is null || !result.IsSuccessStatusCode)
48+
{
49+
_logger.LogError($"File doesn't exist or unavailable: {file}");
50+
continue;
51+
}
52+
}
53+
54+
_logger.LogInformation("File check ended");
55+
}
56+
57+
public Task StopAsync(CancellationToken stoppingToken)
58+
{
59+
_timer.Change(Timeout.Infinite, 0);
60+
61+
return Task.CompletedTask;
62+
}
63+
64+
public void Dispose()
65+
{
66+
_timer.Dispose();
67+
}
68+
}

0 commit comments

Comments
 (0)