Skip to content

Commit

Permalink
Added options to configure machines allowed to run and global suppres…
Browse files Browse the repository at this point in the history
…sion of system machine setting
  • Loading branch information
MarquitosPT committed Oct 30, 2023
1 parent 8559775 commit 288241f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ public static class SchedulerServiceCollectionExtension
public static IServiceCollection AddSchedulerService(this IServiceCollection services)
{
services.AddHostedService<SchedulerService>();
services.AddSingleton(new SchedulerServiceOptions());

return services;
}

/// <summary>
/// Registers the background Scheduler service
/// </summary>
/// <param name="services">This Service Collection</param>
/// <param name="options">Scheduler service configuration options</param>
/// <returns></returns>
public static IServiceCollection AddSchedulerService(this IServiceCollection services, SchedulerServiceOptions options)
{
services.AddHostedService<SchedulerService>();
services.AddSingleton(options);

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static void UpdateFrom(this ScheduledTaskOptions options, ScheduledTaskOp
options.EndOn = sourceOptions.EndOn;
options.Schedule = sourceOptions.Schedule;
options.IsEnabled = sourceOptions.IsEnabled;
options.MachinesAllowedToRun = sourceOptions.MachinesAllowedToRun != null ?
new List<string>(sourceOptions.MachinesAllowedToRun) : new List<string>();
}
}
}
5 changes: 5 additions & 0 deletions src/Marquitos.Schedulers/ScheduledTaskOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ public class ScheduledTaskOptions
/// Indicates if the scheduled task is active and should run at given schedule.
/// </summary>
public bool IsEnabled { get; set; } = true;

/// <summary>
/// If provided, the service engine will only execute the scheduled Task on those machines
/// </summary>
public IEnumerable<string> MachinesAllowedToRun { get; set; } = new List<string>();
}
}
18 changes: 18 additions & 0 deletions src/Marquitos.Schedulers/SchedulerServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Marquitos.Schedulers
{
/// <summary>
/// SchedulerService Options
/// </summary>
public class SchedulerServiceOptions
{
/// <summary>
/// Suppress machine system enabled setting (SCHEDULER_SERVICE_ENABLED)
/// </summary>
public bool SuppressMachineSystemEnabledSetting { get; set; } = false;

/// <summary>
/// If provided, the service engine will only execute the scheduled Task on those machines
/// </summary>
public IEnumerable<string> MachinesAllowedToRun { get; set; } = new List<string>();
}
}
13 changes: 11 additions & 2 deletions src/Marquitos.Schedulers/Services/ScheduledTaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ public async Task InitializeAsync(CancellationToken cancellationToken = default)
{
NextRunTime = schedule.GetNextOccurrence(options.BeginOn, options.EndOn);
}

IsEnabled = options.IsEnabled;

if (options.MachinesAllowedToRun.Any())
{
var machineName = System.Environment.MachineName;

IsEnabled = options.IsEnabled && options.MachinesAllowedToRun.Contains(machineName);
}
else
{
IsEnabled = options.IsEnabled;
}

if (NextRunTime >= options.EndOn && DateTime.Now > options.EndOn)
{
Expand Down
38 changes: 34 additions & 4 deletions src/Marquitos.Schedulers/Services/SchedulerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ internal class SchedulerService : BackgroundService
{
private readonly ILogger<SchedulerService> _logger;
private readonly IEnumerable<IScheduledTaskService> _services;
private readonly SchedulerServiceOptions _options;

public SchedulerService(ILogger<SchedulerService> logger, IEnumerable<IScheduledTaskService> services)
public SchedulerService(ILogger<SchedulerService> logger, IEnumerable<IScheduledTaskService> services, SchedulerServiceOptions options)
{
_logger = logger;
_services = services;
_options = options;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand Down Expand Up @@ -69,10 +71,38 @@ await taskFactory.StartNew(
}

private bool IsEnabled()
{
var envSetting = System.Environment.GetEnvironmentVariable("SCHEDULER_SERVICE_ENABLED");
{
if (_options.SuppressMachineSystemEnabledSetting)
{
if (_options.MachinesAllowedToRun.Any())
{
return _options.MachinesAllowedToRun.Contains(System.Environment.MachineName);
}
else
{
return true;
}
}
else
{
var envSetting = System.Environment.GetEnvironmentVariable("SCHEDULER_SERVICE_ENABLED");

return (envSetting == null || bool.Parse(envSetting) == true);
if (envSetting != null && bool.Parse(envSetting) == false)
{
return false;
}
else
{
if (_options.MachinesAllowedToRun.Any())
{
return _options.MachinesAllowedToRun.Contains(System.Environment.MachineName);
}
else
{
return true;
}
}
}
}
}
}

0 comments on commit 288241f

Please sign in to comment.