af1152b001
Refactored all C# test files to use explicit namespace declarations and moved all using directives inside the namespace block for consistency. Updated assembly info and cache files to reflect the new build. Adjusted MvcTestingAppManifest.json to use fully qualified assembly names. No functional changes; all updates are related to code style, organization, and project metadata.
111 lines
2.6 KiB
C#
111 lines
2.6 KiB
C#
// <copyright file="IntervalTrigger.cs" company="PlaceholderCompany">
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
// </copyright>
|
|
|
|
namespace Emby.Server.Implementations.ScheduledTasks.Triggers;
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using MediaBrowser.Model.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
/// <summary>
|
|
/// Represents a task trigger that runs repeatedly on an interval.
|
|
/// </summary>
|
|
public sealed class IntervalTrigger : ITaskTrigger, IDisposable
|
|
{
|
|
private readonly TimeSpan _interval;
|
|
private DateTime _lastStartDate;
|
|
private Timer? _timer;
|
|
private bool _disposed;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="IntervalTrigger"/> class.
|
|
/// </summary>
|
|
/// <param name="interval">The interval.</param>
|
|
/// <param name="taskOptions">The options of this task.</param>
|
|
public IntervalTrigger(TimeSpan interval, TaskOptions taskOptions)
|
|
{
|
|
_interval = interval;
|
|
TaskOptions = taskOptions;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public event EventHandler<EventArgs>? Triggered;
|
|
|
|
/// <inheritdoc />
|
|
public TaskOptions TaskOptions { get; }
|
|
|
|
/// <inheritdoc />
|
|
public void Start(TaskResult? lastResult, ILogger logger, string taskName, bool isApplicationStartup)
|
|
{
|
|
DisposeTimer();
|
|
|
|
DateTime now = DateTime.UtcNow;
|
|
DateTime triggerDate;
|
|
|
|
if (lastResult is null)
|
|
{
|
|
// Task has never been completed before
|
|
triggerDate = now.AddHours(1);
|
|
}
|
|
else
|
|
{
|
|
triggerDate = new[] { lastResult.EndTimeUtc, _lastStartDate, now.AddMinutes(1) }.Max().Add(_interval);
|
|
}
|
|
|
|
var dueTime = triggerDate - now;
|
|
var maxDueTime = TimeSpan.FromDays(7);
|
|
|
|
if (dueTime > maxDueTime)
|
|
{
|
|
dueTime = maxDueTime;
|
|
}
|
|
|
|
_timer = new Timer(_ => OnTriggered(), null, dueTime, TimeSpan.FromMilliseconds(-1));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Stop()
|
|
{
|
|
DisposeTimer();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disposes the timer.
|
|
/// </summary>
|
|
private void DisposeTimer()
|
|
{
|
|
_timer?.Dispose();
|
|
_timer = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when [triggered].
|
|
/// </summary>
|
|
private void OnTriggered()
|
|
{
|
|
DisposeTimer();
|
|
|
|
if (Triggered is not null)
|
|
{
|
|
_lastStartDate = DateTime.UtcNow;
|
|
Triggered(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
DisposeTimer();
|
|
|
|
_disposed = true;
|
|
}
|
|
}
|