Esempio n. 1
0
        public void TriggerEvent(string name, IContent target, Func <Dictionary <string, object> > tokensContext)
        {
            var tokens = tokensContext();

            var activity = _activitiesManager.GetActivityByName(name);

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

            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.
            var awaitingQuery = _awaitingActivityRepository.Table.Where(x => x.ActivityRecord.Name == name && x.ActivityRecord.Start == false);

            awaitingQuery = target == null || target.ContentItem == null
                    ? awaitingQuery.Where(x => x.WorkflowRecord.ContentItemRecord == null)
                    : awaitingQuery.Where(x => x.WorkflowRecord.ContentItemRecord == target.ContentItem.Record);

            awaitingActivities.AddRange(awaitingQuery.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 == null || workflowContext.Content.ContentItem == null
                            ? null
                            : 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);
            }
        }
Esempio n. 2
0
 public void InsertEvent(WorkflowRecord EventRecord)
 {
     Console.WriteLine(EventRecord.ToString('\t'));
 }
Esempio n. 3
0
 public void UpdateEvent(WorkflowRecord EventRecord)
 {
     Console.WriteLine(EventRecord.ToString('\t').TrimEnd('\n').Split('\n').Last());
 }
Esempio n. 4
0
        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            WorkflowRecord message = new WorkflowRecord();

            message.WFId  = WFIdentifier;
            message.JobId = JobId;

            message.EventId   = (int)record.RecordNumber;
            message.EventTime = record.EventTime.ToLocalTime();
            message.RunId     = record.InstanceId;

            WorkflowInstanceRecord wfRecord = record as WorkflowInstanceRecord;

            if (wfRecord != null)
            {
                message.State = wfRecord.State;
                message.Name  = WorkflowName;

                if (wfRecord.State == WorkflowInstanceStates.Idle)
                {
                    return;                                                //do not track idle status
                }
            }

            ActivityStateRecord actRecord = record as ActivityStateRecord;

            if (actRecord != null)
            {
                message.State = actRecord.State;
                message.Name  = actRecord.Activity.Name;
                if (actRecord.Activity.Name.Equals("DynamicActivity"))
                {
                    return;
                }

                if (actRecord.State == ActivityStates.Executing && actRecord.Activity.TypeName == "System.Activities.Statements.WriteLine")
                {
                    using (StringWriter writer = new StringWriter())
                    {
                        writer.Write(actRecord.Arguments["Text"]);
                        message.Message = writer.ToString();
                        writer.Close();
                    }
                }
            }

            WorkflowInstanceUnhandledExceptionRecord exRecord = record as WorkflowInstanceUnhandledExceptionRecord;

            if (exRecord != null)
            {
                message.State   = exRecord.State;
                message.Name    = WorkflowName;
                message.Message = exRecord.UnhandledException.Message;
            }

            CustomTrackingRecord cuRecord = record as CustomTrackingRecord;

            if (cuRecord != null)
            {
                message.Name    = cuRecord.Activity.Name;
                message.State   = cuRecord.Name;
                message.Message = cuRecord.Data["Message"] as string;
            }

            logWorkflowEvents.InsertEvent(message);
        }