コード例 #1
0
        protected virtual SchedulePostProcessingAction ProcessLocalServiceSource(ScheduledItem scheduledItem, Activity activity,
                                                                                 string transactionId)
        {
            DataService dataService =
                ServiceManager.ValidateDataService(scheduledItem.SourceId, ServiceType.QueryOrSolicitOrExecuteOrTask);

            // Load the service plugin
            ISolicitProcessor            solicitPlugin = null;
            IQueryProcessor              queryPlugin   = null;
            IExecuteProcessor            executePlugin = null;
            ITaskProcessor               taskPlugin    = null;
            IPluginDisposer              disposer;
            string                       flowName = FlowManager.GetDataFlowNameById(dataService.FlowId);
            RequestType                  requestType;
            SchedulePostProcessingAction postProcessingAction = SchedulePostProcessingAction.ContinueNormally;

            try
            {
                if ((dataService.Type & ServiceType.Task) == ServiceType.Task)
                {
                    disposer    = PluginLoader.LoadTaskProcessor(dataService, out taskPlugin);
                    requestType = RequestType.Task;
                }
                else if ((dataService.Type & ServiceType.Execute) == ServiceType.Execute)
                {
                    disposer    = PluginLoader.LoadExecuteProcessor(dataService, out executePlugin);
                    requestType = RequestType.Execute;
                }
                else if ((dataService.Type & ServiceType.Solicit) == ServiceType.Solicit)
                {
                    disposer    = PluginLoader.LoadSolicitProcessor(dataService, out solicitPlugin);
                    requestType = RequestType.Solicit;
                }
                else
                {
                    disposer    = PluginLoader.LoadQueryProcessor(dataService, out queryPlugin);
                    requestType = RequestType.Query;
                }
            }
            catch (Exception e)
            {
                throw new NotImplementedException(string.Format("Failed to load the service \"{0}\" for the scheduled item \"{1}\"",
                                                                dataService.Name, scheduledItem.Name), e);
            }
            using (disposer)
            {
                string requestId =
                    RequestManager.CreateDataRequest(transactionId, dataService.Id, 0, -1,
                                                     requestType, activity.ModifiedById,
                                                     scheduledItem.SourceArgs);

                if (taskPlugin != null)
                {
                    LogActivity(activity, "Calling ProcessTask()");
                    try
                    {
                        ITaskProcessorEx taskPluginEx = taskPlugin as ITaskProcessorEx;
                        if (taskPluginEx != null)
                        {
                            postProcessingAction = taskPluginEx.ProcessTask(requestId, scheduledItem.Id);
                        }
                        else
                        {
                            taskPlugin.ProcessTask(requestId);
                        }
                    }
                    finally
                    {
                        activity.Append(taskPlugin.GetAuditLogEvents());
                    }
                    LogActivity(activity, "Called ProcessTask()");
                }
                else if (executePlugin != null)
                {
                    LogActivity(activity, "Calling ProcessExecute()");
                    ExecuteContentResult result;
                    try
                    {
                        result = executePlugin.ProcessExecute(requestId);
                    }
                    finally
                    {
                        activity.Append(executePlugin.GetAuditLogEvents());
                    }
                    if (result.Content != null)
                    {
                        _documentManager.AddDocument(transactionId, result);
                    }
                    LogActivity(activity, "Called ProcessExecute()");
                }
                else if (solicitPlugin != null)
                {
                    LogActivity(activity, "Calling ProcessSolicit()");
                    try
                    {
                        solicitPlugin.ProcessSolicit(requestId);
                    }
                    finally
                    {
                        activity.Append(solicitPlugin.GetAuditLogEvents());
                    }
                    LogActivity(activity, "Called ProcessSolicit()");
                }
                else
                {
                    LogActivity(activity, "Calling ProcessQuery()");
                    PaginatedContentResult result;
                    try
                    {
                        result = queryPlugin.ProcessQuery(requestId);
                    }
                    finally
                    {
                        activity.Append(queryPlugin.GetAuditLogEvents());
                    }
                    LogActivity(activity, "Called ProcessQuery()");
                }
            }
            return(postProcessingAction);
        }
コード例 #2
0
        public ScheduledItem ProcessScheduledItem(string scheduleId, bool forceRun, out Activity activity)
        {
            activity = null;
            using (INodeProcessorMutex mutex = GetMutex(scheduleId))
            {
                if (!mutex.IsAcquired)
                {
                    LOG.Debug("Exiting ProcessScheduleItem(), could not acquire mutex");
                    return(null);        // Another thread is already working on this transaction, get out of here
                }
                bool          isRunNow;
                ScheduledItem scheduledItem = ScheduleManager.GetScheduledItem(scheduleId, out isRunNow);
                /*************************************/
                var isDebugging        = DebugUtils.IsDebugging;
                var computerPrefix     = "COMPUTER: ";
                var allowScheduleToRun = !isDebugging;
                var hasComputerPrefix  = scheduledItem.Name.StartsWith(computerPrefix, StringComparison.OrdinalIgnoreCase);
                if (hasComputerPrefix)
                {
                    var specialPrefix    = computerPrefix + Environment.MachineName;
                    var hasSpecialPrefix = scheduledItem.Name.StartsWith(specialPrefix, StringComparison.OrdinalIgnoreCase);
                    allowScheduleToRun = hasSpecialPrefix;
                }
                if (!allowScheduleToRun)
                {
                    return(null);
                }
                /*************************************/
                DateTime startTime = DateTime.Now;
                // Make sure the transaction has not been processed yet
                if (!forceRun && ((scheduledItem.NextRunOn > startTime) && !isRunNow))
                {
                    LOG.Debug("Exiting ProcessScheduledItem(), schedule {0} has already run",
                              scheduledItem);
                    return(null);
                }

                string flowName = _flowManager.GetDataFlowNameById(scheduledItem.FlowId);
                activity = new Activity(NodeMethod.Schedule, flowName, scheduledItem.Name, ActivityType.Info,
                                        null, NetworkUtils.GetLocalIp(), "Start processing schedule: \"{0}\"",
                                        scheduledItem.Name);
                string transactionId = null;
                try
                {
                    // Make sure the user that created the schedule is still active
                    UserAccount userAccount = _accountManager.GetById(scheduledItem.ModifiedById);
                    if ((userAccount == null) || !userAccount.IsActive)
                    {
                        activity.AppendFormat("The user account that created the scheduled item \"{0}\" is no longer active.  Scheduled item cannot execute.",
                                              scheduledItem.Name);
                        return(null);
                    }
                    activity.ModifiedById = userAccount.Id;

                    scheduledItem.ExecuteStatus = ScheduleExecuteStatus.Running;
                    ScheduleManager.UpdateScheduleStatus(scheduledItem.Id, scheduledItem.ExecuteStatus);

                    transactionId =
                        _transactionManager.CreateTransaction(NodeMethod.Schedule, EndpointVersionType.Undefined,
                                                              scheduledItem.FlowId, scheduledItem.Name, userAccount.Id,
                                                              CommonTransactionStatusCode.Processing,
                                                              null, null, null, false);
                    activity.TransactionId = transactionId;

                    SchedulePostProcessingAction postProcessingAction = SchedulePostProcessingAction.ContinueNormally;

                    switch (scheduledItem.SourceType)
                    {
                    case ScheduledItemSourceType.LocalService:
                        postProcessingAction = ProcessLocalServiceSource(scheduledItem, activity, transactionId);
                        break;

                    case ScheduledItemSourceType.File:
                        ProcessFileSource(scheduledItem, activity, transactionId);
                        break;

                    case ScheduledItemSourceType.WebServiceQuery:
                        ProcessWebServiceQuerySource(scheduledItem, activity, transactionId);
                        break;

                    case ScheduledItemSourceType.WebServiceSolicit:
                        ProcessWebServiceSolicitSource(scheduledItem, activity, transactionId);
                        break;

                    default:
                        throw new ArgumentException(string.Format("Unrecognized scheduledItem.SourceType: {0}",
                                                                  scheduledItem.SourceType));
                    }

                    TransactionStatus transactionStatus = null;
                    if (postProcessingAction != SchedulePostProcessingAction.None)
                    {
                        if (scheduledItem.TargetType != ScheduledItemTargetType.None)
                        {
                            transactionStatus =
                                _transactionManager.SetTransactionStatus(transactionId, CommonTransactionStatusCode.Processed,
                                                                         null, false);
                            switch (scheduledItem.TargetType)
                            {
                            case ScheduledItemTargetType.LocalService:
                                ProcessLocalServiceTarget(transactionStatus, scheduledItem, activity);
                                break;

                            case ScheduledItemTargetType.Email:
                                ProcessEmailTarget(transactionStatus, scheduledItem, activity);
                                break;

                            case ScheduledItemTargetType.File:
                                ProcessFileTarget(transactionStatus, scheduledItem, activity);
                                break;

                            case ScheduledItemTargetType.Partner:
                                ProcessPartnerTarget(transactionStatus, scheduledItem, activity);
                                break;

                            case ScheduledItemTargetType.Schematron:
                                ProcessSchematronTarget(transactionStatus, scheduledItem, activity);
                                break;

                            default:
                                throw new ArgumentException(string.Format("Unrecognized scheduledItem.TargetType: {0}",
                                                                          scheduledItem.TargetType));
                            }
                        }
                    }

                    transactionStatus =
                        _transactionManager.SetTransactionStatus(transactionId, CommonTransactionStatusCode.Completed,
                                                                 null, true);

                    activity.AppendFormat("End processing schedule: \"{0}\", processing duration: {1}",
                                          scheduledItem.Name, TimeSpan.FromTicks(DateTime.Now.Ticks - startTime.Ticks).ToString());
                    activity.AppendFormat("Transaction \"{0}\" status set to \"{1}\"", transactionStatus.Id.ToString(),
                                          transactionStatus.Status.ToString());
                    _notificationManager.DoScheduleNotifications(transactionStatus, scheduledItem.FlowId,
                                                                 scheduledItem.Name);
                    scheduledItem.ExecuteStatus = ScheduleExecuteStatus.CompletedSuccess;
                }
                catch (Exception e)
                {
                    activity.Type = ActivityType.Error;
                    TransactionStatus transactionStatus = null;
                    if (transactionId != null)
                    {
                        transactionStatus =
                            _transactionManager.SetTransactionStatus(transactionId, CommonTransactionStatusCode.Failed,
                                                                     e.Message, true);
                        activity.AppendFormat("Transaction \"{0}\" status set to \"{1}\"", transactionStatus.Id.ToString(),
                                              transactionStatus.Status.ToString());
                    }
                    LogActivityError(activity, ExceptionUtils.ToShortString(e));
                    LOG.Error("ProcessTransactionRequest() threw an exception.", e);
                    if (transactionId != null)
                    {
                        _notificationManager.DoScheduleNotifications(transactionStatus, scheduledItem.FlowId,
                                                                     scheduledItem.Name,
                                                                     "Error: " + ExceptionUtils.GetDeepExceptionMessageOnly(e));
                    }
                    scheduledItem.ExecuteStatus = ScheduleExecuteStatus.CompletedFailure;
                }
                finally
                {
                    ActivityManager.Log(activity);
                    UpdateScheduleRunInfo(scheduledItem, activity);
                }
                return(scheduledItem);
            }
        }