Beispiel #1
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);
            }
        }
Beispiel #2
0
        public void SetUp()
        {
            TypesForScalarDestructionHolder.Add(typeof(Money));
            LogManager.SetLoggerFactory(new DefaultLoggerFactory());

            DateTimeStrategyHolder.Current = new DefaultDateTimeStrategy();
            _container = GridNode.Container;
            CreateScheduler();
            _scheduler = GridNode.System.ActorOf(GridNode.System.DI().Props <SchedulingActor>());
            _quartzScheduler.Clear();
        }
Beispiel #3
0
        private async Task <bool> TrySetSchedule(string cronExpression, Quartz.IScheduler scheduler)
        {
            if (!CronExpression.IsValidExpression(cronExpression))
            {
                return(false);
            }

            IJobDetail job = JobBuilder.Create <PostWeeklySeedJob>()
                             .WithIdentity("WeeklySeedJob", "Group1")
                             .Build();

            ITrigger trigger = TriggerBuilder.Create()
                               .WithIdentity("schedule", "Group1")
                               .StartNow()
                               .WithCronSchedule(cronExpression)
                               .Build();

            await scheduler.Clear();

            await scheduler.ScheduleJob(job, trigger);

            return(true);
        }
 /// <summary>
 /// 清空调度任务
 /// </summary>
 public static void Clear()
 {
     _Scheduler.Clear();
 }