Exemplo n.º 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);
        }
Exemplo n.º 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)));
            }
        }
Exemplo n.º 3
0
 public HostedJobOptionsBuilder(HostedJobOptions target = null)
 {
     Target = target ?? new HostedJobOptions();
 }
        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 <T>(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)));
            }
        }
Exemplo n.º 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)));
            }
        }