Exemple #1
0
        /// <summary> Create <see cref="IAccessQueue{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="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 IAccessQueue <TResource> CreateAccessQueue <TResource>(this IServiceCollection services, TResource resource,
                                                                             int maxAttempts = int.MaxValue)
            where TResource : notnull
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }
            var manager = new AccessQueueManager <TResource>(maxAttempts);

            services.AddHostedService(provider
                                      => new TaskWorker <TResource, object?>(provider, manager, ProcessorFactory.CreateProcessor <TResource, object?>(resource)));
            return(manager);
        }
Exemple #2
0
        /// <summary> Create <see cref="IAccessQueue{TResource}" /> with static shared resources without service registration </summary>
        /// <param name="services"> Service collection </param>
        /// <param name="resources"> Shared resources collection </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 IAccessQueue <TResource> CreateAccessQueue <TResource>(this IServiceCollection services, IEnumerable <TResource> resources,
                                                                             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 AccessQueueManager <TResource>(maxAttempts);

            services.AddHostedService(provider
                                      => new TaskWorker <TResource, object?>(provider, manager, ProcessorFactory.CreateProcessor <TResource, object?>(arguments)));
            return(manager);
        }
Exemple #3
0
        /// <summary> Create <see cref="IAccessQueue{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="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 IAccessQueue <TResource> CreateAccessQueue <TResource>(this IServiceCollection services,
                                                                             Func <IServiceProvider, TResource> resourceFactory, ReuseStrategy strategy, int maxResourceInstances = 1, 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 AccessQueueManager <TResource>(maxAttempts);

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