workflowManager class optimizing

Topics: Core
Jun 20, 2013 at 11:23 PM
public void TriggerEvent(string name, IContent target, Func<Dictionary<string, object>> tokensContext) {
        var activity = _activitiesManager.GetActivityByName(name);

        if (activity == null) {
            Logger.Error("Activity {0} was not found", name);
            return;
        }
        var tokens = tokensContext();

        var startedWorkflows = new List<ActivityRecord>();

        // look for workflow definitions with a corresponding starting activity
        // it's important to return activities at this point and not workflows,
        // as a workflow definition could have multiple entry points with the same type of activity
        startedWorkflows.AddRange(_activityRepository.Table.Where(
            x =>x.Name == name && x.Start && x.WorkflowDefinitionRecord.Enabled
            )
        );

        var awaitingActivities = new List<AwaitingActivityRecord>();

        // and any running workflow paused on this kind of activity for this content
        // it's important to return activities at this point as a workflow could be awaiting 
        // on several ones. When an activity is restarted, all the other ones of the same workflow are cancelled.
        awaitingActivities.AddRange(_awaitingActivityRepository.Table.Where(
            x => x.ActivityRecord.Name == name && x.ActivityRecord.Start == false && x.WorkflowRecord.ContentItemRecord == target.ContentItem.Record
            ).ToList()
        );

        //// if no activity record is matching the event, do nothing
        //if (!startedWorkflows.Any() && !awaitingActivities.Any()) {
        //    return;
        //}

        // if no activity record is matching the event, do nothing
        if (!startedWorkflows.Any() && !awaitingActivities.Any()) {
            return;
        }

        // resume halted workflows
        foreach (var awaitingActivityRecord in awaitingActivities) {
            var workflowContext = new WorkflowContext {
                Content = target,
                Tokens = tokens,
                Record = awaitingActivityRecord.WorkflowRecord
            };

            workflowContext.Tokens["Workflow"] = workflowContext;

            var activityContext = CreateActivityContext(awaitingActivityRecord.ActivityRecord, tokens);

            // check the condition
            try {
                if (!activity.CanExecute(workflowContext, activityContext)) {
                    continue;
                }
            }
            catch (Exception e) {
                Logger.Error("Error while evaluating an activity condition on {0}: {1}", name, e.ToString());
                continue;
            }

            ResumeWorkflow(awaitingActivityRecord, workflowContext, tokens);
        }

        // start new workflows
        foreach (var activityRecord in startedWorkflows) {

            var workflowContext = new WorkflowContext {
                Content = target,
                Tokens = tokens,
            };

            workflowContext.Tokens["Workflow"] = workflowContext;

            var workflowRecord = new WorkflowRecord {
                WorkflowDefinitionRecord = activityRecord.WorkflowDefinitionRecord,
                State = "{}",
                ContentItemRecord = workflowContext.Content.ContentItem.Record
            };

            workflowContext.Record = workflowRecord;

            var activityContext = CreateActivityContext(activityRecord, tokens);

            // check the condition
            try {
                if(!activity.CanExecute(workflowContext, activityContext)) {
                    continue;
                }
            }
            catch (Exception e) {
                Logger.Error("Error while evaluating an activity condition on {0}: {1}", name, e.ToString());
                continue;
            }

            StartWorkflow(workflowContext, activityRecord, tokens);
        }
    }
===============

var tokens = tokensContext();

//// if no activity record is matching the event, do nothing
        //if (!startedWorkflows.Any() && !awaitingActivities.Any()) {
        //    return;
        //}
Coordinator
Jun 21, 2013 at 12:19 AM
Can you summarize or do I need to read and understand the whole code ;) Not that I didn't write it but it might take some time to recall the details ...

Any general feedback on the module/implementation ?
Jun 21, 2013 at 12:21 AM
1--- it just move down var tokens = tokensContext();
2---comment out one of this
//// if no activity record is matching the event, do nothing
    //if (!startedWorkflows.Any() && !awaitingActivities.Any()) {
    //    return;
    //}