コード例 #1
0
        void EnsurePeriodicBackgroundSave(CancellationToken stoppingToken)
        {
            if (!queue.NeedsSaving)
            {
                return;
            }

            if (backgroundSave == null || backgroundSave.IsCompleted)
            {
                logger.LogInformation("Starting periodic queue saving");

                backgroundSave = Task.Run(async() =>
                {
                    while (!stoppingToken.IsCancellationRequested)
                    {
                        try
                        {
                            var delay = TimeSpan.FromMilliseconds(options.Value
                                                                  .PeriodicBackgroundSaveIntervalMilliseconds);
                            await Task.Delay(
                                delay,
                                stoppingToken);

                            await PersistQueue();
                            var length = await queue.GetLength();

                            if (length == 0 && !inprogress)
                            {
                                logger.LogInformation("Stopping periodic queue saving as queue is empty");
                                break;
                            }

                            logger.LogInformation("Periodic save of queue to storage complete. Next save in {time:#}s",
                                                  delay.TotalSeconds);
                        }
                        catch (OperationCanceledException)
                        {
                        }
                    }
                }, stoppingToken);
            }
        }
コード例 #2
0
        public async Task <QueueInfo> Get()
        {
            var queueLength = await queue.GetLength();

            var deadLetterItems = await queue.GetDeadLetterItems();

            return(new QueueInfo
            {
                Date = DateTime.UtcNow,
                QueueLength = queueLength,
                DeadLetterLength = deadLetterItems.Count
            });
        }