internal bool Fire(RealPlugin p, Context ctx, RealPlugin.MutexInformation mtx) { try { if (mtx == null && _MutexToCapture != "") { string mn = ctx.EvaluateStringExpression(TriggerContextLogger, p, _MutexToCapture); RealPlugin.MutexInformation mi = ctx.plug.GetMutex(mn); RealPlugin.MutexTicket m = mi.QueueForAcquisition(ctx); Task t = new Task(() => { using (m) { DeferredFire(ctx.plug, ctx, mi, m); } }); t.Start(); return(true); } if ((ctx.force & Action.TriggerForceTypeEnum.SkipConditions) == 0) { if (Condition != null && Condition.Enabled == true) { if (Condition.CheckCondition(ctx, TriggerContextLogger, ctx.plug) == false) { AddToLog(p, RealPlugin.DebugLevelEnum.Info, I18n.Translate("internal/Trigger/trignotfired", "Trigger '{0}' not fired, condition not met", LogName)); return(false); } } } DateTime prevLastFired = LastFired; LastFired = DateTime.Now; if (_PeriodRefire == RefireEnum.Deny) { RefireDelayedUntil = LastFired.AddMilliseconds(ctx.EvaluateNumericExpression(TriggerContextLogger, p, _RefirePeriodExpression)); AddToLog(p, RealPlugin.DebugLevelEnum.Info, I18n.Translate("internal/Trigger/delayingrefire", "Delaying trigger '{0}' refire to {1}", LogName, RefireDelayedUntil)); } else { RefireDelayedUntil = DateTime.MinValue; } DateTime curtime = DateTime.Now; if (_Scheduling == SchedulingEnum.FromLastAction) { // get the last queued action as curTime lock (ctx.plug.ActionQueue) { var ixy = from ax in ctx.plug.ActionQueue where ax.ctx.trig.Id == Id orderby ax.when descending select ax; if (ixy.Count() > 0) { curtime = ixy.ElementAt(0).when; AddToLog(p, RealPlugin.DebugLevelEnum.Info, I18n.Translate("internal/Trigger/lastactionfound", "Last action for trigger '{0}' found at {1}", LogName, curtime)); } } } else if (_Scheduling == SchedulingEnum.FromRefirePeriod) { curtime = prevLastFired.AddMilliseconds(ctx.EvaluateNumericExpression(TriggerContextLogger, p, _RefirePeriodExpression)); if (curtime < LastFired) { curtime = LastFired; AddToLog(p, RealPlugin.DebugLevelEnum.Verbose, I18n.Translate("internal/Trigger/beforelastfired", "Current time is before last fired for trigger '{0}'", LogName)); } } if (_PrevActions == PrevActionsEnum.Interrupt) { int exx = 0; lock (ctx.plug.ActionQueue) { var ixy = from ax in ctx.plug.ActionQueue where ax.ctx.trig.Id == Id select ax; if (ixy.Count() > 0) { List <RealPlugin.QueuedAction> rems = new List <RealPlugin.QueuedAction>(); rems.AddRange(ixy); foreach (RealPlugin.QueuedAction qa in rems) { ctx.plug.ActionQueue.Remove(qa); exx++; } } } if (exx > 0) { if (_PrevActionsRefire == RefireEnum.Deny) { AddToLog(p, RealPlugin.DebugLevelEnum.Info, I18n.Translate("internal/Trigger/removefromqueuenorefire", "Removed {0} instance(s) of trigger '{1}' actions from queue, refire denied", exx, LogName)); return(false); } else { AddToLog(p, RealPlugin.DebugLevelEnum.Info, I18n.Translate("internal/Trigger/removefromqueue", "Removed {0} instance(s) of trigger '{1}' actions from queue", exx, LogName)); } } } else if (_PrevActionsRefire == RefireEnum.Deny) { int exx = 0; lock (ctx.plug.ActionQueue) { var ixy = from ax in ctx.plug.ActionQueue where ax.ctx.trig.Id == Id select ax; exx = ixy.Count(); } if (exx > 0) { AddToLog(p, RealPlugin.DebugLevelEnum.Info, I18n.Translate("internal/Trigger/refiredenied", "{0} instance(s) of trigger '{1}' actions in queue, refire denied", exx, LogName)); return(false); } } QueueActions(ctx, curtime, mtx); } catch (Exception ex) { AddToLog(p, RealPlugin.DebugLevelEnum.Error, I18n.Translate("internal/Trigger/firingexception", "Trigger '{0}' didn't fire due to exception: {1}", LogName, ex.Message)); } return(false); }
internal void DeferredFire(RealPlugin p, Context ctx, RealPlugin.MutexInformation mi, RealPlugin.MutexTicket m) { using (m) { mi.Acquire(ctx, m); if (Fire(p, ctx, mi) == false) { mi.Release(ctx); } } }