public static object FromWrapper(ScheduledTaskWrapper target) => new ScheduledTask { TaskId = Guid.ParseExact(target.TaskId, "D"), Name = target.Name, Every = TimeSpan.FromTicks(target.Ticks) };
private async Task ExecuteTask(ScheduledTaskWrapper taskWrapper, ScheduleMode mode, CancellationToken cancellationToken, params object[] args) { try { if (taskWrapper.Task is ScheduledTask) { var task = taskWrapper.Task as ScheduledTask; var count = task.Pipeline.Count(); var msg = $"Starting task {task.GetType().Name} [{mode.ToString()}]"; if (count > 1) { msg = $"{msg} (pipeline of {task.Pipeline.Count()} items)"; //log.LogInformation($"Starting task {task.GetType().Name} (pipeline of {task.Pipeline.Count()} items)"); } log.Information(msg); } var sw = new Stopwatch(); using (var sm = new SemaphoreSlim(1, 1)) { await sm.WaitAsync(); taskWrapper.IsRunning = true; //taskWrapper.Task.SetMode() sw.Start(); await taskWrapper.Task.ExecuteAsync(mode, cancellationToken, args); sw.Stop(); taskWrapper.IsRunning = false; log.Information($"Task {taskWrapper.Task.GetType().Name} completed in {sw.Elapsed}, will run next at {taskWrapper.NextRunTime.ToLocalTime().ToDefaultWithTime()}"); } } catch (Exception ex) { var ex_args = new UnobservedTaskExceptionEventArgs( ex as AggregateException ?? new AggregateException(ex)); UnobservedTaskException?.Invoke(this, ex_args); if (!ex_args.Observed) { throw; } } }