Пример #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            ThrowIfDisposed();

            if (_timer != null && _timer.Enabled)
            {
                throw new InvalidOperationException("The listener has already been started.");
            }

            // if schedule monitoring is enabled for this timer job, check to see if we've
            // missed an occurrence since we last started
            DateTime now = DateTime.UtcNow;

            if (_attribute.UseMonitor)
            {
                if (await _scheduleMonitor.IsPastDueAsync(_timerName, now, _schedule))
                {
                    // we've missed an occurrence so invoke the job function immediately
                    await InvokeJobFunction(now, true);
                }
            }

            _timer = new System.Timers.Timer
            {
                AutoReset = false
            };
            _timer.Elapsed += OnTimer;

            DateTime nextOccurrence = _schedule.GetNextOccurrence(now);
            TimeSpan nextInterval   = nextOccurrence - now;

            StartTimer(nextInterval);
        }
Пример #2
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            ThrowIfDisposed();

            if (_timer != null && _timer.Enabled)
            {
                throw new InvalidOperationException("The listener has already been started.");
            }

            // See if we need to invoke the function immediately now, and if
            // so invoke it.
            DateTime now = DateTime.Now;

            if (_attribute.UseMonitor &&
                await _scheduleMonitor.IsPastDueAsync(_timerName, now, _schedule))
            {
                // If schedule monitoring is enabled for this timer job, check to see if we've
                // missed an occurrence since we last started. If we have, invoke it immediately.
                _trace.Verbose(string.Format("Function '{0}' is past due on startup. Executing now.", _timerName));
                await InvokeJobFunction(now, true);
            }
            else if (_attribute.RunOnStartup)
            {
                // The job is configured to run immediately on startup
                _trace.Verbose(string.Format("Function '{0}' is configured to run on startup. Executing now.", _timerName));
                await InvokeJobFunction(now);
            }

            // log the next several occurrences to console for visibility
            string nextOccurrences = TimerInfo.FormatNextOccurrences(_schedule, 5);

            _trace.Verbose(nextOccurrences);

            // start the timer
            now = DateTime.Now;
            DateTime nextOccurrence = _schedule.GetNextOccurrence(now);
            TimeSpan nextInterval   = nextOccurrence - now;

            StartTimer(nextInterval);
        }