Esempio n. 1
0
 public BackgroundTaskScheduler(string name, DateTime referenceTime)
 {
     Name          = name;
     ReferenceTime = referenceTime;
     Settings      = new BackgroundTaskSettings {
         Name = name
     };
     State = new BackgroundTaskState {
         Name = name
     };
 }
Esempio n. 2
0
        private async Task UpdateAsync(CancellationToken stoppingToken)
        {
            var referenceTime = DateTime.UtcNow;

            if (stoppingToken.IsCancellationRequested)
            {
                return;
            }

            var tasks = _serviceProvider.GetServices <IBackgroundTask>();

            CleanSchedulers();

            var backgroundTaskSettingsProvider = _serviceProvider.GetRequiredService <IBackgroundTaskSettingsProvider>();

            foreach (var task in tasks)
            {
                var taskName = task.GetTaskName();
                if (!_schedulers.TryGetValue(taskName, out var scheduler))
                {
                    _schedulers[taskName] = scheduler = new BackgroundTaskScheduler(taskName, referenceTime);
                }

                BackgroundTaskSettings settings = null;

                settings = await backgroundTaskSettingsProvider.GetSettingsAsync(task);

                scheduler.Settings = settings ?? task.GetDefaultSettings();
            }

            void CleanSchedulers()
            {
                var validKeys = tasks.Select(task => task.GetTaskName()).ToArray();

                var keys = _schedulers.Select(kv => kv.Key).ToArray();

                foreach (var key in keys)
                {
                    if (!validKeys.Contains(key))
                    {
                        _schedulers.TryRemove(key, out var scheduler);
                    }
                }
            }
        }