コード例 #1
0
        public HostedJobService(IServiceProvider serviceProvider, HostedJobOptions jobOptions, ILoggerFactory loggerFactory)
        {
            _serviceProvider = serviceProvider;
            _loggerFactory   = loggerFactory;
            _logger          = loggerFactory.CreateLogger <HostedJobService>();
            _jobOptions      = jobOptions;

            var lifetime = serviceProvider.GetService <ShutdownHostIfNoJobsRunningService>();

            lifetime?.RegisterHostedJobInstance(this);
        }
コード例 #2
0
        public static IServiceCollection AddJob(this IServiceCollection services, HostedJobOptions jobOptions)
        {
            if (jobOptions.JobFactory == null)
            {
                throw new ArgumentNullException(nameof(jobOptions), "jobOptions.JobFactory is required");
            }

            if (String.IsNullOrEmpty(jobOptions.CronSchedule))
            {
                return(services.AddTransient <IHostedService>(s => new HostedJobService(s, jobOptions, s.GetService <ILoggerFactory>())));
            }
            else
            {
                if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService)))
                {
                    services.AddTransient <IHostedService, ScheduledJobService>();
                }

                return(services.AddTransient(s => new ScheduledJobRegistration(jobOptions.JobFactory, jobOptions.CronSchedule)));
            }
        }
コード例 #3
0
 public HostedJobOptionsBuilder(HostedJobOptions target = null)
 {
     Target = target ?? new HostedJobOptions();
 }
コード例 #4
0
        public static IServiceCollection AddJob <T>(this IServiceCollection services, HostedJobOptions jobOptions) where T : class, IJob
        {
            services.AddTransient <T>();
            if (String.IsNullOrEmpty(jobOptions.CronSchedule))
            {
                return(services.AddTransient <IHostedService>(s => {
                    if (jobOptions.JobFactory == null)
                    {
                        jobOptions.JobFactory = s.GetRequiredService <T>;
                    }

                    return new HostedJobService(s, jobOptions, s.GetService <ILoggerFactory>());
                }));
            }
            else
            {
                if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService)))
                {
                    services.AddTransient <IHostedService, ScheduledJobService>();
                }

                return(services.AddTransient(s => new ScheduledJobRegistration(jobOptions.JobFactory ?? (s.GetRequiredService <T>), jobOptions.CronSchedule)));
            }
        }
コード例 #5
0
        public static IServiceCollection AddJob(this IServiceCollection services, Func <IServiceProvider, IJob> jobFactory, HostedJobOptions jobOptions)
        {
            if (String.IsNullOrEmpty(jobOptions.CronSchedule))
            {
                return(services.AddTransient <IHostedService>(s => {
                    jobOptions.JobFactory = () => jobFactory(s);

                    return new HostedJobService(s, jobOptions, s.GetService <ILoggerFactory>());
                }));
            }
            else
            {
                if (!services.Any(s => s.ServiceType == typeof(IHostedService) && s.ImplementationType == typeof(ScheduledJobService)))
                {
                    services.AddTransient <IHostedService, ScheduledJobService>();
                }

                return(services.AddTransient(s => new ScheduledJobRegistration(() => jobFactory(s), jobOptions.CronSchedule)));
            }
        }