LANCommander/LANCommander.Server/Jobs/Recurring/AutomaticRepackage.cs
Pat Hartl add770b227 Refactor Settings
Settings for the server are now combined with the settings from the SDK. This introduces a large breaking change and a migration should be created. This refactor utilizes .NET Configuration and the Options pattern. This will allow for the overriding of any setting using envionment variables. It also means that Settings.yml can be used to override any .NET configuration. A SettingsProvider implementation was created, and any updating of settings was changed from SettingsService.SaveSettings() to SettingsProvider.Update(s => { ... })
2025-11-16 12:31:25 -06:00

56 lines
No EOL
1.9 KiB
C#

using Hangfire;
using LANCommander.SDK.Enums;
using LANCommander.Server.Services;
namespace LANCommander.Server.Jobs.Recurring;
public sealed class AutomaticRepackage(
SettingsProvider<Settings.Settings> settingsProvider,
GameService gameService,
ScriptService scriptService,
ILogger<AutomaticRepackage> logger) : BaseRecurringJob(logger)
{
public override string CronExpression
{
get
{
var minutes = settingsProvider.CurrentValue.Server.Scripts.RepackageEvery % 60;
var hours = settingsProvider.CurrentValue.Server.Scripts.RepackageEvery / 60;
var days = hours % 24;
var minuteExpression = minutes > 0 ? $"*/{minutes}" : "*";
var hourExpression = hours > 0 ? $"*/{hours}" : "*";
var dayExpression = days > 0 ? $"*/{days}" : "*";
return $"{minuteExpression} {hourExpression} {dayExpression} * *";
}
}
public override async Task ExecuteAsync()
{
if (!settingsProvider.CurrentValue.Server.Scripts.EnableAutomaticRepackaging)
{
logger.LogWarning("The automatic repackaging job attempted to execute, but automatic repackaging is disabled");
return;
}
logger.LogInformation("Starting automatic repackaging job");
var repackageScripts = await scriptService.GetAsync(s => s.Type == ScriptType.Package);
logger.LogInformation("Found {ScriptCount} packaging scripts}", repackageScripts.Count);
foreach (var script in repackageScripts)
{
try
{
if (script.GameId.HasValue)
await gameService.PackageAsync(script.GameId.Value);
}
catch (Exception ex)
{
logger?.LogError(ex, "Could not automatically run packaging script with ID {ScriptId}", script.Id);
}
}
}
}