From 1d17eb93ad1445623a4f139074ab99ef53d65464 Mon Sep 17 00:00:00 2001 From: James Tattersall <10601770+jamerst@users.noreply.github.com> Date: Tue, 22 Nov 2022 19:25:43 +0000 Subject: [PATCH] Fix previous trigger detection Wait for config file change if no triggers defined --- README.md | 2 +- Worker.cs | 47 +++++++++++++++++++++++++++++++++-------------- celestial.csproj | 4 ++-- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 87f41bc..564de5f 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ Triggers 30 minutes after dusk "type": "Time", "time": "22:00:00", "path": "/home/james/Pictures/Wallpapers/9.png" - }, + } ], "latitude": 12.34, "longitude": -1.23 diff --git a/Worker.cs b/Worker.cs index 6ff350b..5c66b97 100644 --- a/Worker.cs +++ b/Worker.cs @@ -26,13 +26,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await LoadSettingsAsync(stoppingToken); - if (!settings.Triggers.Any()) - { - _logger.LogCritical("No triggers defined, exiting"); - _host.StopApplication(); - return; - } - _logger.LogInformation("Using provider {provider}", _provider.GetName()); using (var watcher = new FileSystemWatcher(Path.GetDirectoryName(GetConfigPath())!)) @@ -43,6 +36,32 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) watcher.Changed += OnConfigFileChange; + if (!settings.Triggers.Any()) + { + _logger.LogCritical("No triggers defined, waiting for config file change"); + + try + { + // delay of -1ms waits indefinitely + await Task.Delay(-1, ctsCombined.Token); + } + catch (TaskCanceledException e) + { + if (e.CancellationToken == ctsCombined.Token) + { + if (ctsConfig.IsCancellationRequested && !stoppingToken.IsCancellationRequested) + { + _logger.LogInformation("Config file change detected, reloading settings"); + await LoadSettingsAsync(stoppingToken); + } + } + else + { + throw; + } + } + } + SetInitialBackground(); await RunAsync(stoppingToken); @@ -52,14 +71,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) private void SetInitialBackground() { var previousTrigger = settings.Triggers - .Select(t => new { Trigger = t, Today = t.GetPreviousOccurrence(DateTime.Today, settings) }) - .Where(t => t.Today < DateTime.Now) - .OrderByDescending(t => t.Today) + .Select(t => new { Trigger = t, Previous = t.GetPreviousOccurrence(DateTime.Now, settings) }) + .Where(t => t.Previous < DateTime.Now) + .OrderByDescending(t => t.Previous) .FirstOrDefault(); if (previousTrigger != null) { - _logger.LogInformation("Setting initial state from previous trigger {trigger} ({time})", previousTrigger.Trigger, previousTrigger.Today?.ToString("s")); + _logger.LogInformation("Setting initial state from previous trigger {trigger} ({time})", previousTrigger.Trigger, previousTrigger.Previous?.ToString("s")); try { _provider.SetBackground(previousTrigger.Trigger.Path); @@ -116,10 +135,9 @@ private async Task RunAsync(CancellationToken stoppingToken) { _logger.LogInformation("Next trigger is {trigger} in {delay} ({time})", nextTrigger, delay, next?.ToString("s")); - // wait until trigger time (if in future) - try { + // wait until trigger time (if in future) await Task.Delay(delay, ctsCombined.Token); } catch (TaskCanceledException e) @@ -136,7 +154,7 @@ private async Task RunAsync(CancellationToken stoppingToken) } else { - throw e; + throw; } } } @@ -169,6 +187,7 @@ private void OnConfigFileChange(object sender, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed || e.ChangeType == WatcherChangeTypes.Created) { + // request cancellation to break out of any Task.Delays in progress and reload settings ctsConfig.Cancel(); } } diff --git a/celestial.csproj b/celestial.csproj index 43beed4..3a1fc61 100644 --- a/celestial.csproj +++ b/celestial.csproj @@ -5,8 +5,8 @@ net7.0 enable enable - 1.0.0.0 - 1.0.0 + 1.0.1.0 + 1.0.1