コード例 #1
0
        /// <summary> Create <see cref="IPriorityAccessQueue{TResource}" /> with single static shared resource without service registration </summary>
        /// <param name="services"> Service collection </param>
        /// <param name="resource"> Shared resource instance </param>
        /// <param name="maxPriority"> Max allowed operation priority </param>
        /// <param name="maxAttempts"> Max allowed retry on fail attempts </param>
        /// <typeparam name="TResource"> Shared resource type </typeparam>
        /// <exception cref="ArgumentNullException"> Thrown if <paramref name="resource" /> is NULL </exception>
        public static IPriorityAccessQueue <TResource> CreatePriorityAccessQueue <TResource>(this IServiceCollection services, TResource resource,
                                                                                             int maxPriority = 100, int maxAttempts = int.MaxValue)
            where TResource : notnull
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }
            var manager = new PriorityAccessQueueManager <TResource>(maxPriority, maxAttempts);

            services.AddHostedService(provider
                                      => new TaskWorker <TResource, int>(provider, manager, ProcessorFactory.CreateProcessor <TResource, int>(resource)));
            return(manager);
        }
コード例 #2
0
        /// <summary> Create <see cref="IPriorityAccessQueue{TResource}" /> with static shared resources without service registration </summary>
        /// <param name="services"> Service collection </param>
        /// <param name="resources"> Shared resources collection </param>
        /// <param name="maxPriority"> Max allowed operation priority </param>
        /// <param name="maxAttempts"> Max allowed retry on fail attempts </param>
        /// <typeparam name="TResource"> Shared resource type </typeparam>
        /// <exception cref="ArgumentNullException"> Thrown if <paramref name="resources" /> is NULL </exception>
        /// <exception cref="ArgumentException"> Thrown if <paramref name="resources" /> collection is empty </exception>
        public static IPriorityAccessQueue <TResource> CreatePriorityAccessQueue <TResource>(this IServiceCollection services,
                                                                                             IEnumerable <TResource> resources, int maxPriority = 100, int maxAttempts = int.MaxValue)
            where TResource : notnull
        {
            var arguments = (resources ?? throw new ArgumentNullException(nameof(resources))).Where(resource => resource != null).ToList();

            if (arguments.Count == 0)
            {
                throw new ArgumentException("Empty collection", nameof(resources));
            }
            var manager = new PriorityAccessQueueManager <TResource>(maxPriority, maxAttempts);

            services.AddHostedService(provider
                                      => new TaskWorker <TResource, int>(provider, manager, ProcessorFactory.CreateProcessor <TResource, int>(arguments)));
            return(manager);
        }
コード例 #3
0
        /// <summary>
        ///     Create <see cref="IPriorityAccessQueue{TResource}" /> with given shared resources reuse <paramref name="strategy" /> without service
        ///     registration
        /// </summary>
        /// <param name="services"> Service collection </param>
        /// <param name="resourceFactory"> Shared resource factory function </param>
        /// <param name="strategy"> Conveyor machines reuse strategy <seealso cref="ReuseStrategy" /></param>
        /// <param name="maxResourceInstances"> Max allowed shared resource instances count </param>
        /// <param name="maxPriority"> Max allowed operation priority </param>
        /// <param name="maxAttempts"> Max allowed retry on fail attempts </param>
        /// <typeparam name="TResource"> Shared resource type </typeparam>
        /// <exception cref="ArgumentNullException"> Thrown if <paramref name="resourceFactory" /> is NULL </exception>
        /// <exception cref="InvalidEnumArgumentException"> Thrown if <paramref name="strategy" /> has incorrect value </exception>
        public static IPriorityAccessQueue <TResource> CreatePriorityAccessQueue <TResource>(this IServiceCollection services,
                                                                                             Func <IServiceProvider, TResource> resourceFactory, ReuseStrategy strategy, int maxResourceInstances = 1, int maxPriority = 100,
                                                                                             int maxAttempts = int.MaxValue)
            where TResource : notnull
        {
            if (resourceFactory == null)
            {
                throw new ArgumentNullException(nameof(resourceFactory));
            }
            if (strategy != ReuseStrategy.Static && strategy != ReuseStrategy.Reuse && strategy != ReuseStrategy.OneTime)
            {
                throw new InvalidEnumArgumentException(nameof(strategy), (int)strategy, typeof(ReuseStrategy));
            }
            var manager = new PriorityAccessQueueManager <TResource>(maxPriority, maxAttempts);

            services.AddHostedService(provider => new TaskWorker <TResource, int>(provider,
                                                                                  manager,
                                                                                  ProcessorFactory.CreateProcessor <TResource, int>(resourceFactory, strategy, provider, maxResourceInstances)));
            return(manager);
        }