Example #1
0
        private async Task RunAsync()
        {
            var      random        = new SafeRandom();
            TimeSpan?overrideDelay = random.NextTimeSpan(InitialReadRetryPeriod);

            while (await listRefreshTimer.NextTick(overrideDelay))
            {
                try
                {
                    overrideDelay = null;
                    switch (Status)
                    {
                    case GrainServiceStatus.Booting:
                        await DoInitialReadAndUpdateReminders();

                        break;

                    case GrainServiceStatus.Started:
                        await ReadAndUpdateReminders();

                        break;

                    default:
                        listRefreshTimer.Dispose();
                        return;
                    }
                }
                catch (Exception exception)
                {
                    this.logger.LogWarning(exception, "Exception while reading reminders: {Exception}", exception);
                    overrideDelay = random.NextTimeSpan(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(20));
                }
            }
        }
        private async Task Run()
        {
            var      options       = _messagingOptions.CurrentValue;
            var      optionsPeriod = options.GrainWorkloadAnalysisPeriod;
            TimeSpan nextDelay     = optionsPeriod > TimeSpan.Zero ? optionsPeriod : DefaultAnalysisPeriod;

            while (await _scanPeriodTimer.NextTick(nextDelay))
            {
                options       = _messagingOptions.CurrentValue;
                optionsPeriod = options.GrainWorkloadAnalysisPeriod;

                if (optionsPeriod <= TimeSpan.Zero)
                {
                    // Scanning is disabled. Wake up and check again soon.
                    nextDelay = DefaultAnalysisPeriod;
                    if (_enabled)
                    {
                        _enabled = false;
                    }

                    _recentlyUsedActivations.Clear();
                    continue;
                }

                nextDelay = optionsPeriod;
                if (!_enabled)
                {
                    _enabled = true;
                }

                var iteration = 0;
                var now       = DateTime.UtcNow;
                foreach (var activationEntry in _recentlyUsedActivations)
                {
                    var activation = activationEntry.Value;
                    lock (activation)
                    {
                        if (activation.IsInactive && activation.GetIdleness(now) > InactiveGrainIdleness)
                        {
                            _recentlyUsedActivations.TryRemove(activation, out _);
                            continue;
                        }

                        activation.AnalyzeWorkload(now, _messageCenter, _messageFactory, options);
                    }

                    // Yield execution frequently
                    if (++iteration % 100 == 0)
                    {
                        await Task.Yield();

                        now = DateTime.UtcNow;
                    }
                }
            }
        }