/// <summary>
        /// Add job to underlying service collection.jobType shoud be implement `IJob`
        /// </summary>
        public static IServiceCollectionQuartzConfigurator AddJob(
            this IServiceCollectionQuartzConfigurator options,
            Type jobType,
            JobKey?jobKey = null,
            Action <IJobConfigurator>?configure = null)
        {
            if (!typeof(IJob).IsAssignableFrom(jobType))
            {
                ExceptionHelper.ThrowArgumentException("jobType must implement the IJob interface", nameof(jobType));
            }
            var c = new JobConfigurator();

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

            var jobDetail = ConfigureAndBuildJobDetail(jobType, c, configure, hasCustomKey: out _);

            options.Services.Configure <QuartzOptions>(x =>
            {
                x.jobDetails.Add(jobDetail);
            });

            return(options);
        }
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a <see cref="ITrigger" />
        /// fires that is associated with the <see cref="IJob" />.
        /// </summary>
        public virtual async Task Execute(IJobExecutionContext context)
        {
            jobKey = context.JobDetail.Key;
            Console.WriteLine("---- {0} executing at {1:r}", jobKey, DateTime.Now);

            try
            {
                // main job loop...
                // do some work... in this example we are 'simulating' work by sleeping...

                for (int i = 0; i < 4; i++)
                {
                    await Task.Delay(10 *1000);

                    // periodically check if we've been interrupted...
                    if (context.CancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("--- {0}  -- Interrupted... bailing out!", jobKey);
                        return; // could also choose to throw a JobExecutionException
                        // if that made for sense based on the particular
                        // job's responsibilities/behaviors
                    }
                }
            }
            finally
            {
                Console.WriteLine("---- {0} completed at {1:r}", jobKey, DateTime.Now);
            }
        }
 /// <summary>
 /// Add job to underlying service collection. This API maybe change!
 /// </summary>
 public static IServiceCollectionQuartzConfigurator AddJob <T>(
     this IServiceCollectionQuartzConfigurator options,
     JobKey?jobKey = null,
     Action <IJobConfigurator>?configure = null) where T : IJob
 {
     return(AddJob(options, typeof(T), jobKey, configure));
 }
Esempio n. 4
0
        /// <summary>
        /// Set the identity of the Job which should be fired by the produced
        /// Trigger, by extracting the JobKey from the given job.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="jobDetail">the Job to fire.</param>
        /// <returns>the updated TriggerBuilder</returns>
        /// <seealso cref="ITrigger.JobKey" />
        public TriggerBuilder ForJob(IJobDetail jobDetail)
        {
            JobKey k = jobDetail.Key;

            if (k.Name == null)
            {
                throw new ArgumentException("The given job has not yet had a name assigned to it.");
            }
            jobKey = k;
            return(this);
        }
        /// <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);
        }
Esempio n. 6
0
        /// <summary>
        /// Produce the <see cref="IJobDetail" /> instance defined by this JobBuilder.
        /// </summary>
        /// <returns>the defined JobDetail.</returns>
        public IJobDetail Build()
        {
            JobDetailImpl job = new JobDetailImpl();

            job.JobType     = jobType;
            job.Description = description;
            if (key == null)
            {
                key = new JobKey(Guid.NewGuid().ToString());
            }
            job.Key              = key;
            job.Durable          = durability;
            job.RequestsRecovery = shouldRecover;

            if (!jobDataMap.IsEmpty)
            {
                job.JobDataMap = jobDataMap;
            }

            return(job);
        }
Esempio n. 7
0
        /// <summary>
        /// Add job to underlying service collection. This API maybe change!
        /// </summary>
        public static IServiceCollectionQuartzConfigurator AddJob <T>(
            this IServiceCollectionQuartzConfigurator options,
            JobKey?jobKey = null,
            Action <IJobConfigurator>?configure = null) where T : IJob
        {
            var c = new JobConfigurator();

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

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

            options.Services.Configure <QuartzOptions>(x =>
            {
                x.jobDetails.Add(jobDetail);
            });
            options.Services.TryAddTransient(jobDetail.JobType);

            return(options);
        }
Esempio n. 8
0
 /// <summary>
 /// Set the identity of the Job which should be fired by the produced
 /// Trigger - a <see cref="JobKey" /> will be produced with the given
 /// name and group.
 /// </summary>
 /// <remarks>
 /// </remarks>
 /// <param name="jobName">the name of the job to fire.</param>
 /// <param name="jobGroup">the group of the job to fire.</param>
 /// <returns>the updated TriggerBuilder</returns>
 /// <seealso cref="ITrigger.JobKey" />
 public TriggerBuilder ForJob(string jobName, string jobGroup)
 {
     jobKey = new JobKey(jobName, jobGroup);
     return(this);
 }
Esempio n. 9
0
 /// <summary>
 /// Set the identity of the Job which should be fired by the produced
 /// Trigger.
 /// </summary>
 /// <remarks>
 /// </remarks>
 /// <param name="jobKey">the identity of the Job to fire.</param>
 /// <returns>the updated TriggerBuilder</returns>
 /// <seealso cref="ITrigger.JobKey" />
 public TriggerBuilder ForJob(JobKey jobKey)
 {
     this.jobKey = jobKey;
     return(this);
 }
Esempio n. 10
0
 /// <summary>
 /// Use a <see cref="JobKey" /> to identify the JobDetail.
 /// </summary>
 /// <remarks>
 /// <para>If none of the 'withIdentity' methods are set on the JobBuilder,
 /// then a random, unique JobKey will be generated.</para>
 /// </remarks>
 /// <param name="key">the Job's JobKey</param>
 /// <returns>the updated JobBuilder</returns>
 /// <seealso cref="JobKey" />
 /// <seealso cref="IJobDetail.Key" />
 public JobBuilder WithIdentity(JobKey key)
 {
     this.key = key;
     return(this);
 }
Esempio n. 11
0
 /// <summary>
 /// Use a <see cref="JobKey" /> with the given name and group to
 /// identify the JobDetail.
 /// </summary>
 /// <remarks>
 /// <para>If none of the 'withIdentity' methods are set on the JobBuilder,
 /// then a random, unique JobKey will be generated.</para>
 /// </remarks>
 /// <param name="name">the name element for the Job's JobKey</param>
 /// <param name="group"> the group element for the Job's JobKey</param>
 /// <returns>the updated JobBuilder</returns>
 /// <seealso cref="JobKey" />
 /// <seealso cref="IJobDetail.Key" />
 public JobBuilder WithIdentity(string name, string group)
 {
     key = new JobKey(name, group);
     return(this);
 }
Esempio n. 12
0
        public async Task <JobKey> ScheduleJob <T>(DateTimeOffset?startAtTime = null, JobKey?vncCheckerJobKey = null)
            where T : class, IJob
        {
            IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await scheduler.Start();

            IJobDetail jobDetail = JobBuilder.Create <T>().Build();

            ITrigger       trigger;
            TriggerBuilder triggerBuilder = TriggerBuilder.Create().StartNow()
                                            .WithSimpleSchedule(x => x.WithIntervalInSeconds(Constants.JobEecutionIntervalSec)
                                                                .RepeatForever());

            if (vncCheckerJobKey != null)
            {
                triggerBuilder = triggerBuilder.UsingJobData(nameof(Checker), vncCheckerJobKey.Name);
            }

            if (startAtTime != null)
            {
                trigger = triggerBuilder.StartAt((DateTimeOffset)startAtTime).Build();
            }
            else
            {
                trigger = triggerBuilder.StartNow().Build();
            }

            await scheduler.ScheduleJob(jobDetail, trigger);

            return(jobDetail.Key);
        }