Exemplo n.º 1
0
        public async Task Execute(IJobExecutionContext context)
        {
            _logger.LogInformation("Start JobSchedule!");
            var listSchedule = _context.Test_schedule.Where(p => p.status.ToLower().Equals("running")).ToList();

            foreach (var schedule in listSchedule)
            {
                IJobDetail job = JobBuilder.Create <RunTestcaseJob>()
                                 .UsingJobData("scheduleid", schedule.id_schedule)
                                 .WithIdentity(schedule.id_schedule, schedule.id_user)
                                 .StoreDurably()
                                 .RequestRecovery()
                                 .Build();

                await _scheduler.AddJob(job, true);

                string[] time    = schedule.job_repeat_time.Split(':');
                ITrigger trigger = TriggerBuilder.Create()
                                   .ForJob(job)
                                   .WithIdentity(schedule.id_schedule + "trigger", schedule.id_user)
                                   .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(int.Parse(time[0]), int.Parse(time[1])))
                                   //.StartNow()
                                   //.WithSimpleSchedule(z => z.WithIntervalInSeconds(10).RepeatForever())
                                   .Build();

                await _scheduler.ScheduleJob(trigger);
            }
        }
Exemplo n.º 2
0
        private async Task <bool> LoadConfiguration(CancellationToken cancellationToken = default)
        {
            _failedToLoad = true;
            try
            {
                if (!_quartzScheduler.InStandbyMode)
                {
                    throw new InvalidOperationException($"{nameof(LoadConfiguration)} should be called when {nameof(Scheduler)} is stopped.");
                }

                // Load configuration
                var settings = _schedulerConfiguration
                               .GetSection(nameof(SchedulerSettings))
                               .Get <SchedulerSettings>();
                if (settings == null)
                {
                    throw new ArgumentException("Scheduler settings cannot be null!");
                }
                await _quartzScheduler.Clear();

                _internalDetails = new HashSet <SchedulerJobDetails>();

                // Validate settings
                settings.Validate();

                foreach (var job in settings.Jobs)
                {
                    var details = new SchedulerJobDetails
                    {
                        Name              = job.Name,
                        CronExpression    = job.CronExpression,
                        JobInstanceAsJson = job.AsJson()
                    };

                    var quartzJob     = job.GetQuartzJob();
                    var quartzTrigger = job.GetQuartzTrigger();
                    if (job.Active)
                    {
                        await _quartzScheduler.ScheduleJob(quartzJob, quartzTrigger, cancellationToken);
                    }
                    else
                    {
                        await _quartzScheduler.AddJob(quartzJob, true, true, cancellationToken);
                    }

                    details.JobKey         = quartzJob.Key;
                    details.RefireIfMissed = job.RefireIfMissed;
                    _internalDetails.Add(details);
                }

                _failedToLoad = false;
                return(true);
            }
            catch (Exception x)
            {
                _logger.LogError(x, $"Failed to load scheduler settings: {x.Message}");
                return(false);
            }
        }