//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
namespace slskd
{
using System;
using System.Threading.Tasks;
using System.Timers;
///
/// The application clock, used for time based events.
///
public static class Clock
{
static Clock()
{
EveryMinuteTimer.Elapsed += (_, _) => Fire(EveryMinute);
EveryFiveMinutesTimer.Elapsed += (_, _) => Fire(EveryFiveMinutes);
EveryThirtyMinutesTimer.Elapsed += (_, _) => Fire(EveryThirtyMinutes);
EveryHourTimer.Elapsed += (_, _) => Fire(EveryHour);
}
///
/// Fires every 5 minutes.
///
public static event EventHandler EveryFiveMinutes;
///
/// Fires every hour.
///
public static event EventHandler EveryHour;
///
/// Fires every minute.
///
public static event EventHandler EveryMinute;
///
/// Fires every 30 minutes.
///
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.
///
/// A Task that completes when all startup events have finished processing.
public static Task StartAsync()
{
EveryMinuteTimer.Enabled = true;
EveryFiveMinutesTimer.Enabled = true;
EveryThirtyMinutesTimer.Enabled = true;
EveryHourTimer.Enabled = true;
var firstRunArgs = new ClockEventArgs(firstRun: true);
return Task.WhenAll(
Task.Run(() => Fire(EveryMinute, firstRunArgs)),
Task.Run(() => Fire(EveryFiveMinutes, firstRunArgs)),
Task.Run(() => Fire(EveryThirtyMinutes, firstRunArgs)),
Task.Run(() => Fire(EveryHour, firstRunArgs)));
}
///
/// Stops the clock.
///
public static void Stop()
{
EveryMinuteTimer.Stop();
EveryFiveMinutesTimer.Stop();
EveryThirtyMinutesTimer.Stop();
EveryHourTimer.Stop();
}
private static Timer CreateTimer(double interval) => new() { AutoReset = true, Interval = interval, Enabled = false };
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; }
}
}