private void UpdateRules(CacheChangeData update) { if (update.NewItem == null) { throw new ArgumentException("update.NewItem is null: " + update); } string ruleName = null; try { ruleName = update.NewItem.Name; switch (update.Change) { case CacheChange.CacheCleared: _ruleStore.Clear(); break; case CacheChange.ItemExpired: case CacheChange.ItemRemoved: { var oldExternalRule = (AlertRule)update.OldItem.Data; // stop old rule StopOldRule(oldExternalRule, null); Logger.LogDebug("Rule {0}: Removed.", oldExternalRule.PrivateKey); } break; case CacheChange.ItemCreated: { var newExternalRule = (AlertRule)update.NewItem.Data; var newInternalRule = new InternalRule(newExternalRule); StartNewRule(newExternalRule, newInternalRule); Logger.LogDebug("Rule {0}: Created.", newExternalRule.PrivateKey); } break; case CacheChange.ItemUpdated: { var oldExternalRule = (AlertRule)update.OldItem.Data; var newExternalRule = (AlertRule)update.NewItem.Data; var newInternalRule = new InternalRule(newExternalRule); StopOldRule(oldExternalRule, newInternalRule); StartNewRule(newExternalRule, newInternalRule); Logger.LogDebug("Rule {0}: Updated.", newExternalRule.PrivateKey); } break; } } catch (Exception ex) { ReportUncaughtError("UpdateRules", ruleName, ex, null); } }
private void StopOldRule(AlertRule oldExternalRule, InternalRule newInternalRule) { if (_ruleStore.TryGetValue(oldExternalRule.PrivateKey.ToLower(), out var oldInternalRule)) { if (newInternalRule != null) { newInternalRule.LastStatus = oldInternalRule.LastStatus; } oldInternalRule.DataSubs?.Cancel(); } // update rule store _ruleStore.Remove(oldExternalRule.PrivateKey.ToLower()); }
private void StartNewRule(AlertRule newExternalRule, InternalRule newInternalRule) { if (!newInternalRule.Disabled && newInternalRule.DataSubsExpr != null) { newInternalRule.DataSubs = IntClient.Target.CreateUntypedSubscription(null, newInternalRule.DataSubsExpr); newInternalRule.DataSubs.UserCallback = delegate(ISubscription subscription, ICoreItem item) { var rule = (InternalRule)subscription.UserContext; ICoreItem lastItem = rule.CurrentReceivedItem; if (lastItem == null || (item.Created > lastItem.Created)) { rule.CurrentReceivedItem = item; } }; newInternalRule.DataSubs.UserContext = newInternalRule; newInternalRule.DataSubs.DataTypeName = newInternalRule.DataTypeName; newInternalRule.DataSubs.WaitForExisting = true; newInternalRule.DataSubs.Start(); Thread.Sleep(2000); } // update rule store _ruleStore[newExternalRule.PrivateKey.ToLower()] = newInternalRule; }