jellyfin/MediaBrowser.Common/ScheduledTasks/ScheduledTaskHelpers.cs

181 lines
5.5 KiB
C#
Raw Normal View History

2013-02-25 00:13:45 +00:00
using MediaBrowser.Model.Tasks;
2013-02-21 01:33:05 +00:00
using System;
using System.Linq;
namespace MediaBrowser.Common.ScheduledTasks
{
/// <summary>
/// Class ScheduledTaskHelpers
/// </summary>
public static class ScheduledTaskHelpers
{
/// <summary>
/// Gets the task info.
/// </summary>
/// <param name="task">The task.</param>
/// <returns>TaskInfo.</returns>
public static TaskInfo GetTaskInfo(IScheduledTaskWorker task)
2013-02-21 01:33:05 +00:00
{
var isHidden = false;
var configurableTask = task.ScheduledTask as IConfigurableScheduledTask;
if (configurableTask != null)
{
isHidden = configurableTask.IsHidden;
}
2014-04-03 22:50:04 +00:00
string key = null;
var hasKey = task.ScheduledTask as IHasKey;
if (hasKey != null)
{
key = hasKey.Key;
}
2014-09-28 15:27:26 +00:00
var triggers = task.Triggers
.Select(GetTriggerInfo)
.OrderBy(i => i.Type)
.ThenBy(i => i.DayOfWeek ?? DayOfWeek.Sunday)
.ThenBy(i => i.TimeOfDayTicks ?? 0)
.ToList();
2013-02-21 01:33:05 +00:00
return new TaskInfo
{
Name = task.Name,
2013-02-22 04:23:06 +00:00
CurrentProgressPercentage = task.CurrentProgress,
2013-02-21 01:33:05 +00:00
State = task.State,
2014-05-09 19:43:06 +00:00
Id = task.Id,
2013-02-21 01:33:05 +00:00
LastExecutionResult = task.LastExecutionResult,
2014-09-28 15:27:26 +00:00
Triggers = triggers,
2013-02-21 01:33:05 +00:00
Description = task.Description,
Category = task.Category,
2014-04-03 22:50:04 +00:00
IsHidden = isHidden,
Key = key
2013-02-21 01:33:05 +00:00
};
}
/// <summary>
/// Gets the trigger info.
/// </summary>
/// <param name="trigger">The trigger.</param>
/// <returns>TaskTriggerInfo.</returns>
2013-02-24 21:53:54 +00:00
public static TaskTriggerInfo GetTriggerInfo(ITaskTrigger trigger)
2013-02-21 01:33:05 +00:00
{
var info = new TaskTriggerInfo
{
Type = trigger.GetType().Name
};
var dailyTrigger = trigger as DailyTrigger;
if (dailyTrigger != null)
{
info.TimeOfDayTicks = dailyTrigger.TimeOfDay.Ticks;
}
var weeklyTaskTrigger = trigger as WeeklyTrigger;
if (weeklyTaskTrigger != null)
{
info.TimeOfDayTicks = weeklyTaskTrigger.TimeOfDay.Ticks;
info.DayOfWeek = weeklyTaskTrigger.DayOfWeek;
}
var intervalTaskTrigger = trigger as IntervalTrigger;
if (intervalTaskTrigger != null)
{
info.IntervalTicks = intervalTaskTrigger.Interval.Ticks;
}
var systemEventTrigger = trigger as SystemEventTrigger;
if (systemEventTrigger != null)
{
info.SystemEvent = systemEventTrigger.SystemEvent;
}
return info;
}
/// <summary>
/// Converts a TaskTriggerInfo into a concrete BaseTaskTrigger
/// </summary>
/// <param name="info">The info.</param>
/// <returns>BaseTaskTrigger.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
/// <exception cref="System.ArgumentException">Invalid trigger type: + info.Type</exception>
2013-02-24 21:53:54 +00:00
public static ITaskTrigger GetTrigger(TaskTriggerInfo info)
2013-02-21 01:33:05 +00:00
{
if (info.Type.Equals(typeof(DailyTrigger).Name, StringComparison.OrdinalIgnoreCase))
{
if (!info.TimeOfDayTicks.HasValue)
{
throw new ArgumentNullException();
}
return new DailyTrigger
{
TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value)
};
}
if (info.Type.Equals(typeof(WeeklyTrigger).Name, StringComparison.OrdinalIgnoreCase))
{
if (!info.TimeOfDayTicks.HasValue)
{
throw new ArgumentNullException();
}
if (!info.DayOfWeek.HasValue)
{
throw new ArgumentNullException();
}
return new WeeklyTrigger
{
TimeOfDay = TimeSpan.FromTicks(info.TimeOfDayTicks.Value),
DayOfWeek = info.DayOfWeek.Value
};
}
if (info.Type.Equals(typeof(IntervalTrigger).Name, StringComparison.OrdinalIgnoreCase))
{
if (!info.IntervalTicks.HasValue)
{
throw new ArgumentNullException();
}
return new IntervalTrigger
{
Interval = TimeSpan.FromTicks(info.IntervalTicks.Value)
};
}
if (info.Type.Equals(typeof(SystemEventTrigger).Name, StringComparison.OrdinalIgnoreCase))
{
if (!info.SystemEvent.HasValue)
{
throw new ArgumentNullException();
}
return new SystemEventTrigger
{
SystemEvent = info.SystemEvent.Value
};
}
if (info.Type.Equals(typeof(StartupTrigger).Name, StringComparison.OrdinalIgnoreCase))
{
2013-02-23 07:57:11 +00:00
return new StartupTrigger();
2013-02-21 01:33:05 +00:00
}
throw new ArgumentException("Unrecognized trigger type: " + info.Type);
}
}
}