Example #1
0
        /// <summary>
        /// Executes the specified context.
        /// </summary>
        /// <param name="jobContext">The context.</param>
        public void Execute(JobExecutionContext jobContext)
        {
            var stopwatch = Stopwatch.StartNew();
            Ioc.SatisfyImportsOnce(this);

            Logger.Log(LogSeverity.Information, "ActionManagerJob", "Starting Action Manager Job");
            using (new BypassPropertyCheckContext())
            {
                var context = new ActionManagerJobContext();

                try
                {
                    var rules = GenerateSchedulerTable.GetRulesFromDb().ToArray();
                    var processNames = rules.Select(r => r.ProcessSystemName).Distinct();

                    foreach (var processName in processNames)
                    {
                        try
                        {
                            var ruleGuids = new HashSet<Guid>(rules.Where(r => r.ProcessSystemName == processName).Select(r => r.RuleGuid));
                            ExecuteScheduledEvents(processName, ruleGuids, context);
                        }
                        catch (Exception ex)
                        {
                            context.HasErrors = true;
                            Logger.Log(LogSeverity.Error, ActionManagerSource, ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    context.HasErrors = true;
                    Logger.Log(LogSeverity.Error, ActionManagerSource, ex);
                }
                finally
                {
                    stopwatch.Stop();
                    var message = string.Format(
                        CultureInfo.InvariantCulture,
                        "Finished Action Manager Job {0} in {1} seconds.",
                        context.HasErrors ? "With errors" : "without errors",
                        stopwatch.Elapsed.TotalSeconds);
                    Logger.Log(LogSeverity.Information, ActionManagerSource, message);
                }
            }
        }
Example #2
0
        private void ExecuteScheduledEvents(string processName, ICollection<Guid> ruleGuids, ActionManagerJobContext context)
        {
            var rules = GetRules(processName, ruleGuids);
            var scheduledActions = GetScheduledActions(processName, ruleGuids);
            var scheduledTriggers = GetScheduledTriggers(processName, ruleGuids);

            if (scheduledTriggers.Count == 0 && scheduledActions.Count == 0)
            {
                return;
            }

            var dataContext = new DataContext(processName, TheDynamicTypeManager);
            var ruleResults = new Dictionary<Guid, bool>();
            var triggersToExecute = new List<DataTriggerDefinition>();
            var actionsToExecute = new List<IActionDefinition>();
            var infoList = TheDynamicTypeManager.GetInfoList<IInfoClass>(processName, string.Empty, 0, int.MaxValue, null, null, true);

            foreach (var infoItem in infoList)
            {
                try
                {
                    dataContext.InfoItem = infoItem;
                    triggersToExecute.Clear();
                    actionsToExecute.Clear();

                    foreach (var rule in rules)
                    {
                        if (rule.CanUseInfoObjects)
                        {
                            ruleResults[rule.Guid] = rule.CanExecute(dataContext.InfoItem, dataContext.InfoItem);
                        }
                        else
                        {
                            ruleResults[rule.Guid] = rule.CanExecute(dataContext.EditItem, dataContext.EditItem);
                        }
                    }

                    foreach (var triggerDefinition in scheduledTriggers)
                    {
                        bool result;
                        if (triggerDefinition.Rules.Any(r => ruleResults.TryGetValue(r.Guid, out result) && result))
                        {
                            triggersToExecute.Add(triggerDefinition);
                        }
                    }

                    foreach (var actionDefinition in scheduledActions)
                    {
                        bool result;
                        if (actionDefinition.Rules.Any(r => ruleResults.TryGetValue(r.Guid, out result) && result))
                        {
                            actionsToExecute.Add(actionDefinition);
                        }
                    }

                    foreach (var actionDefinition in scheduledActions)
                    {
                        try
                        {
                            if (actionDefinition.CanUseInfoObjectsInRemoveOldActionItems)
                            {
                                actionDefinition.RemoveOldActionItems(dataContext.InfoItem, dataContext.InfoItem);
                            }
                            else
                            {
                                actionDefinition.RemoveOldActionItems(dataContext.EditItem, dataContext.EditItem);
                            }
                        }
                        catch (ItemNotFoundException)
                        {
                            throw;
                        }
                        catch (Exception ex)
                        {
                            context.HasErrors = true;
                            Logger.Log(LogSeverity.Error, ActionManagerSource, ex);
                        }
                    }

                    foreach (var actionDefinition in actionsToExecute)
                    {
                        try
                        {
                            actionDefinition.Execute(dataContext.EditItem, dataContext.EditItem);
                        }
                        catch (ItemNotFoundException)
                        {
                            throw;
                        }
                        catch (Exception ex)
                        {
                            context.HasErrors = true;
                            Logger.Log(LogSeverity.Error, ActionManagerSource, ex);
                        }
                    }

                    if (triggersToExecute.Any())
                    {
                        ExecuteTriggers(dataContext, triggersToExecute);
                    }
                }
                catch (ItemNotFoundException)
                {
                }
                catch (Exception ex)
                {
                    context.HasErrors = true;
                    Logger.Log(LogSeverity.Error, ActionManagerSource, ex);
                }
            }
        }