-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDataRefresherStatusService.cs
More file actions
59 lines (51 loc) · 1.66 KB
/
DataRefresherStatusService.cs
File metadata and controls
59 lines (51 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Threading;
using System.Threading.Tasks;
namespace BookingSystem.AspNetCore.Services
{
/// <summary>
/// A service which tracks the status of the data refresher, including
/// whether it is configured to run and whether a cycle has completed.
/// </summary>
public class DataRefresherStatusService
{
private readonly SemaphoreSlim _completionSemaphore = new SemaphoreSlim(0, 1);
private bool _isRefresherConfigured = false;
private bool _hasCompletedCycle = false;
public void SetRefresherConfigured(bool isConfigured)
{
_isRefresherConfigured = isConfigured;
}
public bool IsRefresherConfigured()
{
return _isRefresherConfigured;
}
public void SignalCycleCompletion()
{
_hasCompletedCycle = true;
// Release the semaphore if someone is waiting on it
if (_completionSemaphore.CurrentCount == 0)
{
_completionSemaphore.Release();
}
}
/// <summary>
/// Has the data refresher completed a cycle?
///
/// This makes it possible to write scripts (for CI) which don't start
/// until the data refresher has completed at least one cycle.
/// </summary>
public bool HasCompletedCycle()
{
return _hasCompletedCycle;
}
public async Task WaitForCycleCompletion(TimeSpan timeout)
{
if (_hasCompletedCycle)
{
return;
}
await _completionSemaphore.WaitAsync(timeout);
}
}
}