Esempio n. 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                var time = DateTime.UtcNow;

                UploadTask[] expiredTasks;

                lock (_tasks)
                {
                    expiredTasks = _tasks.Values.Where(t => t.UpdateTime.AddMilliseconds(_options.UploadTaskExpiryMs) <= time).ToArray();

                    foreach (var task in expiredTasks)
                    {
                        _tasks.Remove(task.Id);
                    }
                }

                foreach (var task in expiredTasks)
                {
                    try
                    {
                        task.Dispose(); // disposal is expensive so run outside lock

                        _logger.LogInformation($"Upload worker {task.Id} expired after inactivity.");
                    }
                    catch (Exception e)
                    {
                        _logger.LogWarning(e, $"Failed to dispose upload worker {task.Id}'.");
                    }
                }

                await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Note: This does not dispose the task. The task should be disposed by the creator.
        /// </summary>
        public UploadTask <T> RemoveTask <T>(string id)
        {
            lock (_tasks)
            {
                var task = GetTask <T>(id);

                if (task != null)
                {
                    _tasks.Remove(task.Id);
                }

                return(task);
            }
        }