/// <summary> /// Returns a pair of the delegate to call the Invoke function and the argument types in its /// method parameters. /// </summary> private static MethodArgumentPair ExtractInvocationInfo(SchedulerTaskWrapper taskThatShouldRun) { var type = taskThatShouldRun.Task.GetType(); var method = type.GetMethod("InvokeAsync", BindingFlags.Instance | BindingFlags.Public); var argumentTypes = method.GetParameters() .Select(a => a.ParameterType) .ToArray(); var exp = Expression.GetDelegateType(argumentTypes.Concat(new[] { method.ReturnType }).ToArray()); var invoke = method.CreateDelegate(exp, taskThatShouldRun.Task); return(new MethodArgumentPair(invoke, argumentTypes)); }
public SchedulerHostedService(IEnumerable <IScheduledTask> scheduledTasks) { var referenceTime = DateTime.UtcNow; foreach (var scheduledTask in scheduledTasks) { var task = new SchedulerTaskWrapper { Schedule = CrontabSchedule.Parse(scheduledTask.Schedule), Task = scheduledTask, NextRunTime = referenceTime }; task.Increment(); _scheduledTasks.Add(task); } }
public SchedulerHostedService(IEnumerable <IScheduledTask> scheduledTasks, TimeSpan?delay = null) { _scheduledTasks = new List <SchedulerTaskWrapper>(); var referenceTime = DateTime.UtcNow; foreach (var scheduledTask in scheduledTasks) { var wrapper = new SchedulerTaskWrapper { Schedule = CrontabSchedule.Parse(scheduledTask.Schedule), Task = scheduledTask, NextRunTime = referenceTime }; _scheduledTasks.Add(wrapper); } _delay = delay ?? TimeSpan.FromMinutes(1); }
private void InitTasks(IEnumerable <IScheduledTask> tasks) { var referenceTime = DateTime.UtcNow; logger.LogTrace("Scheduling Tasks: {referenceTime}", referenceTime); foreach (var task in tasks) { var schedule = CrontabSchedule.Parse(task.Schedule); var schedulerTaskWrapper = new SchedulerTaskWrapper { Schedule = schedule, Task = task, NextRunTime = schedule.GetNextOccurrence(referenceTime) }; logger.LogTrace("Task Scheduled: {taskDescription}, Schedule: {schedule}, NextRun: {nextRunTime}", task.Description, schedule.ToString(), schedulerTaskWrapper.NextRunTime); scheduledTasks.Add(schedulerTaskWrapper); } }