Skip to content

Commit

Permalink
Fix previous trigger detection
Browse files Browse the repository at this point in the history
Wait for config file change if no triggers defined
  • Loading branch information
jamerst committed Nov 22, 2022
1 parent b09f4d8 commit 1d17eb9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 33 additions & 14 deletions Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())!))
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -136,7 +154,7 @@ private async Task RunAsync(CancellationToken stoppingToken)
}
else
{
throw e;
throw;
}
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions celestial.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<Version>1.0.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 1d17eb9

Please sign in to comment.