-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathInitWaitController.cs
More file actions
76 lines (68 loc) · 2.87 KB
/
InitWaitController.cs
File metadata and controls
76 lines (68 loc) · 2.87 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Threading.Tasks;
using BookingSystem.AspNetCore.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
/// <summary>
/// Endpoints which are used to wait for components within
/// BookingSystem.AspNetCore to be initialized.
/// </summary>
namespace BookingSystem.AspNetCore.Controllers
{
[ApiController]
[Route("init-wait")]
public class InitWaitController : ControllerBase
{
private readonly DataRefresherStatusService _statusService;
private readonly ILogger<InitWaitController> _logger;
private static readonly TimeSpan _defaultTimeout = TimeSpan.FromMinutes(5);
public InitWaitController(
DataRefresherStatusService statusService,
ILogger<InitWaitController> logger)
{
_statusService = statusService;
_logger = logger;
}
/// <summary>
/// Wait for the data refresher to complete its first cycle.
///
/// This makes it possible to write scripts (for CI) which don't start
/// until the data refresher has completed at least one cycle.
///
/// - Returns 204 when the data refresher has completed its first cycle.
/// - Returns 503 if the data refresher is not configured to run.
/// - Returns 504 if the data refresher fails to complete a cycle within
/// the default timeout.
/// </summary>
[HttpGet("data-refresher")]
public async Task<IActionResult> WaitForDataRefresher()
{
_logger.LogDebug("Received request to wait for data refresher completion");
// Check if the data refresher is configured to run
if (!_statusService.IsRefresherConfigured())
{
_logger.LogWarning("Data refresher is not configured to run");
return StatusCode(503, "Data refresher service is not configured to run");
}
// If it has already completed a cycle, return immediately
if (_statusService.HasCompletedCycle())
{
_logger.LogDebug("Data refresher has already completed a cycle");
return NoContent();
}
_logger.LogDebug("Waiting for data refresher to complete a cycle...");
// Wait for the cycle to complete, with a timeout
await _statusService.WaitForCycleCompletion(_defaultTimeout);
if (_statusService.HasCompletedCycle())
{
_logger.LogDebug("Data refresher completed a cycle, returning 204");
return NoContent();
}
else
{
_logger.LogWarning("Timed out waiting for data refresher to complete a cycle");
return StatusCode(504, "Timed out waiting for data refresher to complete a cycle");
}
}
}
}