Esempio n. 1
0
        public ProcessServer(IServiceProvider serviceProvider,
                             JobBuilderOptions options,
                             IJobStorage jobStorage,
                             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);

            JobDictionary = new ConcurrentDictionary <string, ProcessableCronJob>(_options.JobList.ToDictionary(p => p.Id, p => new ProcessableCronJob(p)));

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

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

            _bootstrapTask = WrapProcess(this).CreateTask(_loggerFactory, context);
        }
Esempio n. 2
0
        public static IServiceCollection AddNetCoreStackJobServer(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 });
            }

            services.AddSingleton(opts);

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

            services.AddSingleton(assemblyOptions);

            services.TryAdd(ServiceDescriptor.Singleton <IJobStorage, DefaultJobStorage>());

            return(services);
        }