Beispiel #1
0
        private async Task <ITimeZone> LoadLocalTimeZoneAsync()
        {
            var timeZoneResults = new List <TimeZoneSelectorResult>();

            foreach (var timeZoneSelector in _timeZoneSelectors)
            {
                var timeZoneResult = await timeZoneSelector.GetTimeZoneAsync();

                if (timeZoneResult != null)
                {
                    timeZoneResults.Add(timeZoneResult);
                }
            }

            if (timeZoneResults.Count == 0)
            {
                return(_clock.GetSystemTimeZone());
            }
            else if (timeZoneResults.Count > 1)
            {
                timeZoneResults.Sort((x, y) => y.Priority.CompareTo(x.Priority));
            }

            foreach (var result in timeZoneResults)
            {
                var value = await result.TimeZoneId();

                if (!String.IsNullOrEmpty(value))
                {
                    return(_clock.GetTimeZone(value));
                }
            }

            return(_clock.GetSystemTimeZone());
        }
        public async Task <ITimeZone> GetUserTimeZoneAsync()
        {
            string currentTimeZoneId = await GetCurrentUserTimeZoneIdAsync();

            if (String.IsNullOrEmpty(currentTimeZoneId))
            {
                return(null);
            }

            return(_clock.GetTimeZone(currentTimeZoneId));
        }
        private async Task UpdateAsync(IEnumerable <ShellContext> previousShells, IEnumerable <ShellContext> runningShells, CancellationToken stoppingToken)
        {
            var referenceTime = DateTime.UtcNow;

            await GetShellsToUpdate(previousShells, runningShells).ForEachAsync(async shell =>
            {
                var tenant = shell.Settings.Name;

                if (stoppingToken.IsCancellationRequested)
                {
                    return;
                }

                _httpContextAccessor.HttpContext = shell.CreateHttpContext();

                var shellScope = await _shellHost.GetScopeAsync(shell.Settings);

                if (!_options.ShellWarmup && shellScope.ShellContext.Pipeline == null)
                {
                    return;
                }

                await shellScope.UsingAsync(async scope =>
                {
                    var tasks = scope.ServiceProvider.GetServices <IBackgroundTask>();

                    CleanSchedulers(tenant, tasks);

                    if (!tasks.Any())
                    {
                        return;
                    }

                    var settingsProvider  = scope.ServiceProvider.GetService <IBackgroundTaskSettingsProvider>();
                    _changeTokens[tenant] = settingsProvider?.ChangeToken ?? NullChangeToken.Singleton;

                    ITimeZone timeZone = null;

                    var siteService = scope.ServiceProvider.GetService <ISiteService>();
                    if (siteService != null)
                    {
                        try
                        {
                            timeZone = _clock.GetTimeZone((await siteService.GetSiteSettingsAsync()).TimeZoneId);
                        }
                        catch (Exception ex) when(!ex.IsFatal())
                        {
                            _logger.LogError(ex, "Error while getting the time zone from the site settings of the tenant '{TenantName}'.", tenant);
                        }
                    }

                    foreach (var task in tasks)
                    {
                        var taskName = task.GetTaskName();

                        if (!_schedulers.TryGetValue(tenant + taskName, out var scheduler))
                        {
                            _schedulers[tenant + taskName] = scheduler = new BackgroundTaskScheduler(tenant, taskName, referenceTime, _clock);
                        }

                        scheduler.TimeZone = timeZone;

                        if (!scheduler.Released && scheduler.Updated)
                        {
                            continue;
                        }

                        BackgroundTaskSettings settings = null;

                        if (settingsProvider != null)
                        {
                            try
                            {
                                settings = await settingsProvider.GetSettingsAsync(task);
                            }
                            catch (Exception ex) when(!ex.IsFatal())
                            {
                                _logger.LogError(ex, "Error while updating settings of background task '{TaskName}' on tenant '{TenantName}'.", taskName, tenant);
                            }
                        }

                        settings ??= task.GetDefaultSettings();

                        if (scheduler.Released || !scheduler.Settings.Schedule.Equals(settings.Schedule))
                        {
                            scheduler.ReferenceTime = referenceTime;
                        }

                        scheduler.Settings = settings;
                        scheduler.Released = false;
                        scheduler.Updated  = true;
                    }
                });
            });
        }