Esempio n. 1
0
        public void SuspendResume()
        {
            var handler = new RunTriggersHandler();

            var trigger = new WfTriggerUserUpdatesResource();

            trigger.Save();

            var resource = new Resource();

            resource.Save();

            var task = RunTriggersHandler.CreateBackgroundTask(trigger.Id, resource.Id, 5);

            var taskEntity = handler.CreateSuspendedTask(task);

            taskEntity.Save();

            var tasks = handler.RestoreSuspendedTasks();

            tasks.Should().HaveCount(1);

            var runParam = tasks.First().GetData <RunTriggersParams>();

            runParam.TriggerDepth.ShouldBeEquivalentTo(5);
            runParam.TriggerId.ShouldBeEquivalentTo(trigger.Id);
            runParam.EntityId.ShouldBeEquivalentTo(resource.Id);
        }
        private static void RunWorkflow(WfTriggerUserUpdatesResource trigger, EntityRef updatedEntity)
        {
            var wf = trigger.WorkflowToRun;

            if (wf != null)
            {
                var args = new Dictionary <string, object>();

                var actionArg = wf.InputArgumentForAction;

                if (actionArg != null && !string.IsNullOrEmpty(actionArg.Name))
                {
                    args.Add(actionArg.Name, updatedEntity.Entity);
                }

                WorkflowRunner.Instance.RunWorkflow(new WorkflowStartEvent(trigger.Cast <WfTrigger>())
                {
                    Arguments = args
                });
            }
            else
            {
                EventLog.Application.WriteWarning($"WorkflowTriggerHelper.RunWorkflow: Trigger is missing a workflow to run. This is a constraints violation, igoring trigger. Trigger: {trigger.Name ?? "[Unnamed]"}({trigger.Id})");
            }
        }
        private void Trigger_Old(WfTriggerUserUpdatesResource updateTrigger, IEntity entity, bool runInForeground)
        {
            Action action = () =>
            {
                try
                {
                    using (new SecurityBypassContext())
                    {
                        updateTrigger.ActionTrigger(entity);
                    }
                }
                catch (Exception ex)
                {
                    EventLog.Application.WriteError("Workflow trigger '{0}' on entity '{1}' failed: {2}",
                                                    new EntityRef(updateTrigger), new EntityRef(entity), ex);
                }
            };

            action = WrapActionForTriggerDepth(action);

            if (WorkflowRunContext.Current.RunTriggersInCurrentThread || runInForeground)
            {
                WorkflowRunContext.Current.DeferAction(() => action());
            }
            else
            {
                var wrappedAction = WrapActionForThread(action);

                using (var ctx = DatabaseContext.GetContext())
                {
                    ctx.AddPostDisposeAction(() =>                // make sure the second thread does not start till the current transaction has completed.
                                             WorkflowRunContext.Current.QueueAction(wrappedAction));
                }
            }
        }
        public void SetChoice( )
        {
            var trigger = new WfTriggerUserUpdatesResource( );

            trigger.Save( );

            ToDelete.Add(trigger.Id);


            var setAction = new SetChoiceActivity( );

            setAction.Save( );
            ToDelete.Add(setAction.Id);

            var setChoiceAs = setAction.As <WfActivity>( );

            ActivityImplementationBase nextActivity = setChoiceAs.CreateWindowsActivity( );

            var args = new Dictionary <string, object>
            {
                {
                    "Resource to Update", trigger
                },
                {
                    "Field to Update", new EntityRef("core:triggeringCondition").Entity
                },
                {
                    "New Value", new EntityRef("core:triggeredOnEnumCreate").Entity
                },
            };

            RunActivity(nextActivity, args);

            trigger = Entity.Get <WfTriggerUserUpdatesResource>(trigger.Id);

            Assert.IsNotNull(trigger.TriggeringCondition, "Triggering condition should be set");
            Assert.AreEqual("core:triggeredOnEnumCreate", trigger.TriggeringCondition.Alias);

            args = new Dictionary <string, object>
            {
                {
                    "Resource to Update", trigger
                },
                {
                    "Field to Update", new EntityRef("core:triggeringCondition").Entity
                },
                {
                    "New Value", new EntityRef("core:triggeredOnEnumUpdate").Entity
                },
                {
                    "Replace Existing Values", false
                }                 // should ignore this value
            };

            RunActivity(nextActivity, args);

            Assert.IsNotNull(trigger.TriggeringCondition, "Triggering condition should be set");
            Assert.AreEqual("core:triggeredOnEnumUpdate", trigger.TriggeringCondition.Alias);
        }
Esempio n. 5
0
        // [Ignore("Failing on first run in test environment.")]
        public void TestWorkflowRunOnReverseRelationshipUpdate()
        {
            using (new WorkflowRunContext {
                RunTriggersInCurrentThread = true
            })
            {
                var ageField = GetPersonField("Age");
                var titleRel = GetTitleRel();

                var myType = CreateMyType("Type_TestWorkflowRunOnUpdate");

                Workflow wf = CreateTestWorkflow("TestWorkflowRunOnRelationshipUpdate " + DateTime.Now, "990");

                var updateTrigger = new WfTriggerUserUpdatesResource
                {
                    WorkflowToRun = wf,
                    // ReSharper disable SpecifyACultureInStringConversionExplicitly
                    Name = "Test" + DateTime.Now.ToString(),
                    // ReSharper restore SpecifyACultureInStringConversionExplicitly
                    TriggeringCondition_Enum = TriggeredOnEnum_Enumeration.TriggeredOnEnumUpdate,
                    TriggerEnabled           = true
                };

                var task = new DisplayFormUserTask();

                var relToUpdate = Entity.Get <Relationship>("core:recordToPresent");
                updateTrigger.UpdatedRelationshipsToTriggerOn.Add(relToUpdate);         // a reverse relationship
                updateTrigger.TriggeredOnType = myType;

                updateTrigger.Save();
                ToDelete.Add(updateTrigger.Id);
                ToDelete.Add(wf.Id);

                EntityRef employee = CreateResource(myType);

                var savedEmployee = Entity.Get(employee);
                var age           = savedEmployee.GetField <int?>(ageField);
                Assert.AreEqual(null, age, "Age has been not been set by workflow");

                var rels = savedEmployee.GetRelationships(relToUpdate);

                rels.Add(task);

                using (new WorkflowRunContext(true))
                    using (var ctx = DatabaseContext.GetContext(true, preventPostSaveActionsPropagating: true))
                    {
                        savedEmployee.Save();

                        ctx.CommitTransaction();
                    }

                savedEmployee = Entity.Get(employee);
                var age1 = savedEmployee.GetField <int?>(ageField);
                Assert.That(age1, Is.EqualTo(990), "The update has not occurred and has not triggered the workflow.");
            }
        }
Esempio n. 6
0
        public void MissingEntity()
        {
            var trigger = new WfTriggerUserUpdatesResource();

            var taskData = new RunTriggersParams {
                TriggerId = trigger.Id, EntityId = 9999999
            };

            RunAndTestLog(taskData, (eventLogMonitor) =>
            {
                Assert.That(eventLogMonitor.Entries.Any(l => l.Message.Contains("Entity missing")));
            });
        }
        private void Trigger_New(WfTriggerUserUpdatesResource updateTrigger, IEntity entity, bool runInForeground)
        {
            var bgTask = RunTriggersHandler.CreateBackgroundTask(updateTrigger.Id, entity.Id);


            if (WorkflowRunContext.Current.RunTriggersInCurrentThread || runInForeground)
            {
                WorkflowRunContext.Current.DeferAction(() => Factory.BackgroundTaskManager.ExecuteImmediately(bgTask));
            }
            else
            {
                using (var ctx = DatabaseContext.GetContext())
                {
                    ctx.AddPostDisposeAction(() => Factory.BackgroundTaskManager.EnqueueTask(bgTask));
                }
            }
        }
Esempio n. 8
0
        protected ResourceTriggerFilterDef CreateTrigger(string name, EntityType triggeredOn, Workflow wf, bool enabled = true)
        {
            var trigger = new WfTriggerUserUpdatesResource()
            {
                Name           = name,
                TriggerEnabled = enabled, WorkflowToRun = wf, TriggeringCondition = Entity.Get <TriggeredOnEnum>("core:triggeredOnEnumCreateUpdate")
            };

            if (triggeredOn != null)
            {
                trigger.TriggeredOnType = triggeredOn;
            }

            trigger.Save();
            ToDelete.Add(trigger.Id);

            return(trigger.Cast <ResourceTriggerFilterDef>());
        }
Esempio n. 9
0
        public void TestForceDeadlockOnWorkflowRunOnCreate()
        {
            var      ageField = GetPersonField("Age");
            Workflow wf       = CreateTestWorkflow("TestForceDeadlockOnWorkflowRunOnCreate" + DateTime.Now, "999");
            var      empType  = new EntityType();

            empType.Name = "MyType" + DateTime.Now.Ticks;
            empType.Inherits.Add(GetEntityType("AA_Employee"));
            empType.Save();
            ToDelete.Add(empType.Id);


            var trigger = new WfTriggerUserUpdatesResource
            {
                WorkflowToRun            = wf,
                TriggeringCondition_Enum = TriggeredOnEnum_Enumeration.TriggeredOnEnumCreate,
                TriggerEnabled           = true
            };

            trigger.TriggeredOnType = empType;

            trigger.Save();
            ToDelete.Add(trigger.Id);
            ToDelete.Add(wf.Id);

            EntityRef employeeRef = null;

            for (var i = 0; i < 20; i++)
            {
                // Ensure an employee triggers the workflow
                employeeRef = CreateResource(empType);
                ToDelete.Add(employeeRef.Id);
            }

            if (!WaitUntil(() =>
            {
                var savedEmployee = Entity.Get(employeeRef);
                var age = savedEmployee.GetField <int?>(ageField);
                return(age == 999);
            }, 50, 200))
            {
                Assert.Fail("Age has been set by workflow");
            }
        }
Esempio n. 10
0
        // [Ignore("Failing on first run in test environment.")]
        public void TestWorkflowRunOnRelationshipUpdate()
        {
            using (new WorkflowRunContext {
                RunTriggersInCurrentThread = true
            })
            {
                var ageField = GetPersonField("Age");
                var titleRel = GetTitleRel();

                var myType = CreateMyType("Type_TestWorkflowRunOnUpdate");

                Workflow wf = CreateTestWorkflow("TestWorkflowRunOnRelationshipUpdate " + DateTime.Now, "990");

                var updateTrigger = new WfTriggerUserUpdatesResource
                {
                    WorkflowToRun = wf,
                    // ReSharper disable SpecifyACultureInStringConversionExplicitly
                    Name = "Test" + DateTime.Now.ToString(),
                    // ReSharper restore SpecifyACultureInStringConversionExplicitly
                    TriggeringCondition_Enum = TriggeredOnEnum_Enumeration.TriggeredOnEnumUpdate,
                    TriggerEnabled           = true
                };


                updateTrigger.UpdatedRelationshipsToTriggerOn.Add(titleRel);
                updateTrigger.TriggeredOnType = myType;

                updateTrigger.Save();
                ToDelete.Add(updateTrigger.Id);
                ToDelete.Add(wf.Id);

                EntityRef employee = CreateResource(myType);

                var savedEmployee = Entity.Get(employee, true);
                var age           = savedEmployee.GetField <int?>(ageField);
                Assert.AreEqual(null, age, "Age has been not been set by workflow");

                UpdateEmployeeRelationship(savedEmployee.Id, titleRel);

                savedEmployee = Entity.Get(employee);
                var age1 = savedEmployee.GetField <int?>(ageField);
                Assert.That(age1, Is.EqualTo(990), "The update has not occurred and has not triggered the workflow.");
            }
        }
Esempio n. 11
0
        public void Run()
        {
            var wf = CreateLoggingWorkflow();

            wf.Save();

            var trigger = new WfTriggerUserUpdatesResource {
                WorkflowToRun = wf
            };

            trigger.Save();

            var taskData = new RunTriggersParams {
                TriggerId = trigger.Id, EntityId = EntityType.EntityType_Type.Id
            };

            RunAndTestLog(taskData, (eventLogMonitor) =>
            {
                Assert.That(wf.RunningInstances.Count(), Is.EqualTo(1));
                Assert.That(wf.RunningInstances.First().WorkflowRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunCompleted));
            });
        }
Esempio n. 12
0
        //[Ignore("Failing on first run in test environment.")]
        public void TestWorkflowRunOnCreate( )
        {
            using (new WorkflowRunContext {
                RunTriggersInCurrentThread = true
            })
            {
                var ageField = GetPersonField("Age");
                var myType   = CreateMyType("Type_TestWorkflowRunOnUpdate");

                Workflow wf = CreateTestWorkflow("TestWorkflowRunOnCreate" + DateTime.Now, "995");

                var trigger = new WfTriggerUserUpdatesResource
                {
                    WorkflowToRun            = wf,
                    TriggeringCondition_Enum = TriggeredOnEnum_Enumeration.TriggeredOnEnumCreate,
                    TriggerEnabled           = true
                };
                trigger.TriggeredOnType = myType;

                trigger.Save();
                ToDelete.Add(trigger.Id);
                ToDelete.Add(wf.Id);

                // Ensure an employee triggers the workflow
                EntityRef employeeRef = CreateResource(myType);

                var savedEmployee = Entity.Get(employeeRef);
                var age1          = savedEmployee.GetField <int?>(ageField);

                Assert.That(age1, Is.EqualTo(995), "Ensure Age has not been set by workflow");

                EntityRef personRef = CreateResource(Person.Person_Type);

                var savedPerson = Entity.Get(personRef);
                var age2        = savedPerson.GetField <int?>(ageField);
                Assert.That(age2, Is.Null, "Ensure Age has not been set by workflow");
            }
        }
        public static void ActionTrigger(this WfTriggerUserUpdatesResource trigger, IEntity entity)
        {
            perfCounters.GetPerformanceCounter <RatePerSecond32PerformanceCounter>(WorkflowPerformanceCounters.TriggerRateCounterName).Increment();

            RunWorkflow(trigger, entity.Id);
        }
Esempio n. 14
0
        public void TestWorkflowRunOnUpdate()
        {
            using (new WorkflowRunContext {
                RunTriggersInCurrentThread = true
            })
            {
                var emailField = GetPersonField("Email Address");
                var ageField   = GetPersonField("Age");
                var fnField    = GetPersonField("First Name");
                var lnField    = GetPersonField("Last Name");

                var myType = CreateMyType("Type_TestWorkflowRunOnUpdate");

                Workflow wf = CreateTestWorkflow("TestWorkflowRunOnUpdate" + DateTime.Now, "991");

                var updateTrigger = new WfTriggerUserUpdatesResource
                {
                    WorkflowToRun = wf,
                    // ReSharper disable SpecifyACultureInStringConversionExplicitly
                    Name = "Test" + DateTime.Now.ToString(),
                    // ReSharper restore SpecifyACultureInStringConversionExplicitly
                    TriggeringCondition_Enum = TriggeredOnEnum_Enumeration.TriggeredOnEnumUpdate,
                    TriggerEnabled           = true
                };

                updateTrigger.UpdatedFieldsToTriggerOn.Add(emailField.As <Field>());
                updateTrigger.TriggeredOnType = myType;

                updateTrigger.Save();
                ToDelete.Add(updateTrigger.Id);
                ToDelete.Add(wf.Id);

                // Ensure an employee triggers the workflow
                var employee = Entity.Create(myType).Cast <Resource>();
                employee.SetField(fnField, "Test Firstname" + DateTime.Now);
                employee.SetField(lnField, "Test Lastname" + DateTime.Now);
                employee.SetField(ageField, 1);

                using (var ctx = DatabaseContext.GetContext(true, preventPostSaveActionsPropagating: true))
                {
                    employee.Save();

                    ctx.CommitTransaction();
                }

                ToDelete.Add(employee.Id);

                //Thread.Sleep(2000); // Give the workflow time to finish if it started

                var savedEmployee = Entity.Get(employee);
                var age           = savedEmployee.GetField <int?>(ageField);

                Assert.AreEqual(1, age, "Age has been not been set by workflow");

                UpdateEmployeeField(savedEmployee.Id, ageField, 10);

                //Thread.Sleep(2000); // Give the workflow time to finish if it started

                savedEmployee = Entity.Get(savedEmployee.Id);
                age           = savedEmployee.GetField <int?>(ageField);
                Assert.AreEqual(10, age, "The update has occurred but has not triggered the workflow.");

                UpdateEmployeeField(savedEmployee.Id, emailField, "*****@*****.**");

                savedEmployee = Entity.Get(employee);
                age           = savedEmployee.GetField <int?>(ageField);
                Assert.That(age, Is.EqualTo(991), "The update has occurred and has triggered the workflow.");
            }
        }