public void UpdatePlanActionStatus(string planUniqueName, long planInstanceId, ActionItem actionItem)
        {
            ActionUpdateItem item = new ActionUpdateItem()
            {
                PlanUniqueName = planUniqueName,
                PlanInstanceId = planInstanceId,
                ActionItem     = actionItem
            };

            if (ProcessActionsOnSingleton)
            {
                ActionItemSingletonProcessor.Instance.Queue.Enqueue(item);
            }
            else
            {
                UpdatePlanActionStatus(item);
            }
        }
Exemplo n.º 2
0
        void DrainQueue()
        {
            while (true)
            {
                if (Instance.Queue.Count == 0)
                {
                    Thread.Sleep(500);   //no pending actions available, pause
                    if (_allowExit)
                    {
                        ReadyToExit = true;
                    }
                    continue;
                }
                _allowExit = true;

                ActionUpdateItem item = null;
                while (Instance.Queue.TryDequeue(out item))
                {
                    _dal.UpdatePlanActionStatus(item);
                }
            }
        }
        public void UpdatePlanActionStatus(ActionUpdateItem item)
        {
            IMongoCollection <Plan>       hist  = _db.GetCollection <Plan>(_hist);
            IMongoCollection <ActionPath> paths = _db.GetCollection <ActionPath>(_paths);

            long       planInstanceId         = item.PlanInstanceId;
            ActionItem actionItem             = item.ActionItem;
            long       actionInstanceId       = actionItem.InstanceId;
            long       actionParentInstanceId = actionItem.ParentInstanceId;
            string     key = $"{planInstanceId}_{actionInstanceId}";

            FilterDefinition <ActionPath> pathFilter =
                Builders <ActionPath> .Filter.Where(p => p.Key == key);

            List <ActionPath> pathList = paths.Find(pathFilter).ToList();
            string            nodePath = pathList.Count > 0 ? pathList[0].Path : null;

            try
            {
                if (nodePath == null)
                {
                    //default: actionParentInstanceId == 0
                    string updatePath = "Actions";

                    if (actionParentInstanceId != 0)
                    {
                        FilterDefinition <ActionPath> parFilter =
                            Builders <ActionPath> .Filter.Where(p => p.Key == $"{planInstanceId}_{actionParentInstanceId}");

                        List <ActionPath> parList = paths.Find(parFilter).ToList();

                        if (parList.Count > 0)
                        {
                            updatePath = parList[0].Path + ".Actions";
                        }
                        else
                        {
                            throw new Exception($"Could not find Plan.InstanceId = [{planInstanceId}], Action:{actionItem.Name}.ParentInstanceId = [{actionItem.ParentInstanceId}] in paths table.");
                        }
                    }

                    FilterDefinition <Plan> pf = Builders <Plan> .Filter.Where(p => p.InstanceId == planInstanceId);

                    hist.FindOneAndUpdate(pf,
                                          Builders <Plan> .Update.Push(updatePath, actionItem),
                                          new FindOneAndUpdateOptions <Plan, object>()
                    {
                        IsUpsert = true
                    });

                    Plan        plan  = null;
                    List <Plan> plans = hist.Find(pf).ToList();
                    if (plans.Count > 0)
                    {
                        plan = plans[0];
                    }
                    else
                    {
                        throw new Exception($"Could not find Plan.InstanceId = [{planInstanceId}] in history table.");
                    }

                    string path = DalUtilities.GetActionInstanceMaterialzedPath(actionInstanceId, plan.Actions);
                    paths.InsertOne(new ActionPath()
                    {
                        Key = key, Path = path
                    });
                }
                else
                {
                    hist.FindOneAndUpdate(Builders <Plan> .Filter.And(
                                              Builders <Plan> .Filter.Where(p => p.InstanceId == planInstanceId),
                                              Builders <Plan> .Filter.Eq($"{nodePath}.InstanceId", actionInstanceId),
                                              Builders <Plan> .Filter.Lt($"{nodePath}.Result.Status", actionItem.Result.Status)),
                                          //end of filter above, set starts here:
                                          Builders <Plan> .Update.Set($"{nodePath}.Result", actionItem.Result),
                                          new FindOneAndUpdateOptions <Plan, object>()
                    {
                        IsUpsert = false
                    });
                }
            }
            catch (Exception ex)
            {
                ActionItemSingletonProcessor.Instance.Exceptions.Enqueue(ex);

                if (item.RetryAttempts++ < 5)
                {
                    ActionItemSingletonProcessor.Instance.Queue.Enqueue(item);
                }
                else
                {
                    ActionItemSingletonProcessor.Instance.Fatal.Enqueue(ex);
                }
            }
        }