Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Utils/ConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public static class ConfigLoader
private static readonly TimeSpan _cacheDuration = TimeSpan.FromSeconds(5);
private static readonly Lock _lock = new();

private static string? _cachedTzId;
private static TimeZoneInfo _cachedTz = TimeZoneInfo.Utc;

public static async Task<Config?> Load(string path = "config.json")
{
try
Expand Down Expand Up @@ -52,5 +55,24 @@ public static class ConfigLoader
}
}
}

public static TimeZoneInfo GetTimeZone(string? tzId = null)
{
try
{
var id = (tzId ?? LoadCached()?.TZInfo ?? "UTC").Trim();

if (string.Equals(id, _cachedTzId, StringComparison.Ordinal))
return _cachedTz;

_cachedTzId = id;
_cachedTz = TimeZoneInfo.FindSystemTimeZoneById(id);
return _cachedTz;
}
catch
{
return TimeZoneInfo.Utc;
}
}
}
}
25 changes: 1 addition & 24 deletions src/Utils/CustomLogFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

namespace EnvAutoUpdater.src.Utils
{
public class CustomLogFormatter(IOptionsMonitor<SimpleConsoleFormatterOptions> options) : ConsoleFormatter(nameof(CustomLogFormatter))

Check warning on line 8 in src/Utils/CustomLogFormatter.cs

View workflow job for this annotation

GitHub Actions / Unit & Integration Tests

Parameter 'options' is unread.
{
private string? _cachedTzId;
private TimeZoneInfo _cachedTz = TimeZoneInfo.Utc;

public override void Write<TState>(in LogEntry<TState> logEntry, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
var (level, color) = logEntry.LogLevel switch
Expand All @@ -23,7 +20,7 @@
_ => ("UNKNOWN", ConsoleColor.White)
};

var tz = GetTimeZone();
var tz = ConfigLoader.GetTimeZone();
var timestamp = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tz).ToString("yyyy-MM-dd HH:mm:ss");
var category = logEntry.Category?.Split('.').Last();
var message = logEntry.Formatter(logEntry.State, logEntry.Exception);
Expand All @@ -50,25 +47,5 @@
ConsoleColor.White => "\x1B[1m\x1B[37m",
_ => "\x1B[39m"
};

private TimeZoneInfo GetTimeZone()
{
try
{
var config = ConfigLoader.LoadCached();
var tzId = (config?.TZInfo ?? "UTC").Trim();

if (string.Equals(tzId, _cachedTzId, StringComparison.Ordinal))
return _cachedTz;

_cachedTzId = tzId;
_cachedTz = TimeZoneInfo.FindSystemTimeZoneById(tzId);
return _cachedTz;
}
catch
{
return TimeZoneInfo.Utc;
}
}
}
}
11 changes: 7 additions & 4 deletions src/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
return;
}

_logger.LogInformation($"Config: {config!.ToString()}");
_logger.LogInformation($"Config: {config}");

while (!stoppingToken.IsCancellationRequested)
{
config = ConfigLoader.LoadCached() ?? config;
await _envUpdaterService.Run(stoppingToken);
var next = DateTime.Now.AddSeconds(config.CheckInterval);

_logger.LogInformation($"Next execution: {next}");
var tz = ConfigLoader.GetTimeZone(config.TZInfo);
var next = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow.AddSeconds(config.CheckInterval), tz);

_logger.LogInformation($"Next execution: {next:yyyy-MM-dd HH:mm:ss}");

var delay = (int)Math.Min(Math.Max(0, (long)(next - DateTimeOffset.UtcNow).TotalMilliseconds), int.MaxValue);

var delay = Math.Max(0, (int)(next - DateTime.Now).TotalMilliseconds);
await Task.Delay(delay, stoppingToken);
}
}
Expand Down
Loading