void heart_Beat(object sender, EventArgs e) { for (int i = 0; i < actions.Count; i++) { ScheduledAction action = actions[i]; if (action.ShouldExecute()) { action.IsExecuting = true; worker.DoWork(delegate { try { logger.Debug("Executing " + action.GetType().Name); action.Engine = engine; action.Execute(); action.ErrorCount = 0; } catch (Exception ex) { action.ErrorCount++; action.OnError(ex); // wayne: call custom action error handler } finally { try { IClosable closable = action as IClosable; if (closable != null) { closable.Dispose(); } } catch (Exception ex) { errorHandler.Notify(ex); } } action.LastExecuted = Utility.CurrentTime(); action.IsExecuting = false; try { context.Close(); } catch (Exception ex) { errorHandler.Notify(ex); } }); if (action.Repeat == Repeat.Once) { actions.RemoveAt(i); --i; } } } }
private IEnumerable <ScheduledAction> InstantiateActions(IPluginFinder plugins) { foreach (ScheduleExecutionAttribute attr in plugins.GetPlugins <ScheduleExecutionAttribute>()) { ScheduledAction action = (ScheduledAction)engine.Resolve(attr.Decorates); action.Interval = CalculateInterval(attr.Interval, attr.Unit); action.Repeat = attr.Repeat; yield return(action); } }
public Scheduler(IEngine engine, IHeart heart, IWorker worker, IWebContext context, IErrorNotifier errorHandler, ScheduledAction[] registeredActions, Configuration.EngineSection config) { this.engine = engine; this.heart = heart; this.worker = worker; this.context = context; this.errorHandler = errorHandler; this.enabled = config.Scheduler.Enabled; if (!string.IsNullOrEmpty(config.Scheduler.ExecuteOnMachineNamed)) if (config.Scheduler.ExecuteOnMachineNamed != Environment.MachineName) this.enabled = false; if (enabled) { actions = new List<ScheduledAction>(InstantiateActions(registeredActions, config.Scheduler)); } }
/// <summary>Executes the scheduled actions that are scheduled for executions.</summary> public void ExecuteActions() { if (!enabled) { return; } if (Debugger.IsAttached && !runWhileDebuggerAttached) { return; } for (int i = 0; i < actions.Count; i++) { ScheduledAction action = actions[i]; if (action.ShouldExecute()) { Action work = delegate { try { var config = ((System.Web.Configuration.GlobalizationSection)System.Configuration.ConfigurationManager.GetSection("system.web/globalization")); if (!string.IsNullOrEmpty(config.Culture)) { Thread.CurrentThread.CurrentCulture = new CultureInfo(config.Culture); } if (!string.IsNullOrEmpty(config.UICulture)) { Thread.CurrentThread.CurrentUICulture = new CultureInfo(config.UICulture); } } catch (Exception ex) { logger.Warn(ex); } try { logger.Debug("Executing " + action.GetType().Name); action.Engine = engine; action.Execute(); action.ErrorCount = 0; } catch (Exception ex) { action.ErrorCount++; action.OnError(ex); // wayne: call custom action error handler } finally { try { IClosable closable = action as IClosable; if (closable != null) { closable.Dispose(); } } catch (Exception ex) { errorHandler.Notify(ex); } } action.LastExecuted = Utility.CurrentTime(); action.IsExecuting = false; try { context.Close(); } catch (Exception ex) { errorHandler.Notify(ex); } }; action.IsExecuting = true; if (asyncActions) { worker.DoWork(work); } else { work(); } if (action.Repeat == Repeat.Once) { actions.RemoveAt(i); --i; } } } }