diff --git a/Global.cs b/Global.cs index 09e6df8..8355d2b 100644 --- a/Global.cs +++ b/Global.cs @@ -8,4 +8,5 @@ global using System.Text; global using System.Threading.Tasks; global using static Xorog.UniversalExtensions.UniversalExtensionsEnums; -global using static Xorog.UniversalExtensions.Internal; \ No newline at end of file +global using static Xorog.UniversalExtensions.Internal; +global using static Xorog.UniversalExtensions.InternalSheduler; \ No newline at end of file diff --git a/Internal.cs b/Internal/Internal.cs similarity index 92% rename from Internal.cs rename to Internal/Internal.cs index c36c085..c757e23 100644 --- a/Internal.cs +++ b/Internal/Internal.cs @@ -76,4 +76,16 @@ internal static class Internal return _timespan.ToString(); } } +} + +public class InternalSheduler +{ + public static Dictionary registeredScheduledTasks { get; private set; } = new Dictionary(); + + public class taskInfo + { + public string userId { get; internal set; } = ""; + internal CancellationTokenSource? tokenSource { get; set; } + public DateTime? runTime { get; internal set; } + } } \ No newline at end of file diff --git a/UniversalExtensions.cs b/UniversalExtensions.cs index ac5a0c5..8af40f4 100644 --- a/UniversalExtensions.cs +++ b/UniversalExtensions.cs @@ -168,6 +168,76 @@ public static class UniversalExtensions + /// + /// Create a scheduled task + /// + /// The task to run + /// The time to run the task + /// An unique identifier of the task + public static string CreateScheduleTask(this Task task, DateTime runTime, string UserId = "") + { + string UID = Guid.NewGuid().ToString(); + CancellationTokenSource CancellationToken = new CancellationTokenSource(); + + _ = Task.Delay(runTime.GetTimespanUntil().Milliseconds, CancellationToken.Token).ContinueWith(x => + { + if (x.IsCompletedSuccessfully) + task.Start(); + }); + + registeredScheduledTasks.Add(UID, new taskInfo { tokenSource = CancellationToken, userId = UserId, runTime = runTime}); + return UID; + } + + + + /// + /// Deletes a scheduled task + /// + /// The task's unique identifier + /// Throws if the task hasn't been found or if an internal error occured + public static void DeleteSheduleTask(string UID) + { + if (!registeredScheduledTasks.ContainsKey(UID)) + throw new Exception($"No sheduled task has been found with UID '{UID}'"); + + if (registeredScheduledTasks[ UID ].tokenSource is null) + throw new Exception($"Internal: There is no token source registered the specified task."); + + registeredScheduledTasks[ UID ].tokenSource?.Cancel(); + registeredScheduledTasks.Remove(UID); + return; + } + + + + /// + /// Gets a list of all registered tasks + /// + /// A list of all registered tasks + public static IReadOnlyDictionary? GetSheduleTasks() + { + return registeredScheduledTasks.ToList() as IReadOnlyDictionary; + } + + + + /// + /// Gets a specific task + /// + /// The unique identifier of what task to get + /// The task + /// Throws if the task has not been found + public static taskInfo GetSheduleTask(string UID) + { + if (!registeredScheduledTasks.ContainsKey(UID)) + throw new Exception($"The specified task doesn't exist."); + + return registeredScheduledTasks[UID]; + } + + + /// /// Compute the SHA256-Hash for a given file ///