Esempio n. 1
0
        /// <summary>
        /// Creates an instance of <see cref="SequentialTaskProcessor"/> for
        /// sequential task processing, using common configuration settings.
        /// </summary>
        /// <param name="vaultApplication">The vault application this task processor is associated with.</param>
        /// <param name="queueId">The queue Id (must be unique in the vault) that this application is processing.</param>
        /// <param name="taskHandlers">The task Ids and handlers that this processor can handle.</param>
        /// <param name="cancellationTokenSource">The cancellation token source.</param>
        /// <param name="maxPollingInterval">The maximum interval (in seconds) between polling.</param>
        /// <param name="automaticallyRegisterQueues">If true, automatically calls <see cref="AppTaskBatchProcessor.RegisterTaskQueues"/>.</param>
        /// <param name="automaticallyStartPolling">If true, automatically calls <see cref="TaskQueueManager.EnableTaskPolling"/>.</param>
        /// <returns>The sequential batch processor.</returns>
        internal static SequentialTaskProcessor CreateSequentialTaskProcessor
        (
            this VaultApplicationBase vaultApplication,
            AppTaskProcessorSettings processorSettings,
            bool automaticallyRegisterQueues = true,
            bool automaticallyStartPolling   = true,
            CancellationTokenSource cancellationTokenSource = default
        )
        {
            // Sanity.
            if (null == vaultApplication)
            {
                throw new ArgumentNullException(nameof(vaultApplication));
            }
            if (null == processorSettings)
            {
                throw new ArgumentNullException(nameof(processorSettings));
            }
            if (processorSettings.QueueDef.TaskType != TaskQueueManager.TaskType.ApplicationTasks)
            {
                throw new ArgumentException("The processor settings queue definition task type must be ApplicationTasks.", nameof(processorSettings));
            }
            if (processorSettings.QueueDef.ProcessingBehavior != MFTaskQueueProcessingBehavior.MFProcessingBehaviorSequential)
            {
                throw new ArgumentException("The processor settings queue definition processing behaviour must be MFProcessingBehaviorSequential.", nameof(processorSettings));
            }
            if (null == processorSettings.TaskHandlers)
            {
                throw new ArgumentException("The processor settings must have at least one task handler defined.", nameof(processorSettings));
            }
            if (processorSettings.TaskHandlers.Count == 0)
            {
                throw new ArgumentException("The processor settings must have at least one task handler defined.", nameof(processorSettings));
            }
            if (processorSettings.TaskHandlers.Any(kvp => kvp.Value == null))
            {
                throw new ArgumentException("Task handlers cannot be null.", nameof(processorSettings));
            }

            // Ensure the integer values are valid.
            if (processorSettings.MaxConcurrentJobs <= 0)
            {
                SysUtils.ReportToEventLog
                (
                    "The maximum concurrent jobs must be a positive integer; using default.",
                    System.Diagnostics.EventLogEntryType.Warning
                );
                processorSettings.MaxConcurrentJobs = 5;
            }
            if (processorSettings.QueueDef.MaximumPollingIntervalInSeconds <= 0)
            {
                SysUtils.ReportToEventLog
                (
                    "The maximum polling interval must be a positive integer; using default.",
                    System.Diagnostics.EventLogEntryType.Warning
                );
                processorSettings.QueueDef.MaximumPollingIntervalInSeconds = 10;
            }

            // Create the processor.
            var processor = new SequentialTaskProcessor
                            (
                processorSettings,
                cancellationTokenSource?.Token ?? default
                            );

            // Should we automatically register the task queues?
            if (automaticallyRegisterQueues)
            {
                processor.RegisterTaskQueues();
            }

            // Enable polling/processing of the queue.
            if (automaticallyStartPolling)
            {
                vaultApplication.TaskQueueManager.EnableTaskPolling(true);
            }

            // Return the processor.
            return(processor);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an instance of <see cref="SequentialTaskProcessor"/> for
        /// sequential task processing, using common configuration settings.
        /// </summary>
        /// <param name="vaultApplication">The vault application this task processor is associated with.</param>
        /// <param name="queueId">The queue Id (must be unique in the vault) that this application is processing.</param>
        /// <param name="taskHandlers">The task Ids and handlers that this processor can handle.</param>
        /// <param name="cancellationTokenSource">The cancellation token source.</param>
        /// <param name="maxPollingInterval">The maximum interval (in seconds) between polling.</param>
        /// <param name="automaticallyRegisterQueues">If true, automatically calls <see cref="AppTaskBatchProcessor.RegisterTaskQueues"/>.</param>
        /// <param name="automaticallyStartPolling">If true, automatically calls <see cref="TaskQueueManager.EnableTaskPolling"/>.</param>
        /// <returns>The sequential batch processor.</returns>
        public static SequentialTaskProcessor CreateSequentialTaskProcessor
        (
            this VaultApplicationBase vaultApplication,
            string queueId,
            Dictionary <string, TaskProcessorJobHandler> taskHandlers,
            int maxPollingInterval             = 10,
            bool automaticallyRegisterQueues   = true,
            bool automaticallyStartPolling     = true,
            string vaultExtensionProxyMethodId = null,
            CancellationTokenSource cancellationTokenSource = default
        )
        {
            // Sanity.
            if (null == vaultApplication)
            {
                throw new ArgumentNullException(nameof(vaultApplication));
            }
            if (string.IsNullOrWhiteSpace(queueId))
            {
                throw new ArgumentException("A queue Id must be provided.", nameof(queueId));
            }
            if (null == taskHandlers)
            {
                throw new ArgumentNullException(nameof(taskHandlers));
            }
            if (taskHandlers.Count == 0)
            {
                throw new ArgumentException("No task handlers were registered.", nameof(taskHandlers));
            }
            if (taskHandlers.Count == 0)
            {
                throw new ArgumentException("No task handlers were registered.", nameof(taskHandlers));
            }
            if (taskHandlers.Any(kvp => kvp.Value == null))
            {
                throw new ArgumentException("Task handlers cannot be null.", nameof(taskHandlers));
            }

            // Ensure the integer values are valid.
            if (maxPollingInterval <= 0)
            {
                SysUtils.ReportToEventLog
                (
                    "The maximum polling interval must be a positive integer; using default.",
                    System.Diagnostics.EventLogEntryType.Warning
                );
                maxPollingInterval = 10;
            }

            // Create the processor settings.
            var processorSettings = new AppTaskProcessorSettings
            {
                PollTasksOnJobCompletion   = true,
                MaxConcurrentJobs          = 1,        // Always 1 for a sequential task processor.
                PermanentVault             = vaultApplication.PermanentVault,
                EnableAutomaticTaskUpdates = true,
                QueueDef = new TaskQueueDef
                {
                    TaskType           = TaskQueueManager.TaskType.ApplicationTasks,
                    Id                 = queueId,
                    ProcessingBehavior = MFTaskQueueProcessingBehavior.MFProcessingBehaviorSequential,
                    MaximumPollingIntervalInSeconds = maxPollingInterval,
                    LastBroadcastId = ""
                },
                TaskHandlers                = taskHandlers,
                TaskQueueManager            = vaultApplication.TaskQueueManager,
                VaultExtensionMethodProxyId = vaultExtensionProxyMethodId
            };

            // Use the other overload.
            return(vaultApplication.CreateSequentialTaskProcessor
                   (
                       processorSettings,
                       automaticallyRegisterQueues,
                       automaticallyStartPolling,
                       cancellationTokenSource
                   ));
        }