1
- using Microsoft . Extensions . Options ;
2
1
using NTorSpectator . Observer . TorIntegration ;
3
2
using NTorSpectator . Services ;
4
3
using NTorSpectator . Services . Models ;
4
+ using Quartz ;
5
5
6
6
namespace NTorSpectator . Observer . Services ;
7
7
8
- public class Spectator : BackgroundService
8
+ public class SpectatorJob : IJob
9
9
{
10
- private readonly ILogger < Spectator > _logger ;
11
- private readonly TorControlManager _torControl ;
12
- private readonly string _torSitesFile ;
13
- private readonly TimeSpan _cooldown ;
10
+ private readonly ILogger < SpectatorJob > _logger ;
14
11
private readonly ISitesCatalogue _sitesCatalogue ;
12
+ private readonly TorControlManager _torControl ;
15
13
private readonly ISiteObserver _siteObserver ;
16
14
17
- public Spectator ( ILogger < Spectator > logger , TorControlManager torControl , IOptions < SpectatorSettings > opts , ISitesCatalogue sitesCatalogue , ISiteObserver siteObserver )
15
+ public SpectatorJob ( ILogger < SpectatorJob > logger , ISitesCatalogue sitesCatalogue , TorControlManager torControl , ISiteObserver siteObserver )
18
16
{
19
17
_logger = logger ;
20
- _torControl = torControl ;
21
18
_sitesCatalogue = sitesCatalogue ;
19
+ _torControl = torControl ;
22
20
_siteObserver = siteObserver ;
23
- _torSitesFile = opts . Value . SiteList ;
24
- _cooldown = opts . Value . CooldownInterval ;
25
- }
26
-
27
- public bool IsRunning { get ; private set ; }
28
- protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
29
- {
30
- try
31
- {
32
- IsRunning = true ;
33
- await UpdateSites ( ) ;
34
- while ( ! stoppingToken . IsCancellationRequested )
35
- {
36
- await Watch ( ) ;
37
- await Task . Delay ( _cooldown , stoppingToken ) ;
38
- }
39
- }
40
- finally
41
- {
42
- IsRunning = false ;
43
- }
44
21
}
45
22
46
- private async Task Watch ( )
23
+ public async Task Execute ( IJobExecutionContext context )
47
24
{
25
+ _logger . LogDebug ( "Starting sites observations" ) ;
48
26
var sites = await _sitesCatalogue . GetAllSites ( ) ;
49
- var siteQueue = new Queue < QueuedSite > ( sites . Select ( x => new QueuedSite ( x , 0 ) ) ) ;
27
+ _logger . LogDebug ( "Got {Count} sites to observe" , sites . Count ) ;
50
28
29
+ var siteQueue = new Queue < QueuedSite > ( sites . Select ( x => new QueuedSite ( x , 0 ) ) ) ;
51
30
while ( siteQueue . TryDequeue ( out var queuedSite ) )
52
31
{
53
32
using var _ = _logger . BeginScope ( new Dictionary < string , object > { { "HiddenService" , queuedSite . Site . SiteUri } } ) ;
33
+ _logger . LogDebug ( "Starting observations on the next site" ) ;
54
34
try
55
35
{
56
36
var observations = await ObserveSite ( queuedSite . Site . SiteUri ) ;
@@ -65,6 +45,7 @@ private async Task Watch()
65
45
continue ;
66
46
}
67
47
}
48
+ _logger . LogDebug ( "Site seems to be up" ) ;
68
49
await _siteObserver . AddNewObservation ( queuedSite . Site . SiteUri , observations . IsOk ) ;
69
50
_logger . LogInformation ( "Site observed" ) ;
70
51
}
@@ -73,28 +54,16 @@ private async Task Watch()
73
54
_logger . LogError ( e , "Observation for site failed" ) ;
74
55
}
75
56
}
57
+ _logger . LogDebug ( "The queue is finally empty, observations finished" ) ;
76
58
}
77
-
78
- private async Task UpdateSites ( )
79
- {
80
- var sites = await File . ReadAllLinesAsync ( _torSitesFile ) ;
81
- foreach ( var site in sites )
82
- {
83
- await _sitesCatalogue . AddIfNotExists ( site ) ;
84
- }
85
- }
59
+
60
+ private record QueuedSite ( Site Site , int ObservationsCount ) ;
61
+
86
62
private async Task < TorWatchResults > ObserveSite ( string site )
87
63
{
88
64
var torReply = await _torControl . HsFetch ( site ) ;
89
65
var positive = torReply . Count ( x => x . Action == HsDescAction . Received ) ;
90
66
var negative = torReply . Count ( x => x . Action == HsDescAction . Failed ) ;
91
67
return new ( site , positive , negative ) ;
92
68
}
93
-
94
- private record QueuedSite ( Site Site , int ObservationsCount ) ;
95
- }
96
-
97
- public record TorWatchResults ( string Site , int Positive , int Negative )
98
- {
99
- public bool IsOk => Positive > 0 ;
100
69
}
0 commit comments