static Task DeferTask(TaskDefinition taskDefinition, IPipelineContext context) { var options = new SendOptions(); options.DelayDeliveryWith(taskDefinition.Every); options.RouteToThisEndpoint(); return context.Send(new ScheduledTask { TaskId = taskDefinition.Id, Name = taskDefinition.Name, Every = taskDefinition.Every }, options); }
/// <summary> /// Schedules a task to be executed repeatedly in a given interval. /// </summary> /// <param name="session">The session which allows you to perform message operation.</param> /// <param name="timeSpan">The interval to repeatedly execute the <paramref name="task" />.</param> /// <param name="task">The async function to execute.</param> /// <param name="name">The name to used for logging the task being executed.</param> public static Task ScheduleEvery(this IMessageSession session, TimeSpan timeSpan, string name, Func<IPipelineContext, Task> task) { Guard.AgainstNull(nameof(task), task); Guard.AgainstNullAndEmpty(nameof(name), name); Guard.AgainstNegativeAndZero(nameof(timeSpan), timeSpan); var taskDefinition = new TaskDefinition { Every = timeSpan, Name = name, Task = task }; return Schedule(session, taskDefinition); }
static Task Schedule(IMessageSession session, TaskDefinition taskDefinition) { logger.DebugFormat("Task '{0}' (with id {1}) scheduled with timeSpan {2}", taskDefinition.Name, taskDefinition.Id, taskDefinition.Every); var options = new SendOptions(); options.DelayDeliveryWith(taskDefinition.Every); options.RouteToThisEndpoint(); options.Context.GetOrCreate<ScheduleBehavior.State>().TaskDefinition = taskDefinition; var scheduledTask = new ScheduledTask { TaskId = taskDefinition.Id, Name = taskDefinition.Name, Every = taskDefinition.Every }; return session.Send(scheduledTask, options); }
static async Task ExecuteTask(TaskDefinition taskDefinition, IPipelineContext context) { logger.InfoFormat("Start executing scheduled task named '{0}'.", taskDefinition.Name); var sw = new Stopwatch(); sw.Start(); try { await taskDefinition.Task(context).ConfigureAwait(false); logger.InfoFormat("Scheduled task '{0}' run for {1}", taskDefinition.Name, sw.Elapsed); } catch (Exception ex) { logger.Error($"Failed to execute scheduled task '{taskDefinition.Name}'.", ex); } finally { sw.Stop(); } }
public void Schedule(TaskDefinition taskDefinition) { scheduledTasks[taskDefinition.Id] = taskDefinition; }