Skip to content

Commit 9c2e5c2

Browse files
committed
background task only runs once and loads sites, if they are available #21
1 parent d4c3ef5 commit 9c2e5c2

File tree

2 files changed

+36
-100
lines changed

2 files changed

+36
-100
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Extensions.Options;
2+
using NTorSpectator.Services;
3+
4+
namespace NTorSpectator.Observer.Services;
5+
6+
public class SitesUpdater : BackgroundService
7+
{
8+
private readonly ILogger<SitesUpdater> _logger;
9+
private readonly string _torSitesFile;
10+
private readonly ISitesCatalogue _sitesCatalogue;
11+
12+
public SitesUpdater(ILogger<SitesUpdater> logger, IOptions<SpectatorSettings> opts, ISitesCatalogue sitesCatalogue)
13+
{
14+
_logger = logger;
15+
_sitesCatalogue = sitesCatalogue;
16+
_torSitesFile = opts.Value.SiteList;
17+
}
18+
19+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
20+
{
21+
if (!File.Exists(_torSitesFile))
22+
{
23+
_logger.LogWarning("Tor sites file is not found at path {TorSitesFilePath}. Exiting", _torSitesFile);
24+
return;
25+
}
26+
27+
var sites = await File.ReadAllLinesAsync(_torSitesFile);
28+
foreach (var site in sites)
29+
{
30+
if (string.IsNullOrWhiteSpace(site))
31+
continue;
32+
await _sitesCatalogue.AddIfNotExists(site);
33+
}
34+
_logger.LogDebug("Finished adding sites");
35+
}
36+
}

src/NTorSpectator.Observer/Services/Spectator.cs

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)