private bool IsDue(IHTaskItem item) { if (item?.Schedule == null) { return(false); } DateTime timeNow = DateTime.Now; var log = this.TimeLog?[item.UniqueKey]; #region error retry check if (item.Schedule.RetryAttemptsAfterError > 0 && log?.LastError != null && ( log.ErrorCount > item.Schedule.RetryAttemptsAfterError || ( item.Schedule.RetryInMilisecAfterError != null && timeNow < ((DateTime)log.LastError).AddMilliseconds( (int)item.Schedule.RetryInMilisecAfterError) ) ) ) { return(false); } #endregion #region not before if (item.Schedule.NotBefore != null && timeNow < item.Schedule.NotBefore) { return(false); } #endregion #region not after if (item.Schedule.NotAfter != null && timeNow > item.Schedule.NotAfter) { return(false); } #endregion #region dates if (item.Schedule.Dates != null && !item.Schedule.Dates.Any(x => timeNow >= x && timeNow < x.Date.AddDays(1))) { return(false); } #endregion #region days of the year if (item.Schedule.DaysOfYear != null && !item.Schedule.DaysOfYear.Any(x => x == timeNow.DayOfYear)) { return(false); } #endregion #region end of the month if (item.Schedule.LastDayOfMonth == true && timeNow.AddDays(1).Day != 1) { return(false); } #endregion #region days of the month if (item.Schedule.DaysOfMonth != null && !item.Schedule.DaysOfMonth.Any(x => x == timeNow.Day)) { return(false); } #endregion #region days of the week if (item.Schedule.DaysOfWeek != null && !item.Schedule.DaysOfWeek .Any(x => x.EqualsIgnoreCase(timeNow.DayOfWeek.ToString()))) { return(false); } #endregion #region time if (item.Schedule.Time != null && timeNow < (timeNow.Date + item.Schedule.Time)) { return(false); } #endregion #region until time if (item.Schedule.UntilTime != null && timeNow > (timeNow.Date + item.Schedule.UntilTime)) { return(false); } #endregion #region interval & last executed logic if (item.Schedule.Interval != null) { if (log?.LastExecuted != null && item.Schedule.Interval > ((TimeSpan)(timeNow - log.LastExecuted)).TotalMilliseconds) { return(false); } } else { if (log.LastExecuted?.Date == timeNow.Date) { return(false); } } #endregion #region is enabled if (!item.Schedule.Enabled) { return(false); } #endregion return(true); }
private void Process(IHTaskItem task) { HTaskSchedulerEventArgs evArgs = null; try { // check if task is eligible to run including retry on error status (if failed to run in an earlier attempt and the schedule // for when to retry and retry max attempts). if (!this.IsDue(task)) { return; } void RunTask() { if (this.Cts.IsCancellationRequested) { return; } // todo: threaded having continuewith to check // run status evArgs = new HTaskSchedulerEventArgs( this, task, this.Cts.Token ); // if eligible to run (and has no previous error logged), trigger synchronous event this.OnTaskIsDueAsync(evArgs) .GetAwaiter().GetResult(); } if (task.Schedule?.Repeat != null) { AtomicGate delaySwitch = new AtomicGate(); foreach (var repeatDataModel in task.Schedule?.Repeat.EnsureEnumerable()) { if (task.Schedule?.RepeatDelayInterval > 0 && !delaySwitch.TryOpen()) { if (this.Cts?.Token != null) { Task.Delay((int)task.Schedule.RepeatDelayInterval, (CancellationToken)this.Cts.Token) .GetAwaiter().GetResult(); } else { Task.Delay((int)task.Schedule.RepeatDelayInterval) .GetAwaiter().GetResult(); } } if (this.Cts.IsCancellationRequested) { return; } IEnumerable <IHTaskItem> AllChildren(IHTaskItem item) { return((item.Children?.SelectMany(x => AllChildren(x)) ?? Enumerable.Empty <IHTaskItem>()).Append(item)); } foreach (var child in AllChildren(task) //.Where(x => x.Vars?.Custom == null) ) { child.Vars.Custom = repeatDataModel; } RunTask(); } } else { RunTask(); } // log successful run, and reset retry on error logic in case it was previously set. this.TimeLog[task.UniqueKey].LastExecuted = DateTime.Now; this.TimeLog[task.UniqueKey].ErrorCount = 0; this.TimeLog[task.UniqueKey].LastError = null; this.TimeLog.Save(); } catch (Exception ex) { // catch errors within the thread, and check if retry on error is enabled // if enabled, don't throw exception, trigger OnErrorEvent async, then log error retry attempts and last error this.OnErrorAsync(new HTaskSchedularErrorEventArgs(this, ex, evArgs)); if (task.Schedule.RetryAttemptsAfterError == null) { throw; } this.TimeLog[task.UniqueKey].ErrorCount++; this.TimeLog[task.UniqueKey].LastError = DateTime.Now; this.TimeLog.Save(); } }
public static ValueProcessorItem Parse(IHTaskItem item) => new ValueProcessorItem() { Item = item, Value = item?.RawValue };
bool ICollection <IHTaskItem> .Remove(IHTaskItem item) => throw new NotImplementedException();
bool ICollection <IHTaskItem> .Contains(IHTaskItem item) => this.GetTasks()?.Contains(item) ?? false;
void ICollection <IHTaskItem> .Add(IHTaskItem item) => throw new NotImplementedException();