예제 #1
0
        /// <summary>
        /// Creates an instance of <see cref="AppTaskBatchProcessor"/> for
        /// broadcast task processing, using common configuration settings.
        /// </summary>
        /// <param name="vaultApplication">The vault application that 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="cancellationToken">The cancellation token used for cancelling ongoing operations.</param>
        /// <param name="maxConcurrentBatches">The maximum concurrent batches (defaults to 5).</param>
        /// <param name="maxConcurrentJobs">The maximum number of concurrent jobs per batch (defaults to 5).</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>
        /// <param name="vaultExtensionProxyMethodId">The Id of the vault extension method proxy to use for re-broadcasts.</param>
        /// <returns>The broadcast batch processor.</returns>
        public static AppTaskBatchProcessor CreateBroadcastTaskProcessor
        (
            this VaultApplicationBase vaultApplication,
            string queueId,
            Dictionary <string, TaskProcessorJobHandler> taskHandlers,
            int maxConcurrentBatches         = 5,
            int maxConcurrentJobs            = 5,
            int maxPollingInterval           = 10,
            bool automaticallyRegisterQueues = true,
            bool automaticallyStartPolling   = true,
            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("The processor settings must have at least one task handler defined.", 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 (maxConcurrentBatches <= 0)
            {
                SysUtils.ReportToEventLog
                (
                    "The maximum concurrent batches must be a positive integer; using default.",
                    System.Diagnostics.EventLogEntryType.Warning
                );
                maxConcurrentBatches = 5;
            }
            if (maxConcurrentJobs <= 0)
            {
                SysUtils.ReportToEventLog
                (
                    "The maximum concurrent jobs must be a positive integer; using default.",
                    System.Diagnostics.EventLogEntryType.Warning
                );
                maxConcurrentJobs = 5;
            }
            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 AppTaskBatchProcessorSettings
            {
                QueueDef = new TaskQueueDef
                {
                    TaskType           = TaskQueueManager.TaskType.BroadcastMessages,
                    Id                 = queueId,
                    ProcessingBehavior = MFTaskQueueProcessingBehavior.MFProcessingBehaviorConcurrent,
                    MaximumPollingIntervalInSeconds = maxPollingInterval,
                    LastBroadcastId = ""
                },
                PermanentVault                  = vaultApplication.PermanentVault,
                MaxConcurrentBatches            = maxConcurrentBatches,
                MaxConcurrentJobs               = maxConcurrentJobs,
                TaskHandlers                    = taskHandlers,
                TaskQueueManager                = vaultApplication.TaskQueueManager,
                EnableAutomaticTaskUpdates      = true,
                DisableAutomaticProgressUpdates = false,
                PollTasksOnJobCompletion        = true,
                VaultExtensionMethodProxyId     = vaultApplication.GetVaultExtensionMethodEventHandlerProxyName()
            };

            // Create the processor.
            var processor = vaultApplication.CreateBroadcastTaskProcessor
                            (
                processorSettings,
                automaticallyRegisterQueues,
                automaticallyStartPolling,
                cancellationTokenSource
                            );

            // Return the processor.
            return(processor);
        }
예제 #2
0
        /// <summary>
        /// Enables rebroadcasting of the configuration data to all servers.
        /// </summary>
        /// <typeparam name="TSecureConfiguration">The configuration type.</typeparam>
        /// <param name="vaultApplication">The vault application to enable rebroadcasting for.</param>
        /// <param name="broadcastTaskProcessor">The processor used to process the configuration rebroadcasting queue.</param>
        /// <param name="broadcastTaskQueueId">The queue Id used for the configuration rebroadcasting.</param>
        /// <param name="taskHandlers">Handlers for any additional tasks that the queue should handle.</param>
        /// <param name="maxConcurrentBatches">The maximum number of concurrent batches.</param>
        /// <param name="maxConcurrentJobs">The maximum number of concurrent jobs.</param>
        /// <param name="maxPollingInterval">The maximum polling interval.</param>
        /// <param name="cancellationTokenSource">The token source for cancellation.</param>
        public static void EnableConfigurationRebroadcasting <TSecureConfiguration>
        (
            this ConfigurableVaultApplicationBase <TSecureConfiguration> vaultApplication,
            out AppTaskBatchProcessor broadcastTaskProcessor,
            out string broadcastTaskQueueId,
            Dictionary <string, TaskProcessorJobHandler> taskHandlers = null,
            int maxConcurrentBatches = 5,
            int maxConcurrentJobs    = 5,
            int maxPollingInterval   = 10,
            CancellationTokenSource cancellationTokenSource = default
        )
            where TSecureConfiguration : class, new()
        {
            // Sanity.
            if (null == vaultApplication)
            {
                throw new ArgumentNullException(nameof(vaultApplication));
            }

            // We must have at least one task handler or the underlying implementation throws.
            taskHandlers = taskHandlers ?? new Dictionary <string, TaskProcessorJobHandler>();
            if (taskHandlers.Count == 0)
            {
                taskHandlers.Add(Guid.NewGuid().ToString(), (j) => { });
            }

            // Set up the broadcast task queue ID.  This is specific for this application.
            broadcastTaskQueueId = $"{vaultApplication.GetType().FullName.Replace(".", "-")}-ConfigurationRebroadcastQueue";

            // Create the settings instance.
            var processorSettings = new AppTaskBatchProcessorSettings
            {
                QueueDef = new TaskQueueDef
                {
                    TaskType           = TaskQueueManager.TaskType.BroadcastMessages,
                    Id                 = broadcastTaskQueueId,
                    ProcessingBehavior = MFTaskQueueProcessingBehavior.MFProcessingBehaviorConcurrent,
                    MaximumPollingIntervalInSeconds = maxPollingInterval,
                    LastBroadcastId = ""
                },
                PermanentVault                  = vaultApplication.PermanentVault,
                MaxConcurrentBatches            = maxConcurrentBatches,
                MaxConcurrentJobs               = maxConcurrentJobs,
                TaskHandlers                    = taskHandlers,
                TaskQueueManager                = vaultApplication.TaskQueueManager,
                EnableAutomaticTaskUpdates      = true,
                DisableAutomaticProgressUpdates = false,
                PollTasksOnJobCompletion        = true,
                VaultExtensionMethodProxyId     = vaultApplication.GetVaultExtensionMethodEventHandlerProxyName()
            };

            // Set up the cancellation token source.
            cancellationTokenSource = cancellationTokenSource == null
                                ? new CancellationTokenSource()
                                : CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token);

            // Create the broadcast processor using the vault extension method.
            broadcastTaskProcessor = vaultApplication.CreateBroadcastTaskProcessor
                                     (
                processorSettings,
                cancellationTokenSource: cancellationTokenSource
                                     );
        }
예제 #3
0
        /// <summary>
        /// Creates an instance of <see cref="AppTaskBatchProcessor"/> for
        /// broadcast task processing, using common configuration settings.
        /// </summary>
        /// <param name="vaultApplication">The vault application that this task processor is associated with.</param>
        /// <param name="processorSettings">The settings for the task processor.</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 broadcast batch processor.</returns>
        public static AppTaskBatchProcessor CreateBroadcastTaskProcessor
        (
            this VaultApplicationBase vaultApplication,
            AppTaskBatchProcessorSettings 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.BroadcastMessages)
            {
                throw new ArgumentException("The processor settings queue definition task type must be BroadcastMessages.", 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.MaxConcurrentBatches <= 0)
            {
                SysUtils.ReportToEventLog
                (
                    "The maximum concurrent batches must be a positive integer; using default.",
                    System.Diagnostics.EventLogEntryType.Warning
                );
                processorSettings.MaxConcurrentBatches = 5;
            }
            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 AppTaskBatchProcessor
                            (
                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);
        }