You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Shedulling/Sheduller.cs

364 lines
15 KiB

6 years ago
using System;
using System.Threading.Tasks;
using ZeroLevel.Services.Shedulling;
namespace ZeroLevel
{
/// <summary>
/// The default scheduler is intended for service tasks
6 years ago
/// </summary>
public static class Sheduller
{
#region Factory
6 years ago
public static IExpirationSheduller CreateExpirationSheduller()
{
return new DateTimeSheduller();
}
public static IExpirationAsyncSheduller CreateAsyncExpirationSheduller()
{
return new DateTimeAsyncSheduller();
}
public static ISheduller Create()
{
return new ShedullerImpl();
}
public static IAsyncSheduller CreateAsync()
{
return new AsyncShedullerImpl();
}
#endregion Factory
6 years ago
#region Singletones
6 years ago
private static ISheduller __instanse;
private static readonly object _create_lock = new object();
6 years ago
private static ISheduller DefaultInstance
{
get
{
if (__instanse == null)
{
lock (_create_lock)
{
if (__instanse == null)
{
__instanse = Sheduller.Create();
}
}
}
return __instanse;
}
}
private static IAsyncSheduller __async_instance;
private static readonly object _async_create_lock = new object();
6 years ago
private static IAsyncSheduller DefaultAsyncInstance
{
get
{
if (__async_instance == null)
{
lock (_async_create_lock)
6 years ago
{
if (__async_instance == null)
{
__async_instance = Sheduller.CreateAsync();
}
}
}
return __async_instance;
}
}
#endregion Singletones
6 years ago
#region Sync default instance api
6 years ago
public static long RemindWhile(TimeSpan timespan, Func<long, bool> callback, Action continueWith = null)
{
return DefaultInstance.RemindWhile(timespan, callback, continueWith);
}
6 years ago
public static long RemindWhile(TimeSpan timespan, Func<bool> callback, Action continueWith = null)
{
return DefaultInstance.RemindWhile(timespan, _ => callback(), continueWith);
6 years ago
}
public static long RemindAfter(TimeSpan timespan, Action<long> callback)
{
return DefaultInstance.RemindAfter(timespan, callback);
}
6 years ago
public static long RemindAfter(TimeSpan timespan, Action callback)
{
return DefaultInstance.RemindAfter(timespan, _ => callback());
6 years ago
}
public static long RemindAt(DateTime date, Action<long> callback)
{
return DefaultInstance.RemindAt(date, callback);
}
6 years ago
public static long RemindAt(DateTime date, Action callback)
{
return DefaultInstance.RemindAt(date, _ => callback());
6 years ago
}
public static long RemindEvery(TimeSpan first, TimeSpan next, Action<long> callback)
{
return DefaultInstance.RemindEvery(first, next, callback);
}
6 years ago
public static long RemindEvery(TimeSpan first, TimeSpan next, Action callback)
{
return DefaultInstance.RemindEvery(first, next, _ => callback());
6 years ago
}
public static long RemindEvery(TimeSpan timespan, Action<long> callback)
{
return DefaultInstance.RemindEvery(timespan, callback);
}
6 years ago
public static long RemindEvery(TimeSpan timespan, Action callback)
{
return DefaultInstance.RemindEvery(timespan, _ => callback());
6 years ago
}
/// <summary>
/// Performs an action once a period, while the period is recalculated according to the transferred function at each re-creation of the task.
6 years ago
/// </summary>
/// <param name="nextEventPeriodCalcFunction">Function to calculate the next period</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindEveryNonlinearPeriod(Func<TimeSpan> nextEventPeriodCalcFunction, Action<long> callback)
{
return DefaultInstance.RemindEveryNonlinearPeriod(nextEventPeriodCalcFunction, nextEventPeriodCalcFunction, callback);
}
6 years ago
public static long RemindEveryNonlinearPeriod(Func<TimeSpan> nextEventPeriodCalcFunction, Action callback)
{
return DefaultInstance.RemindEveryNonlinearPeriod(nextEventPeriodCalcFunction, nextEventPeriodCalcFunction, k => callback());
}
6 years ago
/// <summary>
/// Performs an action once a period, while the period is recalculated according to the transferred function at each re-creation of the task.
6 years ago
/// </summary>
/// <param name="firstEventPeriodCalcFunction">The function to calculate the period to the first execution</param>
/// <param name="nextEventPeriodCalcFunction">The function for calculating the period until subsequent performances</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindEveryNonlinearPeriod(Func<TimeSpan> firstEventPeriodCalcFunction, Func<TimeSpan> nextEventPeriodCalcFunction, Action<long> callback)
{
return DefaultInstance.RemindEveryNonlinearPeriod(firstEventPeriodCalcFunction, nextEventPeriodCalcFunction, callback);
}
6 years ago
public static long RemindEveryNonlinearPeriod(Func<TimeSpan> firstEventPeriodCalcFunction, Func<TimeSpan> nextEventPeriodCalcFunction, Action callback)
{
return DefaultInstance.RemindEveryNonlinearPeriod(firstEventPeriodCalcFunction, nextEventPeriodCalcFunction, k => callback());
}
6 years ago
/// <summary>
/// Performs an action once per period, while the date is recalculated from the function transferred each time the task is recreated.
6 years ago
/// </summary>
/// <param name="nextEventDateCalcFunction">The function to calculate the next date</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindEveryNonlinearDate(Func<DateTime, DateTime> nextEventDateCalcFunction, Action<long> callback)
{
return DefaultInstance.RemindEveryNonlinearDate(nextEventDateCalcFunction, nextEventDateCalcFunction, callback);
}
6 years ago
public static long RemindEveryNonlinearDate(Func<DateTime, DateTime> nextEventDateCalcFunction, Action callback)
{
return DefaultInstance.RemindEveryNonlinearDate(nextEventDateCalcFunction, nextEventDateCalcFunction, k => callback());
}
6 years ago
/// <summary>
/// Performs an action once per period, while the date is recalculated from the function transferred each time the task is recreated.
6 years ago
/// </summary>
/// <param name="firstEventDateCalcFunction">The function to calculate the first run date</param>
/// <param name="nextEventDateCalcFunction">The function to calculate the next date</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindEveryNonlinearDate(Func<DateTime, DateTime> firstEventDateCalcFunction,
Func<DateTime, DateTime> nextEventDateCalcFunction, Action<long> callback)
{
return DefaultInstance.RemindEveryNonlinearDate(firstEventDateCalcFunction, nextEventDateCalcFunction, callback);
}
6 years ago
public static long RemindEveryNonlinearDate(Func<DateTime, DateTime> firstEventDateCalcFunction,
Func<DateTime, DateTime> nextEventDateCalcFunction, Action callback)
{
return DefaultInstance.RemindEveryNonlinearDate(firstEventDateCalcFunction, nextEventDateCalcFunction, k => callback());
}
public static bool Remove(long key)
{
return DefaultInstance.Remove(key);
}
#endregion Sync default instance api
6 years ago
#region Async default instance api
6 years ago
public static long RemindAsyncWhile(TimeSpan timespan, Func<long, Task<bool>> callback, Action continueWith = null)
{
return DefaultAsyncInstance.RemindAsyncWhile(timespan, callback, continueWith);
}
6 years ago
public static long RemindAsyncWhile(TimeSpan timespan, Func<Task<bool>> callback, Action continueWith = null)
{
return DefaultAsyncInstance.RemindAsyncWhile(timespan, (k) => callback(), continueWith);
}
public static long RemindAsyncAfter(TimeSpan timespan, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncAfter(timespan, callback);
}
6 years ago
public static long RemindAsyncAfter(TimeSpan timespan, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncAfter(timespan, async k => await callback());
}
public static long RemindAsyncAt(DateTime date, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncAt(date, callback);
}
6 years ago
public static long RemindAsyncAt(DateTime date, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncAt(date, async k => await callback());
}
public static long RemindAsyncEvery(TimeSpan first, TimeSpan next, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEvery(first, next, callback);
}
6 years ago
public static long RemindAsyncEvery(TimeSpan first, TimeSpan next, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEvery(first, next, async k => await callback());
}
public static long RemindAsyncEvery(TimeSpan timespan, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEvery(timespan, callback);
}
6 years ago
public static long RemindAsyncEvery(TimeSpan timespan, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEvery(timespan, async k => await callback());
}
/// <summary>
/// Performs an action once a period, while the period is recalculated according to the transferred function at each re-creation of the task.
6 years ago
/// </summary>
/// <param name="nextEventPeriodCalcFunction">Function to calculate the next period</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindAsyncEveryNonlinearPeriod(Func<TimeSpan> nextEventPeriodCalcFunction, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearPeriod(nextEventPeriodCalcFunction, nextEventPeriodCalcFunction, callback);
}
6 years ago
public static long RemindAsyncEveryNonlinearPeriod(Func<TimeSpan> nextEventPeriodCalcFunction, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearPeriod(nextEventPeriodCalcFunction, nextEventPeriodCalcFunction, async k => await callback());
}
6 years ago
/// <summary>
/// Performs an action once a period, while the period is recalculated according to the transferred function at each re-creation of the task.
6 years ago
/// </summary>
/// <param name="firstEventPeriodCalcFunction">The function to calculate the period to the first execution</param>
/// <param name="nextEventPeriodCalcFunction">The function for calculating the period until subsequent performances</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindAsyncEveryNonlinearPeriod(Func<TimeSpan> firstEventPeriodCalcFunction, Func<TimeSpan> nextEventPeriodCalcFunction, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearPeriod(firstEventPeriodCalcFunction, nextEventPeriodCalcFunction, callback);
}
6 years ago
public static long RemindAsyncEveryNonlinearPeriod(Func<TimeSpan> firstEventPeriodCalcFunction, Func<TimeSpan> nextEventPeriodCalcFunction, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearPeriod(firstEventPeriodCalcFunction, nextEventPeriodCalcFunction, async k => await callback());
}
6 years ago
/// <summary>
/// Performs an action once per period, while the date is recalculated from the function transferred each time the task is recreated.
6 years ago
/// </summary>
/// <param name="nextEventDateCalcFunction">The function to calculate the next date</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindAsyncEveryNonlinearDate(Func<DateTime, DateTime> nextEventDateCalcFunction, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearDate(nextEventDateCalcFunction, nextEventDateCalcFunction, callback);
}
6 years ago
public static long RemindAsyncEveryNonlinearDate(Func<DateTime, DateTime> nextEventDateCalcFunction, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearDate(nextEventDateCalcFunction, nextEventDateCalcFunction, async k => await callback());
}
6 years ago
/// <summary>
/// Performs an action once per period, while the date is recalculated from the function transferred each time the task is recreated.
6 years ago
/// </summary>
/// <param name="firstEventDateCalcFunction">The function to calculate the first run date</param>
/// <param name="nextEventDateCalcFunction">The function to calculate the next date</param>
/// <param name="callback">Action</param>
/// <returns>Task ID</returns>
6 years ago
public static long RemindAsyncEveryNonlinearDate(Func<DateTime, DateTime> firstEventDateCalcFunction,
Func<DateTime, DateTime> nextEventDateCalcFunction, Func<long, Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearDate(firstEventDateCalcFunction, nextEventDateCalcFunction, callback);
}
6 years ago
public static long RemindAsyncEveryNonlinearDate(Func<DateTime, DateTime> firstEventDateCalcFunction,
Func<DateTime, DateTime> nextEventDateCalcFunction, Func<Task> callback)
{
return DefaultAsyncInstance.RemindAsyncEveryNonlinearDate(firstEventDateCalcFunction, nextEventDateCalcFunction, async k => await callback());
}
public static bool RemoveAsync(long key)
{
return DefaultAsyncInstance.Remove(key);
}
#endregion Async default instance api
6 years ago
#region Default instances control
6 years ago
public static void Pause()
{
DefaultInstance.Pause();
DefaultAsyncInstance.Pause();
}
public static void Resume()
{
DefaultInstance.Resume();
DefaultAsyncInstance.Resume();
}
public static void Clean()
{
DefaultInstance.Clean();
DefaultAsyncInstance.Clean();
}
public static void Dispose()
{
DefaultAsyncInstance.Dispose();
DefaultInstance.Dispose();
}
#endregion Default instances control
6 years ago
}
}

Powered by TurnKey Linux.