コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScheduledJob"/> class.
        /// </summary>
        /// <param name="options"></param>
        protected ScheduledJob(SchedulerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            CronSchedule = options.CronSchedule;

            RunImmediately = options.RunImmediately;

            CronTimeZone = options?.CronTimeZone ?? string.Empty;
        }
コード例 #2
0
        private bool AddJob(string name, IScheduledJob job, SchedulerOptions options)
        {
            _jobs.AddOrUpdate(name, job, (n, j) => job);

            var wrapped = Create(name, job, options);

            if (wrapped != null)
            {
                _wrappedJobs.AddOrUpdate(name, wrapped, (n, v) => wrapped);

                return(true);
            }

            return(false);
        }
コード例 #3
0
        private SchedulerTaskWrapper?Create(string jobName, IScheduledJob job, SchedulerOptions options)
        {
            var currentTimeUtc = DateTimeOffset.UtcNow;
            var timeZone       = TimeZoneInfo.Local;

            if (string.IsNullOrEmpty(options.CronSchedule))
            {
                _logger.MissingCron(jobName, options.CronSchedule);

                return(null);
            }

            if (!string.IsNullOrEmpty(options.CronTimeZone))
            {
                try
                {
                    timeZone = TimeZoneInfo.FindSystemTimeZoneById(options.CronTimeZone);
                }
                catch (Exception ex)
                {
                    _logger.TimeZoneParseFailure(jobName, options.CronTimeZone, ex);
                    timeZone = TimeZoneInfo.Local;
                }
            }

            _logger.TimeZone(jobName, timeZone.Id);

            CronExpression crontabSchedule;

            if (options.CronSchedule.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).Length == 6)
            {
                crontabSchedule = CronExpression.Parse(options.CronSchedule, CronFormat.IncludeSeconds);
            }
            else
            {
                crontabSchedule = CronExpression.Parse(options.CronSchedule, CronFormat.Standard);
            }

            var nextRunTime = options.RunImmediately ? currentTimeUtc : crontabSchedule.GetNextOccurrence(currentTimeUtc, timeZone) !.Value;

            return(new SchedulerTaskWrapper(crontabSchedule, job, nextRunTime, timeZone));
        }
コード例 #4
0
        public bool AddOrUpdate(IScheduledJob job, SchedulerOptions options)
        {
            var name = job.GetType().Name;

            return(AddJob(name, job, options));
        }