From 51eddf4e8cfee48b5b612adcb321f308605a3a8f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 26 Dec 2023 20:43:09 -0600 Subject: add ClockEventArgs, reorganize to remove warnings --- src/slskd/Core/Clock.cs | 58 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 18 deletions(-) (limited to 'src') 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 } /// - /// Fires every minute. + /// Fires every 5 minutes. /// - public static event EventHandler EveryMinute; + public static event EventHandler EveryFiveMinutes; /// - /// Fires every 5 minutes. + /// Fires every hour. /// - public static event EventHandler EveryFiveMinutes; + public static event EventHandler EveryHour; /// - /// Fires every 30 minutes. + /// Fires every minute. /// - public static event EventHandler EveryThirtyMinutes; + public static event EventHandler EveryMinute; /// - /// Fires every hour. + /// Fires every 30 minutes. /// - public static event EventHandler EveryHour; + public static event EventHandler 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); /// /// 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))); } /// @@ -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 e, ClockEventArgs args = null) => e?.Invoke(null, args ?? new ClockEventArgs()); + } + + /// + /// EventArgs for the application clock. + /// + public class ClockEventArgs : EventArgs + { + /// + /// Initializes a new instance of the class. + /// + /// A value indicating whether this event was raised when the clock was started. + public ClockEventArgs(bool firstRun = false) + { + FirstRun = firstRun; + } + + /// + /// Gets a value indicating whether this event was raised when the click was started. + /// + public bool FirstRun { get; init; } } } \ No newline at end of file -- cgit v1.2.3 From 2ab48b5c90f902c552782d62212ddf72d5aa7118 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 26 Dec 2023 20:45:00 -0600 Subject: delay start of clock, update event handlers --- src/slskd/Application.cs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src') 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.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) { } -- cgit v1.2.3