private async Task RunItemAsync(SchedulerDataProvider schedDP, SchedulerItemData item) { long logId = DateTime.UtcNow.Ticks; SchedulerLog.SetCurrent(logId, 0, item.Name); item.IsRunning = true; item.RunTime = new TimeSpan(); item.Last = DateTime.UtcNow; try { await schedDP.UpdateItemAsync(item); } catch (Exception exc) { Logging.AddErrorLog("Updating scheduler item {0} failed.", item.Name, exc); } StringBuilder errors = new StringBuilder(); DateTime? nextRun = null;// called event handlers can return a next run time try { item.Errors = null; DateTime now = DateTime.UtcNow; { string m = $"Scheduler event - running scheduler item '{item.Name}'."; Logging.AddLog(m); errors.AppendLine(m); } Type tp = null; try { Assembly asm = Assemblies.Load(item.Event.ImplementingAssembly); tp = asm.GetType(item.Event.ImplementingType); } catch (Exception exc) { throw new InternalError("Scheduler item '{0}' could not be loaded (Type={1}, Assembly={2}) - {3}", item.Name, item.Event.ImplementingType, item.Event.ImplementingAssembly, ErrorHandling.FormatExceptionMessage(exc)); } if (item.SiteSpecific) { DataProviderGetRecords <SiteDefinition> info = await SiteDefinition.GetSitesAsync(0, 0, null, null); foreach (SiteDefinition site in info.Data) { IScheduling schedEvt = null; try { schedEvt = (IScheduling)Activator.CreateInstance(tp); } catch (Exception exc) { string m = $"Scheduler item '{item.Name}' could not be instantiated (Type={item.Event.ImplementingType}, Assembly={item.Event.ImplementingAssembly}) - {ErrorHandling.FormatExceptionMessage(exc)}"; Logging.AddLog(m); errors.AppendLine(m); } if (schedEvt != null) { YetaWFManager.MakeThreadInstance(site, null, true);// set up a manager for the site SchedulerLog.LimitTo(YetaWFManager.Manager); SchedulerLog.SetCurrent(logId, site.Identity, item.Name); SchedulerItemBase itemBase = new SchedulerItemBase { Name = item.Name, Description = item.Description, EventName = item.Event.Name, Enabled = true, Frequency = item.Frequency, Startup = item.Startup, SiteSpecific = true }; try { await schedEvt.RunItemAsync(itemBase); } catch (Exception exc) { string m = $"An error occurred in scheduler item '{site.Identity}: {item.Name}' - {ErrorHandling.FormatExceptionMessage(exc)}"; Logging.AddLog(m); errors.AppendLine(m); } foreach (var s in itemBase.Log) { string m = $"{site.Identity}: {s}"; Logging.AddLog(m); errors.AppendLine(m); } if (itemBase.NextRun != null && (nextRun == null || (DateTime)itemBase.NextRun < nextRun)) { nextRun = itemBase.NextRun; } YetaWFManager.RemoveThreadInstance(); YetaWFManager.MakeThreadInstance(null, null, true);// restore scheduler's manager SchedulerLog.LimitTo(YetaWFManager.Manager); SchedulerLog.SetCurrent(); } } } else { IScheduling schedEvt = null; try { schedEvt = (IScheduling)Activator.CreateInstance(tp); } catch (Exception exc) { string m = $"Scheduler item '{item.Name}' could not be instantiated (Type={item.Event.ImplementingType}, Assembly={item.Event.ImplementingAssembly}) - {ErrorHandling.FormatExceptionMessage(exc)}"; Logging.AddLog(m); errors.AppendLine(m); } if (schedEvt != null) { SchedulerItemBase itemBase = new SchedulerItemBase { Name = item.Name, Description = item.Description, EventName = item.Event.Name, Enabled = true, Frequency = item.Frequency, Startup = item.Startup, SiteSpecific = false }; try { await schedEvt.RunItemAsync(itemBase); } catch (Exception exc) { string m = $"An error occurred in scheduler item '{item.Name}' - {ErrorHandling.FormatExceptionMessage(exc)}"; Logging.AddLog(m); errors.AppendLine(m); } foreach (var s in itemBase.Log) { Logging.AddLog(s); errors.AppendLine(s); } if (itemBase.NextRun != null && (nextRun == null || (DateTime)itemBase.NextRun < nextRun)) { nextRun = itemBase.NextRun; } } } TimeSpan diff = DateTime.UtcNow - now; item.RunTime = diff; { string m = $"Elapsed time for scheduler item '{item.Name}' was {diff} (hh:mm:ss.ms)."; Logging.AddLog(m); errors.AppendLine(m); } } catch (Exception exc) { string m = $"Scheduler item {item.Name} failed - {ErrorHandling.FormatExceptionMessage(exc)}"; Logging.AddErrorLog(m); errors.AppendLine(m); } if (item.RunOnce) { item.Enabled = false; } item.IsRunning = false; item.SetNextRuntime(); if (nextRun != null) { Logging.AddLog($"Next run at {nextRun} (UTC)"); item.Next = nextRun; item.Enabled = true; } item.Errors = errors.ToString(); try { await schedDP.UpdateItemAsync(item); } catch (Exception exc) { string m = $"Updating scheduler item {item.Name} failed - {ErrorHandling.FormatExceptionMessage(exc)}"; Logging.AddErrorLog(m); errors.AppendLine(m); } SchedulerLog.SetCurrent(); }