Exemplo n.º 1
0
        public static IServiceCollection AddJobs(this IServiceCollection services, Action <JobBuilderOptions> setup)
        {
            services.AddMemoryCache();
            services.AddOptions();

            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setup == null)
            {
                throw new ArgumentNullException(nameof(setup));
            }

            services.AddSingleton <BackgroundJobServerMarkerService>();

            var opts = new JobBuilderOptions();

            setup?.Invoke(opts);
            foreach (var item in opts.JobList)
            {
                var type            = item.Type;
                var genericRegistry = registryDelegate.MakeGenericMethod(type);
                genericRegistry.Invoke(null, new object[] { services, type });

                if (item.ThrottlerType != null)
                {
                    services.AddTransient(item.ThrottlerType);
                }
            }

            services.AddSingleton(opts);

            var assemblies      = opts.JobList.Select(j => j.GetType().Assembly).Distinct().ToList();
            var assemblyOptions = new AssemblyOptions(assemblies);

            services.AddSingleton(assemblyOptions);

            return(services);
        }
Exemplo n.º 2
0
        public ProcessServer(IServiceProvider serviceProvider,
                             JobBuilderOptions options,
                             ILoggerFactory loggerFactory)
        {
            _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(_serviceProvider));
            _options         = options ?? throw new ArgumentNullException(nameof(options));
            _loggerFactory   = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));

            var properties = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            var cronJobDictionary = new ConcurrentDictionary <string, ProcessableCronJob>(
                _options.JobList.Where(j => j.Cron != null && j.ThrottlerType == null)
                .ToDictionary(p => p.Id, p => new ProcessableCronJob(p)));

            var customThrottlerJobDictionary = new ConcurrentDictionary <string, ProcessableCronJob>(
                _options.JobList.Where(j => j.ThrottlerType != null && j.Cron == null)
                .ToDictionary(p => p.Id, p => new ProcessableCronJob(p)));

            _processes = new List <IBackgroundTask>
            {
                new CronJobScheduler(_serviceProvider, new EveryMinuteThrottler(), ScheduleInstant.Factory, cronJobDictionary, _loggerFactory)
            };

            foreach (KeyValuePair <string, ProcessableCronJob> entry in customThrottlerJobDictionary)
            {
                var throttler = (IThrottler)serviceProvider.GetService(entry.Value.ThrottlerType);
                var invoker   = (IJob)serviceProvider.GetService(entry.Value.Type);
                _processes.Add(new CustomJobScheduler(serviceProvider, throttler, invoker.InvokeAsync));
            }

            var context = new TaskContext(
                GetGloballyUniqueServerId(),
                properties,
                _cts.Token);

            _bootstrapTask = WrapProcess(this).CreateTask(_loggerFactory, context);
        }