/// <summary> /// Initializes the specified scheduler. /// </summary> /// <param name="scheduler">The scheduler.</param> public void Initialize(IScheduler scheduler) { var index = 0; foreach (var notificationRule in _notificationRuleRepository.Query()) { var parsedRule = _ruleParser.ParseNotificationRule(notificationRule.Query); if (!parsedRule.IsTimeTriggered) { continue; } var job = new NotificationPollJob(_notificationRuleRepository, _ruleParser, _ruleExecutorDirector) { Interval = parsedRule.Expression.Trigger.Repeat.Value }; var scheduledJob = JobBuilder .Create(job.GetType()) .WithIdentity("NotificationPollJob.Rule_{0}".FormatWith(notificationRule.Id)) .UsingJobData("NotificationRuleId", notificationRule.Id) .Build(); var trigger = TriggerBuilder .Create() .WithIdentity("NotificationPollTrigger.Rule_{0}".FormatWith(notificationRule.Id)) .StartAt(SystemTime.UtcNow().AddSeconds(20 * index)) .WithSimpleSchedule(x => x.WithInterval(job.Interval).RepeatForever()) .Build(); scheduler.ScheduleJob(scheduledJob, trigger); index++; } }
/// <summary> /// Executes the job. /// </summary> /// <returns> /// Positive value to repeat the task, negative value or 0 - to finish the task and to wait time interval before the /// next run. /// </returns> protected override int Process() { var ruleId = (long)JobExecutionContext.MergedJobDataMap.Get("NotificationRuleId"); var rule = _notificationRuleRepository.GetById(ruleId); var parsedRule = _ruleParser.ParseNotificationRule(rule.Query); _ruleExecutorDirector.Execute <INotificationRule, NotificationRuleResult>( parsedRule, new Dictionary <string, string> { { Variables.ProjectId, rule.ProjectId.ToString() } }); return(0); }
private void ProcessEventNotification(Event eventToHandle, NotificationRules notificationRule, IDictionary <string, string> data) { try { var parcedRule = _ruleParser.ParseNotificationRule(notificationRule.Query); var eventRule = parcedRule as IEventNotificationRule; if (eventRule == null) { _log.Warning("Event notification rule has incorrect type. Type='{0}'".FormatWith(parcedRule.GetType().FullName)); return; } if (!(eventRule.IsForAllEvents || (eventRule.HasDependentEvents && eventRule.IsEventMatch(eventToHandle.Key)))) { _log.Trace("Event notification rule skipped. Current event='{0}', Target event='{0}'".FormatWith( eventToHandle.Key, eventRule.GetDependentEvents.ToCommaSeparatedString())); return; } _ruleExecutorDirector.Execute <INotificationRule, NotificationRuleResult>((dynamic)eventRule, data); _log.Info("Event notification rule processed. Event='{0}', Notification rule Id='{1}', Notification rule name='{2}'" .FormatWith( eventToHandle.Key, notificationRule.Id, notificationRule.DisplayName)); } catch (Exception exc) { _log.Error( "Event notification rule processing has failed. Event='{0}', Notification rule Id='{1}', Notification rule name='{2}'" .FormatWith(eventToHandle.Key, notificationRule.Id, notificationRule.DisplayName), exc); } }