private void ScheduleJobsForQueue(BackgroundProcessContext context, string queueName, IStorageConnection connection)
        {
            var recurringJobIds = connection.GetAllItemsFromSetQueue("recurring-jobs", queueName);

            foreach (var recurringJobId in recurringJobIds)
            {
                var recurringJob = connection.GetAllEntriesFromHash(
                    $"recurring-job:{recurringJobId}");

                if (recurringJob == null)
                {
                    continue;
                }

                try
                {
                    TryScheduleJob(context.Storage, connection, recurringJobId, recurringJob, queueName);
                }
                catch (JobLoadException ex)
                {
                    Logger.WarnException(
                        $"Recurring job '{recurringJobId}' can not be scheduled due to job load exception.",
                        ex);
                }
            }

            _throttler.Delay(context.CancellationToken);
        }
        public void Execute(CancellationToken cancellationToken)
        {
            _throttler.Throttle(cancellationToken);

            using (var connection = _storage.GetConnection())
                using (connection.AcquireDistributedLock("recurring-jobs:lock", LockTimeout))
                {
                    var recurringJobIds = connection.GetAllItemsFromSet("recurring-jobs");

                    foreach (var recurringJobId in recurringJobIds)
                    {
                        var recurringJob = connection.GetAllEntriesFromHash(
                            String.Format("recurring-job:{0}", recurringJobId));

                        if (recurringJob == null)
                        {
                            continue;
                        }

                        try
                        {
                            TryScheduleJob(connection, recurringJobId, recurringJob);
                        }
                        catch (JobLoadException ex)
                        {
                            Logger.WarnFormat("Recurring job '{0}' can not be scheduled due to job load exception.", ex, recurringJobId);
                        }
                    }

                    _throttler.Delay(cancellationToken);
                }
        }
        public void Invoke(TaskContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _throttler.Throttle(context.CancellationToken);

            Task.WhenAll(_func.Invoke(context));

            _throttler.Delay(context.CancellationToken);
        }
Exemple #4
0
        public void Invoke(TaskContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _throttler.Throttle(context.CancellationToken);

            var cronJobs = _jobDictionary.Select(p => p.Value).ToArray();

            Task.WhenAll(cronJobs.Select(job => RunAsync(job, context)));

            _throttler.Delay(context.CancellationToken);
        }
Exemple #5
0
        /// <inheritdoc />
        public void Execute(BackgroundProcessContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _throttler.Throttle(context.CancellationToken);

            UseConnectionDistributedLock(context.Storage, connection =>
            {
                var recurringJobIds = connection.GetAllItemsFromSet("recurring-jobs");

                foreach (var recurringJobId in recurringJobIds)
                {
                    if (context.IsShutdownRequested)
                    {
                        return;
                    }

                    var recurringJob = connection.GetAllEntriesFromHash(
                        $"recurring-job:{recurringJobId}");

                    if (recurringJob == null)
                    {
                        continue;
                    }

                    try
                    {
                        TryScheduleJob(context.Storage, connection, recurringJobId, recurringJob);
                    }
                    catch (JobLoadException ex)
                    {
                        _logger.WarnException(
                            $"Recurring job '{recurringJobId}' can not be scheduled due to job load exception.",
                            ex);
                    }
                }
            });

            // The code above may be completed in less than a second. Default throttler use
            // the second resolution, and without an extra delay, CPU and DB bursts may happen.
            _throttler.Delay(context.CancellationToken);
        }
        /// <inheritdoc />
        public void Execute(BackgroundProcessContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            _throttler.Throttle(context.CancellationToken);

            using (var connection = context.Storage.GetConnection())
                using (connection.AcquireDistributedLock($"{PluginConstants.JobSet}:lock", LockTimeout))
                {
                    var recurringJobIds = connection.GetAllItemsFromSet(PluginConstants.JobSet);

                    foreach (var recurringJobId in recurringJobIds)
                    {
                        var recurringJob = connection.GetAllEntriesFromHash(
                            $"{PluginConstants.JobType}:{recurringJobId}");

                        if (recurringJob == null)
                        {
                            continue;
                        }

                        try
                        {
                            TryScheduleJob(context.Storage, connection, recurringJobId, recurringJob);
                        }
                        catch (JobLoadException ex)
                        {
                            Logger.WarnException(
                                $"Recurring job '{recurringJobId}' can not be scheduled due to job load exception.",
                                ex);
                        }
                    }
                }

            // The code above may be completed in less than a second. Default throttler use
            // the second resolution, and without an extra delay, CPU and DB bursts may happen.
            _throttler.Delay(context.CancellationToken);
        }
        /// <inheritdoc />
        public void Execute(BackgroundProcessContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            _throttler.Throttle(context.CancellationToken);

            using (var connection = context.Storage.GetConnection())
                using (connection.AcquireDistributedLock("recurring-jobs:lock", LockTimeout))
                {
                    var recurringJobIds = connection.GetAllItemsFromSet("recurring-jobs");

                    foreach (var recurringJobId in recurringJobIds)
                    {
                        var recurringJob = connection.GetAllEntriesFromHash(
                            String.Format("recurring-job:{0}", recurringJobId));

                        if (recurringJob == null)
                        {
                            continue;
                        }

                        try
                        {
                            TryScheduleJob(context.Storage, connection, recurringJobId, recurringJob);
                        }
                        catch (JobLoadException ex)
                        {
                            Logger.WarnException(
                                String.Format(
                                    "Recurring job '{0}' can not be scheduled due to job load exception.",
                                    recurringJobId),
                                ex);
                        }
                    }

                    _throttler.Delay(context.CancellationToken);
                }
        }
        public async Task Invoke(Func <BatchContext, Task> next, BatchContext context)
        {
            _timer.Start();

            await next(context).ConfigureAwait(false);

            var currentInterval = _timer.ElapsedMilliseconds / _intervalInMilliSeconds;

            var startInterval = (currentInterval - _rollingIntervals > 0) ? currentInterval - _rollingIntervals : 0;
            var messagesProcessedInLastInterval = 0;

            for (long i = startInterval; i <= currentInterval; i++)
            {
                var intervalFound = _messagesProcessed.FirstOrDefault(x => x.IntervalNumber == i);
                if (intervalFound != null)
                {
                    messagesProcessedInLastInterval += intervalFound.MessagesProcessed;
                }
            }

            var elapsedSinceLastIntervalBegan = _timer.ElapsedMilliseconds - currentInterval * _intervalInMilliSeconds;
            var wholeIntervalsElapsed         = currentInterval - startInterval;

            var    elapsedTime      = wholeIntervalsElapsed * _intervalInMilliSeconds + elapsedSinceLastIntervalBegan;
            double elapsedIntervals = (double)elapsedTime / (_intervalInMilliSeconds);
            var    processingRate   = _intervalsPerMinute * messagesProcessedInLastInterval / elapsedIntervals;

            if (processingRate > _rateLimit)
            {
                var targetTimePerMessage = 1000 * 60 / _rateLimit;
                var targetTime           = messagesProcessedInLastInterval * targetTimePerMessage;
                var delay = targetTime - elapsedTime;
                OnThrottling(new ThrottlingEventArgs(delay));
                await _throttler.Delay(delay).ConfigureAwait(false);
            }
        }