diff options
| author | JP Dillingham <jp@dillingham.me> | 2023-12-26 20:54:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-26 20:54:01 -0600 |
| commit | 3ae7cfb73a365c876dfc600779d886ec9be2a944 (patch) | |
| tree | 22b90376616bee4a8ef57cbcb16df86763048a77 | |
| parent | 3b575c6fb531c253c410e47d401072a06fc9b5be (diff) | |
| parent | 2ab48b5c90f902c552782d62212ddf72d5aa7118 (diff) | |
| download | slskd-3ae7cfb73a365c876dfc600779d886ec9be2a944.tar.gz | |
Merge pull request #1030 from slskd/re-scan
Delay start of the application's internal clock
| -rw-r--r-- | src/slskd/Application.cs | 23 | ||||
| -rw-r--r-- | src/slskd/Core/Clock.cs | 58 |
2 files changed, 55 insertions, 26 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index a7483105..37bc4f78 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -286,10 +286,6 @@ namespace slskd { Log.Information("Application started"); - Log.Debug("Starting clock"); - await Clock.StartAsync(); - Log.Debug("Clock started"); - // if the application shut down "uncleanly", transfers may need to be cleaned up. we deliberately don't allow these // records to be updated if the application has started to shut down so that we can do this cleanup and properly // disposition them as having failed due to an application shutdown, instead of some random exception thrown while @@ -411,6 +407,17 @@ namespace slskd } } + // the placement of this may be a bit contentious long term; when the clock starts, all of the + // scheduled tasks will fire for the first time. the clock EventArgs contain a FirstRun flag + // to help timed logic decide whether to run, but as a safeguard the start is delayed until + // *just* before the application connects to the server/controller, meaning all configuration + // and bootup tasks are complete. if this needs to be moved higher, a way to defer tasks from + // the first run of the clock will need to be introduced, and those deferred tasks will need + // to be executed around here somewhere. + Log.Information("Starting system clock..."); + await Clock.StartAsync(); + Log.Information("System clock started"); + if (OptionsAtStartup.Relay.Enabled && OptionsAtStartup.Relay.Mode.ToEnum<RelayMode>() == RelayMode.Agent) { Log.Information("Running in Agent relay mode; not connecting to the Soulseek server."); @@ -736,23 +743,23 @@ namespace slskd Metrics.DistributedNetwork.Children.Set(e.Children.Count); } - private void Clock_EveryMinute(object sender, EventArgs e) + private void Clock_EveryMinute(object sender, ClockEventArgs e) { Metrics.DistributedNetwork.BroadcastLatency.Observe(Client.DistributedNetwork.AverageBroadcastLatency ?? 0); Metrics.DistributedNetwork.CurrentBroadcastLatency.Set(Client.DistributedNetwork.AverageBroadcastLatency ?? 0); } - private void Clock_EveryFiveMinutes(object sender, EventArgs e) + private void Clock_EveryFiveMinutes(object sender, ClockEventArgs e) { _ = Task.Run(() => PruneTransfers()); } - private void Clock_EveryThirtyMinutes(object sender, EventArgs e) + private void Clock_EveryThirtyMinutes(object sender, ClockEventArgs e) { _ = Task.Run(() => PruneFiles()); } - private void Clock_EveryHour(object sender, EventArgs e) + private void Clock_EveryHour(object sender, ClockEventArgs e) { } diff --git a/src/slskd/Core/Clock.cs b/src/slskd/Core/Clock.cs index 31519fc8..1563a895 100644 --- a/src/slskd/Core/Clock.cs +++ b/src/slskd/Core/Clock.cs @@ -35,24 +35,29 @@ namespace slskd } /// <summary> - /// Fires every minute. + /// Fires every 5 minutes. /// </summary> - public static event EventHandler EveryMinute; + public static event EventHandler<ClockEventArgs> EveryFiveMinutes; /// <summary> - /// Fires every 5 minutes. + /// Fires every hour. /// </summary> - public static event EventHandler EveryFiveMinutes; + public static event EventHandler<ClockEventArgs> EveryHour; /// <summary> - /// Fires every 30 minutes. + /// Fires every minute. /// </summary> - public static event EventHandler EveryThirtyMinutes; + public static event EventHandler<ClockEventArgs> EveryMinute; /// <summary> - /// Fires every hour. + /// Fires every 30 minutes. /// </summary> - public static event EventHandler EveryHour; + public static event EventHandler<ClockEventArgs> EveryThirtyMinutes; + + private static Timer EveryFiveMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 5); + private static Timer EveryHourTimer { get; } = CreateTimer(interval: 1000 * 60 * 60); + private static Timer EveryMinuteTimer { get; } = CreateTimer(interval: 1000 * 60); + private static Timer EveryThirtyMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 30); /// <summary> /// Starts the clock. @@ -65,11 +70,13 @@ namespace slskd EveryThirtyMinutesTimer.Enabled = true; EveryHourTimer.Enabled = true; + var firstRunArgs = new ClockEventArgs(firstRun: true); + return Task.WhenAll( - Task.Run(() => Fire(EveryMinute)), - Task.Run(() => Fire(EveryFiveMinutes)), - Task.Run(() => Fire(EveryThirtyMinutes)), - Task.Run(() => Fire(EveryHour))); + Task.Run(() => Fire(EveryMinute, firstRunArgs)), + Task.Run(() => Fire(EveryFiveMinutes, firstRunArgs)), + Task.Run(() => Fire(EveryThirtyMinutes, firstRunArgs)), + Task.Run(() => Fire(EveryHour, firstRunArgs))); } /// <summary> @@ -83,12 +90,27 @@ namespace slskd EveryHourTimer.Stop(); } - private static Timer EveryMinuteTimer { get; } = CreateTimer(interval: 1000 * 60); - private static Timer EveryFiveMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 5); - private static Timer EveryThirtyMinutesTimer { get; } = CreateTimer(interval: 1000 * 60 * 30); - private static Timer EveryHourTimer { get; } = CreateTimer(interval: 1000 * 60 * 60); - private static Timer CreateTimer(double interval) => new() { AutoReset = true, Interval = interval, Enabled = false }; - private static void Fire(EventHandler e) => e?.Invoke(null, EventArgs.Empty); + private static void Fire(EventHandler<ClockEventArgs> e, ClockEventArgs args = null) => e?.Invoke(null, args ?? new ClockEventArgs()); + } + + /// <summary> + /// EventArgs for the application clock. + /// </summary> + public class ClockEventArgs : EventArgs + { + /// <summary> + /// Initializes a new instance of the <see cref="ClockEventArgs"/> class. + /// </summary> + /// <param name="firstRun">A value indicating whether this event was raised when the clock was started.</param> + public ClockEventArgs(bool firstRun = false) + { + FirstRun = firstRun; + } + + /// <summary> + /// Gets a value indicating whether this event was raised when the click was started. + /// </summary> + public bool FirstRun { get; init; } } }
\ No newline at end of file |
