/// <summary>
        /// Schedule job with trigger to underlying service collection. This API maybe change!
        /// </summary>
        public static IServiceCollectionQuartzConfigurator ScheduleJob <T>(
            this IServiceCollectionQuartzConfigurator configurator,
            Action <IServiceCollectionTriggerConfigurator> trigger,
            Action <IServiceCollectionJobConfigurator>?job = null) where T : IJob
        {
            if (trigger is null)
            {
                throw new ArgumentNullException(nameof(trigger));
            }

            var jobConfigurator = new ServiceCollectionJobConfigurator(configurator.Services);
            var jobDetail       = ConfigureAndBuildJobDetail <T>(jobConfigurator, job);

            configurator.Services.AddTransient(x => jobDetail);
            configurator.Services.AddTransient(jobDetail.JobType);

            var triggerConfigurator = new ServiceCollectionTriggerConfigurator(configurator.Services);

            triggerConfigurator.ForJob(jobDetail);

            trigger.Invoke(triggerConfigurator);
            var t = triggerConfigurator.Build();

            if (t.JobKey is null || !t.JobKey.Equals(jobDetail.Key))
            {
                throw new InvalidOperationException("Trigger doesn't refer to job being scheduled");
            }

            configurator.Services.AddTransient(x => t);

            return(configurator);
        }
        private static IJobDetail ConfigureAndBuildJobDetail <T>(
            ServiceCollectionJobConfigurator builder,
            Action <IServiceCollectionJobConfigurator>?configure) where T : IJob
        {
            builder.OfType <T>();
            configure?.Invoke(builder);
            var jobDetail = builder.Build();

            return(jobDetail);
        }
示例#3
0
        /// <summary>
        /// Adds LibLog configuration to use Microsoft's logging abstraction instead of trying to find one.
        /// </summary>
        public static IServiceCollectionQuartzConfigurator AddJob <T>(
            this IServiceCollectionQuartzConfigurator configurator,
            Action <IServiceCollectionJobConfigurator>?configure = null) where T : IJob
        {
            var c = new ServiceCollectionJobConfigurator(configurator.Services);

            c.OfType <T>();

            configure?.Invoke(c);
            var jobDetail = c.Build();

            configurator.Services.AddTransient(x => jobDetail);
            configurator.Services.AddTransient(jobDetail.JobType);

            return(configurator);
        }
        /// <summary>
        /// Add job to underlying service collection. This API maybe change!
        /// </summary>
        public static IServiceCollectionQuartzConfigurator AddJob <T>(
            this IServiceCollectionQuartzConfigurator configurator,
            JobKey?jobKey = null,
            Action <IServiceCollectionJobConfigurator>?configure = null) where T : IJob
        {
            var c = new ServiceCollectionJobConfigurator(configurator.Services);

            if (jobKey != null)
            {
                c.WithIdentity(jobKey);
            }

            var jobDetail = ConfigureAndBuildJobDetail <T>(c, configure);

            configurator.Services.AddTransient(x => jobDetail);
            configurator.Services.AddTransient(jobDetail.JobType);

            return(configurator);
        }