Skip to content

Commit

Permalink
added file check task
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfds committed Jun 15, 2024
1 parent 7817ea8 commit 8653c81
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions Web.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void Main(string[] args)
builder.Services.AddHostedService<AppReleasesTask>();
builder.Services.AddHostedService<PortsReleasesTask>();
builder.Services.AddHostedService<ToolsReleasesTask>();
builder.Services.AddHostedService<FileCheckTask>();
}

builder.Services.AddSingleton<AppReleasesProvider>();
Expand Down
68 changes: 68 additions & 0 deletions Web.Server/Tasks/FileCheckTask.cs
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();
}
}

0 comments on commit 8653c81

Please sign in to comment.