Skip to content

Commit

Permalink
Refresh job
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickdewid committed Sep 27, 2021
1 parent 8e0bc1c commit e9ca01b
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 15 deletions.
18 changes: 18 additions & 0 deletions src/FunderMaps.BatchNode/MapBundleOptions.cs
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; }
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace FunderMaps.BatchNode
{
/// <summary>
/// Batch node options.
/// Model options.
/// </summary>
public sealed record BatchOptions
public sealed record ModelOptions
{
/// <summary>
/// Configuration section key.
/// </summary>
public const string Section = "Batch";
public const string Section = "Model";

/// <summary>
/// Batch service interval in hours.
Expand Down
8 changes: 6 additions & 2 deletions src/FunderMaps.BatchNode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
(Configuration, HostEnvironment) = services.BuildStartupProperties();

// Add the task scheduler.
services.Configure<BatchOptions>(Configuration.GetSection(BatchOptions.Section));
services.AddHostedService<TimedHostedService>();
services.Configure<MapBundleOptions>(Configuration.GetSection(MapBundleOptions.Section));
services.AddHostedService<TimedMapBundleService>();

// Add the task scheduler.
services.Configure<ModelOptions>(Configuration.GetSection(ModelOptions.Section));
services.AddHostedService<TimedModelService>();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ namespace FunderMaps.BatchNode
/// <summary>
/// Schedule tasks with interval.
/// </summary>
public class TimedHostedService : IHostedService, IAsyncDisposable
public class TimedMapBundleService : IHostedService, IAsyncDisposable
{
private readonly BatchOptions _options;
private readonly MapBundleOptions _options;

private readonly IServiceProvider _servicesProvider;
private readonly ILogger<TimedHostedService> _logger;
private readonly ILogger<TimedMapBundleService> _logger;

private Timer _timer;

/// <summary>
/// Create new instance.
/// </summary>
public TimedHostedService(IServiceProvider serviceProvider,
ILogger<TimedHostedService> logger,
public TimedMapBundleService(IServiceProvider serviceProvider,
ILogger<TimedMapBundleService> logger,
IConfiguration configuration,
IOptions<BatchOptions> options)
IOptions<MapBundleOptions> options)
{
_servicesProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_logger = logger = logger ?? throw new ArgumentNullException(nameof(logger));
Expand Down
82 changes: 82 additions & 0 deletions src/FunderMaps.BatchNode/TimedModelService.cs
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();
}
}
5 changes: 1 addition & 4 deletions src/FunderMaps.Core/Model/ModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ public ModelService(BackgroundTaskDispatcher backgroundTaskDispatcher)
=> _backgroundTaskDispatcher = backgroundTaskDispatcher ?? throw new ArgumentNullException(nameof(backgroundTaskDispatcher));

/// <summary>
/// Send build candidates off to background worker.
/// Dispatch refresh job to background worker.
/// </summary>
// public async Task BuildAsync() => await _backgroundTaskDispatcher.EnqueueTaskAsync<ExportJob>();

public async Task UpdateAllModelsAsync()
{
// throw new NotImplementedException();
await _backgroundTaskDispatcher.EnqueueTaskAsync<RefreshJob>();
}
}
Expand Down

0 comments on commit e9ca01b

Please sign in to comment.