/// <summary>
        /// Registers the task queue used by this application or module.
        /// </summary>
        public void RegisterTaskQueues()
        {
            // Create the cancellation token source.
            if (this.TokenSource == null)
            {
                this.TokenSource = new CancellationTokenSource();
            }

            // Create the task processor.
            if (this.TaskProcessor == null)
            {
                // Initialize the task processor.
                this.TaskProcessor = new AppTaskBatchProcessor
                                     (
                    new AppTaskBatchProcessorSettings
                {
                    QueueDef = new TaskQueueDef
                    {
                        TaskType           = TaskQueueManager.TaskType.Both,
                        Id                 = VaultApplication.BackgroundOperationTaskQueueId,
                        ProcessingBehavior = MFTaskQueueProcessingBehavior.MFProcessingBehaviorConcurrent,
                        MaximumPollingIntervalInSeconds = this.Configuration.MaxPollingIntervalInSeconds,
                        LastBroadcastId = ""
                    },
                    PermanentVault       = this.PermanentVault,
                    MaxConcurrentBatches = this.Configuration.MaxConcurrentBatches,
                    MaxConcurrentJobs    = this.Configuration.MaxConcurrentJobs,
                    TaskHandlers         = new Dictionary <string, TaskProcessorJobHandler>
                    {
                        { VaultApplication.TaskTypeHourlyRecurringTask, ProcessHourlyTask }
                    },
                    TaskQueueManager                = this.TaskQueueManager,
                    EnableAutomaticTaskUpdates      = true,
                    DisableAutomaticProgressUpdates = false,
                    PollTasksOnJobCompletion        = true
                },
                    this.TokenSource.Token
                                     );

                // Schedule the hourly task.
                ScheduleHourlyTask();
            }

            // Register the task queue.
            this.TaskProcessor.RegisterTaskQueues();

            // Ensure that the configuration broadcast queue is initialized.
            this.InitializeConfigurationBroadcastProcessor();
        }
        /// <summary>
        /// Initializes the configuration broadcast task processor.
        /// - This processor also handles the rebroadcast messages from the SaveCommand from the MFAdmin.
        /// </summary>
        private void InitializeConfigurationBroadcastProcessor()
        {
            // Verify the broadcast task processor token source has been created.
            if (this.ConfigurationBroadcastTokenSource == null)
            {
                this.ConfigurationBroadcastTokenSource = new CancellationTokenSource();
            }

            // Initialize the batch task processor.
            if (this.ConfigurationBroadcastProcessor == null)
            {
                this.ConfigurationBroadcastProcessor = new AppTaskBatchProcessor(
                    new AppTaskBatchProcessorSettings
                {
                    QueueDef = new TaskQueueDef
                    {
                        TaskType           = TaskQueueManager.TaskType.BroadcastMessages,
                        Id                 = ConfigurationBroadcastTaskQueueId,
                        ProcessingBehavior = MFTaskQueueProcessingBehavior.MFProcessingBehaviorConcurrent,
                        MaximumPollingIntervalInSeconds = this.Configuration.MaxPollingIntervalInSeconds,
                        LastBroadcastId = ""
                    },
                    PermanentVault       = this.PermanentVault,
                    MaxConcurrentBatches = 5,
                    MaxConcurrentJobs    = 5,
                    // This does not require any task handlers, but if other broadcast tasks are used then they could be added here.
                    TaskHandlers = new Dictionary <string, TaskProcessorJobHandler>()
                    {
                        // Note that we have to provide at least one task handler or the underlying call excepts.
                        { Guid.NewGuid().ToString(), (j) => { } }
                    },
                    TaskQueueManager                = this.TaskQueueManager,
                    EnableAutomaticTaskUpdates      = true,
                    DisableAutomaticProgressUpdates = false,
                    PollTasksOnJobCompletion        = true,
                    VaultExtensionMethodProxyId     = this.GetVaultExtensionMethodEventHandlerProxyName()
                },
                    this.ConfigurationBroadcastTokenSource.Token);
            }

            // Register the task queues.
            this.ConfigurationBroadcastProcessor.RegisterTaskQueues();
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public override string GetRebroadcastQueueId()
        {
            // If we do not have a rebroadcast queue for the configuration data
            // then create one.
            if (null == this.ConfigurationRebroadcastTaskProcessor)
            {
                // Enable the configuration rebroadcasting.
                this.EnableConfigurationRebroadcasting
                (
                    out AppTaskBatchProcessor processor,
                    out string queueId
                );

                // Populate references to the task processor and queue Id.
                this.ConfigurationRebroadcastQueueId       = queueId;
                this.ConfigurationRebroadcastTaskProcessor = processor;
            }

            // Return the broadcast queue Id.
            return(this.ConfigurationRebroadcastQueueId);
        }
        /// <summary>
        /// Creates a background operation manager for a given queue.
        /// </summary>
        /// <param name="vaultApplication">The vault application that contains this background operation manager.</param>
        /// <param name="queueId">The queue Id.</param>
        /// <param name="cancellationTokenSource">The cancellation token source, if cancellation should be supported.</param>
        public TaskQueueBackgroundOperationManager
        (
            VaultApplicationBase vaultApplication,
            string queueId,
            CancellationTokenSource cancellationTokenSource = default
        )
        {
            // Sanity.
            if (string.IsNullOrWhiteSpace(queueId))
            {
                throw new ArgumentException("The queue id cannot be null or whitespace.", nameof(queueId));
            }

            // Assign.
            this.CancellationTokenSource = cancellationTokenSource;
            this.VaultApplication        = vaultApplication ?? throw new ArgumentNullException(nameof(vaultApplication));
            this.QueueId = queueId;

            // Set up the task processor
            this.TaskProcessor = this
                                 .VaultApplication
                                 .CreateConcurrentTaskProcessor
                                 (
                this.QueueId,
                new Dictionary <string, TaskProcessorJobHandler>
            {
                { TaskQueueBackgroundOperation.TaskTypeId, this.ProcessJobHandler }
            },
                cancellationTokenSource: cancellationTokenSource == null
                                                ? new CancellationTokenSource()
                                                : CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token)
                                 );

            // Ensure we have a current server.
            TaskQueueBackgroundOperationManager.SetCurrentServer(vaultApplication);

            // Register the task queues.
            this.TaskProcessor.RegisterTaskQueues();
        }
Ejemplo n.º 5
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
                                     );
        }