Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new background operation that runs the method in separate task.
 /// </summary>
 /// <param name="name">The name of the background operation.</param>
 /// <param name="method">The method to invoke. The background operation will be passed to the method.</param>
 /// <param name="backgroundOperationManager">The background operation manager that manages this operation.</param>
 /// <param name="cancellationTokenSource">The cancellation token source.</param>
 public TaskQueueBackgroundOperation
 (
     TaskQueueBackgroundOperationManager backgroundOperationManager,
     string name,
     Action <TaskProcessorJob, TDirective> method,
     CancellationTokenSource cancellationTokenSource = default
 ) :
     base
     (
         backgroundOperationManager,
         name,
         (j, d) => { },                 // Ignore the method (we will set it below).
         cancellationTokenSource
     )
 {
     // Save parameters.
     this.UserMethod = method ?? throw new ArgumentNullException(nameof(method));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns <see cref="TaskQueueBackgroundOperationManager"/> instances declared on properties and fields
        /// on this instance.
        /// </summary>
        /// <returns>A collection of background operation managers.</returns>
        protected virtual IEnumerable <TaskQueueBackgroundOperationManager> GetTaskQueueBackgroundOperationManagers()
        {
            var taskQueueBackgroundOperationManagerType = typeof(TaskQueueBackgroundOperationManager);
            TaskQueueBackgroundOperationManager value   = null;

            // Get all properties.
            foreach (var p in this.GetType().GetProperties(System.Reflection.BindingFlags.Instance
                                                           | System.Reflection.BindingFlags.FlattenHierarchy
                                                           | System.Reflection.BindingFlags.Public
                                                           | System.Reflection.BindingFlags.NonPublic)
                     .Where(p => p.CanRead && taskQueueBackgroundOperationManagerType.IsAssignableFrom(p.PropertyType)))
            {
                value = null;
                try
                {
                    value = p.GetValue(this) as TaskQueueBackgroundOperationManager;
                }
                catch { }
                if (null != value)
                {
                    yield return(value);
                }
            }

            // Get all fields.
            foreach (var f in this.GetType().GetFields(System.Reflection.BindingFlags.Instance
                                                       | System.Reflection.BindingFlags.FlattenHierarchy
                                                       | System.Reflection.BindingFlags.Public
                                                       | System.Reflection.BindingFlags.NonPublic)
                     .Where(f => !f.Name.EndsWith("_BackingField") &&          // Ignore backing fields for properties otherwise we report twice.
                            taskQueueBackgroundOperationManagerType.IsAssignableFrom(f.FieldType)))
            {
                value = null;
                try
                {
                    value = f.GetValue(this) as TaskQueueBackgroundOperationManager;
                }
                catch { }
                if (null != value)
                {
                    yield return(value);
                }
            }
        }
        /// <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();
        }