示例#1
0
        protected override void Execute()
        {
            try
            {
                //Get the items to start processing to start processing

                var items = this.workflowService.GetNextBatchToProcess().TrackActions;

                if (items.IsAny())
                {
                    while (true)
                    {
                        var errorMessages = new List <TrackActionLog>();
                        foreach (var item in items.OrderBy(a => a.TrackActionID))
                        {
                            try
                            {
                                var  state      = default(State);
                                bool hasMatched = false;
                                if (item != null && item.WorkflowAction != null)
                                {
                                    Logger.Current.Informational("TrackActionID :" + item.WorkflowAction.WorkflowActionID + " ContactID:" + item.TrackMessage.ContactId);
                                }
                                hasMatched = workflowService.HasContactMatchedEndTrigger(item.TrackMessage.ContactId, item.WorkflowID, item.TrackMessageID);
                                if (!hasMatched)
                                {
                                    switch ((Entities.WorkflowActionType)item.WorkflowActionTypeID)
                                    {
                                    case Entities.WorkflowActionType.SendCampaign:
                                        var workflowCampaignAction = (item.WorkflowAction as WorkflowCampaignAction);
                                        state = new SendCampaignState(workflowCampaignAction.CampaignID, item.WorkflowID, item.Workflow.CreatedBy, item.ActionID,
                                                                      workflowCampaignAction.Links != null && workflowCampaignAction.Links.Any() ? workflowCampaignAction.Links.Select(l => l.LinkID) : new List <int>(),
                                                                      campaignService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.SendEmail:
                                        var action = item.WorkflowAction as WorkflowEmailNotificationAction;
                                        state = new SendEmailState(item.WorkflowID, action.WorkflowActionID, action.Body, action.Subject, action.FromEmailID, workflowService, communicationService);
                                        break;

                                    case Entities.WorkflowActionType.NotifyUser:
                                        //Added 5 milli seconds wait time in-order to make notify emails pull fresh data. NEXG-2697
                                        System.Threading.Thread.Sleep(5000);
                                        var workflowNotifyUserAction = (item.WorkflowAction as WorkflowNotifyUserAction);
                                        state = new NotifyUserState(workflowNotifyUserAction.UserID, workflowNotifyUserAction.NotificationFieldID, item.WorkflowID, item.Workflow.WorkflowName, item.Workflow.CreatedBy, item.ActionID,
                                                                    workflowNotifyUserAction.MessageBody, workflowNotifyUserAction.NotifyType, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.AddTag:
                                        state = new TagAddedState((item.WorkflowAction as WorkflowTagAction).TagID, item.WorkflowID, item.Workflow.CreatedBy, item.ActionID, tagService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.RemoveTag:
                                        state = new TagRemovedState((item.WorkflowAction as WorkflowTagAction).TagID, item.WorkflowID, item.Workflow.CreatedBy, item.ActionID, tagService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.AdjustLeadScore:
                                        state = new AdjustLeadScoreState((item.WorkflowAction as WorkflowLeadScoreAction).LeadScoreValue, item.WorkflowID, item.Workflow.CreatedBy, item.TrackMessage.AccountId, item.ActionID, leadScoreService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.ChangeLifecycle:
                                        state = new LifecycleChangedState((item.WorkflowAction as WorkflowLifeCycleAction).LifecycleDropdownValueID, item.WorkflowID, item.Workflow.CreatedBy, item.ActionID, contactService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.UpdateField:
                                        var workflowContactFieldAction = item.WorkflowAction as WorkflowContactFieldAction;
                                        state = new UpdateFieldState(workflowContactFieldAction.FieldID, item.WorkflowID, item.ActionID, workflowContactFieldAction.FieldValue, (int)workflowContactFieldAction.FieldInputTypeId, contactService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.AssignToUser:
                                        var assignUser = (item.WorkflowAction as WorkflowUserAssignmentAction);
                                        state = new AssignUserState(assignUser.WorkflowUserAssignmentActionID, assignUser.ScheduledID, item.WorkflowID, item.Workflow.CreatedBy, item.ActionID, contactService, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.TriggerWorkflow:
                                        state = new TriggerWorkflowState(item.WorkflowID, (item.WorkflowAction as TriggerWorkflowAction).SiblingWorkflowID, item.ActionID, workflowService, messageService);
                                        break;

                                    case Entities.WorkflowActionType.WorkflowEndState:
                                    case Entities.WorkflowActionType.LinkActions:
                                        state = new WorkflowEndState(item.WorkflowID, item.ActionID, workflowService);
                                        break;

                                    case Entities.WorkflowActionType.SetTimer:
                                        state = new WaitingPeriodState(item.ActionID, workflowService);
                                        break;

                                    default:
                                        //Handle exception here
                                        break;
                                    }
                                    if (state != null)
                                    {
                                        state.OnEntry(ToMessage(item.TrackMessage, item.WorkflowID));
                                    }
                                    item.ActionProcessStatusID = Entities.TrackActionProcessStatus.Executed;
                                    item.ExecutedOn            = DateTime.UtcNow;
                                }
                                else
                                {
                                    item.ActionProcessStatusID = Entities.TrackActionProcessStatus.Termintaed;
                                    item.ExecutedOn            = DateTime.UtcNow;
                                }
                            }
                            catch (Exception ex)
                            {
                                var message = string.Format("Error while executing {0} workflow - {1} action id - {2} track message, {4} - action type. Error - {3}", item.WorkflowID, item.TrackActionID, item.TrackMessage.TrackMessageID, ex.Message, ((Entities.WorkflowActionType)item.WorkflowActionTypeID).ToString());
                                Logger.Current.Error(message, ex);
                                item.ActionProcessStatusID = Entities.TrackActionProcessStatus.Error;
                                item.ExecutedOn            = DateTime.UtcNow;
                                errorMessages.Add(new TrackActionLog {
                                    TrackActionID = item.TrackActionID, ErrorMessage = ex.Message
                                });
                            }
                            try
                            {
                                accountService.ScheduleAnalyticsRefresh(item.WorkflowID, (byte)IndexType.Workflows);
                            }
                            catch (Exception wex)
                            {
                                Logger.Current.Error("Error while inserting to refresh analytics - workflows", wex);
                            }
                        }
                        workflowService.UpdateActionBatchStatus(new ApplicationServices.Messaging.WorkFlow.UpdateActionBatchStatusRequest {
                            TrackActions = items, TrackActionLogs = errorMessages
                        });

                        //this.UpdateLastNotifyDateTime();
                        //Check for next iteration
                        items = this.workflowService.GetNextBatchToProcess().TrackActions;
                        if (items.IsAny() == false)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Current.Error("Automation Error - ", ex);
            }
        }
示例#2
0
        protected override void ExecuteInternal(IJobExecutionContext context)
        {
            //Get the items to start processing to start processing
            var trackActions = _workflowService.GetNextBatchToProcess()
                               .TrackActions
                               .OrderBy(a => a.TrackActionID)
                               .Take(_jobConfig.WorkflowActionChunkSize)
                               .ToArray();

            if (!trackActions.IsAny())
            {
                return;
            }

            var errorMessages = new List <TrackActionLog>();

            foreach (var trackAction in trackActions)
            {
                try
                {
                    if (trackAction.WorkflowAction != null && trackAction.TrackMessage != null)
                    {
                        Log.Informational("TrackActionID :" + trackAction.WorkflowAction.WorkflowActionID + " ContactID:" + trackAction.TrackMessage.ContactId);
                    }
                    var hasMatched = _workflowService.HasContactMatchedEndTrigger(trackAction.TrackMessage.ContactId, trackAction.WorkflowID, trackAction.TrackMessageID);
                    if (!hasMatched)
                    {
                        State state = null;
                        switch ((WorkflowActionType)trackAction.WorkflowActionTypeID)
                        {
                        case WorkflowActionType.SendCampaign:
                            var workflowCampaignAction = (trackAction.WorkflowAction as WorkflowCampaignAction);
                            state = new SendCampaignState(workflowCampaignAction.CampaignID, trackAction.WorkflowID, trackAction.Workflow.CreatedBy, trackAction.ActionID,
                                                          workflowCampaignAction.Links != null && workflowCampaignAction.Links.Any() ? workflowCampaignAction.Links.Select(l => l.LinkID) : new List <int>(),
                                                          _campaignService, _workflowService);
                            break;

                        case WorkflowActionType.SendEmail:
                            var action = trackAction.WorkflowAction as WorkflowEmailNotificationAction;
                            state = new SendEmailState(trackAction.WorkflowID, action.WorkflowActionID, action.Body, action.Subject, action.FromEmailID, _workflowService, _communicationService);
                            break;

                        case WorkflowActionType.NotifyUser:
                            //Added 5 seconds wait time in-order to make notify emails pull fresh data. NEXG-2697
                            System.Threading.Thread.Sleep(5000);
                            var workflowNotifyUserAction = (trackAction.WorkflowAction as WorkflowNotifyUserAction);
                            state = new NotifyUserState(workflowNotifyUserAction.UserID, workflowNotifyUserAction.NotificationFieldID, trackAction.WorkflowID, trackAction.Workflow.WorkflowName, trackAction.Workflow.CreatedBy, trackAction.ActionID,
                                                        workflowNotifyUserAction.MessageBody, workflowNotifyUserAction.NotifyType, _workflowService);
                            break;

                        case WorkflowActionType.AddTag:
                            state = new TagAddedState((trackAction.WorkflowAction as WorkflowTagAction).TagID, trackAction.WorkflowID, trackAction.Workflow.CreatedBy, trackAction.ActionID, _tagService, _workflowService);
                            break;

                        case WorkflowActionType.RemoveTag:
                            state = new TagRemovedState((trackAction.WorkflowAction as WorkflowTagAction).TagID, trackAction.WorkflowID, trackAction.Workflow.CreatedBy, trackAction.ActionID, _tagService, _workflowService);
                            break;

                        case WorkflowActionType.AdjustLeadScore:
                            state = new AdjustLeadScoreState((trackAction.WorkflowAction as WorkflowLeadScoreAction).LeadScoreValue, trackAction.WorkflowID, trackAction.Workflow.CreatedBy, trackAction.TrackMessage.AccountId, trackAction.ActionID, _leadScoreService, _workflowService);
                            break;

                        case WorkflowActionType.ChangeLifecycle:
                            state = new LifecycleChangedState((trackAction.WorkflowAction as WorkflowLifeCycleAction).LifecycleDropdownValueID, trackAction.WorkflowID, trackAction.Workflow.CreatedBy, trackAction.ActionID, _contactService, _workflowService);
                            break;

                        case WorkflowActionType.UpdateField:
                            var workflowContactFieldAction = trackAction.WorkflowAction as WorkflowContactFieldAction;
                            state = new UpdateFieldState(workflowContactFieldAction.FieldID, trackAction.WorkflowID, trackAction.ActionID, workflowContactFieldAction.FieldValue, (int)workflowContactFieldAction.FieldInputTypeId, _contactService, _workflowService);
                            break;

                        case WorkflowActionType.AssignToUser:
                            var assignUser = trackAction.WorkflowAction as WorkflowUserAssignmentAction;
                            state = new AssignUserState(assignUser.WorkflowUserAssignmentActionID, assignUser.ScheduledID, trackAction.WorkflowID, trackAction.Workflow.CreatedBy, trackAction.ActionID, _contactService, _workflowService);
                            break;

                        case WorkflowActionType.TriggerWorkflow:
                            state = new TriggerWorkflowState(trackAction.WorkflowID, (trackAction.WorkflowAction as TriggerWorkflowAction).SiblingWorkflowID, trackAction.ActionID, _workflowService, _messageService);
                            break;

                        case WorkflowActionType.WorkflowEndState:
                        case WorkflowActionType.LinkActions:
                            state = new WorkflowEndState(trackAction.WorkflowID, trackAction.ActionID, _workflowService);
                            break;

                        case WorkflowActionType.SetTimer:
                            state = new WaitingPeriodState(trackAction.ActionID, _workflowService);
                            break;

                        case WorkflowActionType.SendText:
                            //Not implemented
                            break;

                        default:
                            //Handle exception here
                            throw new InvalidOperationException($"Handling of WorkflowActionType {(WorkflowActionType)trackAction.WorkflowActionTypeID} not implemented");
                        }
                        state?.OnEntry(ToMessage(trackAction.TrackMessage, trackAction.WorkflowID));
                        trackAction.ActionProcessStatusID = TrackActionProcessStatus.Executed;
                        trackAction.ExecutedOn            = DateTime.UtcNow;
                    }
                    else
                    {
                        trackAction.ActionProcessStatusID = TrackActionProcessStatus.Termintaed;
                        trackAction.ExecutedOn            = DateTime.UtcNow;
                    }
                }
                catch (Exception ex)
                {
                    var message = string.Format("Error while executing {0} workflow - {1} action id - {2} track message, {4} - action type. Error - {3}", trackAction.WorkflowID, trackAction.TrackActionID, trackAction.TrackMessage.TrackMessageID, ex.Message, ((Entities.WorkflowActionType)trackAction.WorkflowActionTypeID));
                    Log.Error(message, ex);
                    trackAction.ActionProcessStatusID = TrackActionProcessStatus.Error;
                    trackAction.ExecutedOn            = DateTime.UtcNow;
                    errorMessages.Add(new TrackActionLog {
                        TrackActionID = trackAction.TrackActionID, ErrorMessage = ex.Message
                    });
                }
                try
                {
                    _accountService.ScheduleAnalyticsRefresh(trackAction.WorkflowID, (byte)IndexType.Workflows);
                }
                catch (Exception wex)
                {
                    Log.Error("Error while inserting to refresh analytics - workflows", wex);
                }
            }
            _workflowService.UpdateActionBatchStatus(new UpdateActionBatchStatusRequest {
                TrackActions = trackActions, TrackActionLogs = errorMessages
            });
            Log.Informational($"Updated status for TrackActionIDs {string.Join(",", trackActions.Select(x => x.TrackActionID))}");
        }