public void WorkflowTaskMonitorScheduleTest()
        {
            DeleteAll(Entities.account);

            var workflowName = "Test Account Target Schedule Failure";

            DeleteWorkflowTasks(workflowName);

            var account = CreateAccount();

            var scheduleFailWorkflow = GetWorkflow(workflowName);

            var initialThreshold = DateTime.UtcNow.AddDays(-3);

            var workflowTask = InitialiseValidWorkflowTask();

            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_targetworkflow, scheduleFailWorkflow);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, DateTime.UtcNow.AddDays(1));
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_name, workflowName);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationfortargetfailures, true);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationforschedulefailures, true);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsfrom, TestQueue);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TestQueue);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime, initialThreshold);
            workflowTask = CreateAndRetrieve(workflowTask);

            //wait until the monitor completed its first check - will respawn another the check it
            Thread.Sleep(10000);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, DateTime.UtcNow.AddDays(-2));
            workflowTask = UpdateFieldsAndRetreive(workflowTask, Fields.jmcg_workflowtask_.jmcg_nextexecutiontime);

            WaitTillTrue(() => WorkflowSchedulerService.GetRecurringInstancesFailed(workflowTask.Id).Any(), 60);

            WorkflowSchedulerService.StartNewMonitorWorkflowFor(workflowTask.Id);
            WaitTillTrue(() => GetRegardingEmails(workflowTask).Count() == 1, 60);
            WaitTillTrue(() => initialThreshold < Refresh(workflowTask).GetDateTimeField(Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime), 60);

            workflowTask = Refresh(workflowTask);
            Thread.Sleep(1000);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, DateTime.UtcNow.AddDays(-1));
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime, DateTime.UtcNow.AddDays(-2));
            workflowTask = UpdateFieldsAndRetreive(workflowTask, Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, Fields.jmcg_workflowtask_.jmcg_minimumschedulefailuredatetime);

            WorkflowSchedulerService.StartNewContinuousWorkflowFor(workflowTask.Id);
            WaitTillTrue(() => WorkflowSchedulerService.GetRecurringInstancesFailed(workflowTask.Id).Count() > 1, 60);

            WorkflowSchedulerService.StartNewMonitorWorkflowFor(workflowTask.Id);
            WaitTillTrue(() => GetRegardingEmails(workflowTask).Count() == 2, 60);

            WorkflowSchedulerService.StartNewMonitorWorkflowFor(workflowTask.Id);
            Thread.Sleep(60000);
            Assert.AreEqual(2, GetRegardingEmails(workflowTask).Count());

            XrmService.Delete(workflowTask);
            XrmService.Delete(account);
        }
 private void SpawnMonitorInstance()
 {
     if (IsMessage(PluginMessage.Create, PluginMessage.Update) && IsStage(PluginStage.PostEvent) &&
         IsMode(PluginMode.Synchronous))
     {
         if (BooleanChangingToTrue(Fields.jmcg_workflowtask_.jmcg_on))
         {
             WorkflowSchedulerService.StartNewMonitorWorkflowFor(TargetId);
         }
         if (BooleanChangingToFalse(Fields.jmcg_workflowtask_.jmcg_on))
         {
             WorkflowSchedulerService.StopMonitorWorkflowFor(TargetId);
         }
     }
 }
        public void WorkflowTaskMonitorOnlyTest()
        {
            var workflowName = "Test Account Target Failure";
            var taskName     = "Test Monitor Only";

            DeleteWorkflowTasks(taskName);

            var workflowWillFail = GetWorkflow(workflowName);
            //create the workflow task montior only
            var workflowTask = new Entity(Entities.jmcg_workflowtask);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_name, taskName);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_notesfortargetfailureemail, "These are some fake instructions\nto fix the problem");
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_targetworkflow, workflowWillFail);
            workflowTask.SetOptionSetField(Fields.jmcg_workflowtask_.jmcg_workflowexecutiontype,
                                           OptionSets.WorkflowTask.WorkflowExecutionType.MonitorOnly);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsfrom, TestQueue);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TestQueue);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_on, true);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_crmbaseurl, "jmcg_wstestsettings.jmcg_crminstanceurl");
            workflowTask = CreateAndRetrieve(workflowTask);

            //wait a second and verify only the montior workflow spawned
            //i.e this doesn't have a continuous workflow as it doesn't
            //run a process on a schedule it just monitors
            Thread.Sleep(5000);
            Assert.IsFalse(WorkflowSchedulerService.GetRecurringInstances(workflowTask.Id).Any());
            Assert.IsTrue(WorkflowSchedulerService.GetMonitorInstances(workflowTask.Id).Any());
            //stop the monitor we will start it again in a second
            WorkflowSchedulerService.StopMonitorWorkflowFor(workflowTask.Id);

            //run the workflow which will fail
            var account = CreateAccount();

            XrmService.StartWorkflow(workflowWillFail.Id, account.Id);
            //wait a second and spawn the monitor
            Thread.Sleep(5000);
            WorkflowSchedulerService.StartNewMonitorWorkflowFor(workflowTask.Id);
            //verify the notification email created
            WaitTillTrue(() => GetRegardingEmails(workflowTask).Any(), 60);

            DeleteMyToday();
        }
        public void WorkflowTaskMonitorTargetsTest()
        {
            var initialMinumumThreshold = DateTime.UtcNow.AddDays(-1);

            DeleteAll(Entities.account, true);
            var workflowName = "Test Account Target Failure";

            DeleteWorkflowTasks(workflowName);

            var scheduleFailWorkflow = GetWorkflow(workflowName);

            DeleteSystemJobs(scheduleFailWorkflow);

            var account = CreateAccount();

            var workflowTask = InitialiseValidWorkflowTask();

            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_targetworkflow, scheduleFailWorkflow);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, DateTime.UtcNow.AddDays(1));
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_name, workflowName);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationfortargetfailures, true);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_sendnotificationforschedulefailures, true);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsfrom, TestQueue);
            workflowTask.SetLookupField(Fields.jmcg_workflowtask_.jmcg_sendfailurenotificationsto, TestQueue);
            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_minimumtargetfailuredatetime, initialMinumumThreshold);
            workflowTask = CreateAndRetrieve(workflowTask);

            //wait until the monitor completed its first check - will respawn another the check it
            Thread.Sleep(10000);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, DateTime.UtcNow.AddDays(-2));
            workflowTask = UpdateFieldsAndRetreive(workflowTask, Fields.jmcg_workflowtask_.jmcg_nextexecutiontime);

            WaitTillTrue(() => Refresh(workflowTask).GetDateTimeField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime) > DateTime.UtcNow.AddDays(-1), 60);

            var workflow = CreateWorkflowInstance <TargetWorkflowTaskMonitorInstance>(workflowTask);

            WaitTillTrue(() => workflow.HasNewFailure(), 60);

            WorkflowSchedulerService.StartNewMonitorWorkflowFor(workflowTask.Id);

            //okay need to set conditions for the target having not executed
            WaitTillTrue(() => GetRegardingEmails(workflowTask).Any(), 60);

            WaitTillTrue(() => Refresh(workflowTask).GetDateTimeField(Fields.jmcg_workflowtask_.jmcg_minimumtargetfailuredatetime) > initialMinumumThreshold, 60);
            workflowTask = Refresh(workflowTask);
            workflow     = CreateWorkflowInstance <TargetWorkflowTaskMonitorInstance>(workflowTask);

            Assert.IsFalse(workflow.HasNewFailure());

            Thread.Sleep(1000);

            workflowTask.SetField(Fields.jmcg_workflowtask_.jmcg_nextexecutiontime, DateTime.UtcNow.AddDays(-2));
            workflowTask = UpdateFieldsAndRetreive(workflowTask, Fields.jmcg_workflowtask_.jmcg_nextexecutiontime);
            workflow     = CreateWorkflowInstance <TargetWorkflowTaskMonitorInstance>(workflowTask);

            WaitTillTrue(() => workflow.HasNewFailure(), 60);

            XrmService.Delete(workflowTask);
            XrmService.Delete(account);
        }
Example #5
0
 private void DoIt()
 {
     WorkflowSchedulerService.StartNewMonitorWorkflowFor(TargetId);
 }