-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e0bc1c
commit e9ca01b
Showing
6 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace FunderMaps.BatchNode | ||
{ | ||
/// <summary> | ||
/// Map bundle options. | ||
/// </summary> | ||
public sealed record MapBundleOptions | ||
{ | ||
/// <summary> | ||
/// Configuration section key. | ||
/// </summary> | ||
public const string Section = "MapBundle"; | ||
|
||
/// <summary> | ||
/// Batch service interval in hours. | ||
/// </summary> | ||
public int Interval { get; set; } | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
src/FunderMaps.BatchNode/BatchOptions.cs → src/FunderMaps.BatchNode/ModelOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
using FunderMaps.Core.Model; | ||
|
||
namespace FunderMaps.BatchNode | ||
{ | ||
/// <summary> | ||
/// Schedule tasks with interval. | ||
/// </summary> | ||
public class TimedModelService : IHostedService, IAsyncDisposable | ||
{ | ||
private readonly ModelOptions _options; | ||
|
||
private readonly IServiceProvider _servicesProvider; | ||
private readonly ILogger<TimedModelService> _logger; | ||
|
||
private Timer _timer; | ||
|
||
/// <summary> | ||
/// Create new instance. | ||
/// </summary> | ||
public TimedModelService(IServiceProvider serviceProvider, | ||
ILogger<TimedModelService> logger, | ||
IConfiguration configuration, | ||
IOptions<ModelOptions> options) | ||
{ | ||
_servicesProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); | ||
_logger = logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_options = options?.Value ?? throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
/// <summary> | ||
/// Triggered when the application host is ready to start the service. | ||
/// </summary> | ||
public Task StartAsync(CancellationToken stoppingToken) | ||
{ | ||
_timer = new(Worker, null, TimeSpan.Zero, TimeSpan.FromHours(_options.Interval)); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Run the scheduled tasks. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is the only situation in which an asynchronous method can be called | ||
/// without returning a future. We know for sure that this method was executed | ||
/// on a separate thread. | ||
/// </remarks> | ||
private async void Worker(object state) | ||
{ | ||
_logger.LogTrace("Timed worker is running."); | ||
|
||
using IServiceScope scope = _servicesProvider.CreateScope(); | ||
var modelService = scope.ServiceProvider.GetRequiredService<IModelService>(); | ||
|
||
await modelService.UpdateAllModelsAsync(); | ||
} | ||
|
||
/// <summary> | ||
/// Triggered when the application host is performing a graceful shutdown. | ||
/// </summary> | ||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_timer.Change(Timeout.Infinite, Timeout.Infinite); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting | ||
/// unmanaged resources. | ||
/// </summary> | ||
public ValueTask DisposeAsync() => _timer.DisposeAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters