public UnleashServices(UnleashSettings settings, IRandom random, IUnleashApiClientFactory unleashApiClientFactory,
                               IUnleashScheduledTaskManager scheduledTaskManager, IToggleCollectionCache toggleCollectionCache,
                               IEnumerable <IStrategy> strategies)
        {
            this.Random = random ?? throw new ArgumentNullException(nameof(random));

            this.scheduledTaskManager = scheduledTaskManager ?? throw new ArgumentNullException(nameof(scheduledTaskManager));

            var settingsValidator = new UnleashSettingsValidator();

            settingsValidator.Validate(settings);

            StrategyMap = BuildStrategyMap(strategies?.ToArray() ?? new IStrategy[0]);

            CancellationToken = cancellationTokenSource.Token;

            var cachedFilesResult = toggleCollectionCache.Load(cancellationTokenSource.Token).GetAwaiter().GetResult();

            cacheMiss = cachedFilesResult.IsCacheMiss;


            ToggleCollection = new ThreadSafeToggleCollection
            {
                Instance = cachedFilesResult.InitialToggleCollection ?? new ToggleCollection()
            };

            MetricsBucket = new ThreadSafeMetricsBucket();

            var scheduledTasks = CreateScheduledTasks(settings, unleashApiClientFactory, toggleCollectionCache, cachedFilesResult);

            scheduledTaskManager.Configure(scheduledTasks, CancellationToken);
        }
 public FetchFeatureTogglesTask(
     IUnleashApiClientFactory apiClientFactory,
     ThreadSafeToggleCollection toggleCollection,
     IToggleCollectionCache toggleCollectionCache,
     TaskCompletionSource <object> initializationTaskCompletionSource)
 {
     this.apiClientFactory      = apiClientFactory;
     this.toggleCollection      = toggleCollection;
     this.toggleCollectionCache = toggleCollectionCache;
     this.initializationTaskCompletionSource = initializationTaskCompletionSource;
 }
        private IEnumerable <IUnleashScheduledTask> CreateScheduledTasks(UnleashSettings settings,
                                                                         IUnleashApiClientFactory unleashApiClientFactory, IToggleCollectionCache toggleCollectionCache,
                                                                         ToggleCollectionCacheResult toggleCollectionCacheResult)
        {
            yield return(new FetchFeatureTogglesTask(
                             unleashApiClientFactory,
                             ToggleCollection,
                             toggleCollectionCache,
                             flagsFetchedFromUnleashTaskCompletionSource)
            {
                ExecuteDuringStartup = true,
                Interval = settings.FetchTogglesInterval,
                Etag = toggleCollectionCacheResult.InitialETag
            });

            if (settings.RegisterClient)
            {
                yield return(new ClientRegistrationBackgroundTask(
                                 unleashApiClientFactory,
                                 settings,
                                 StrategyMap.Keys.ToList())
                {
                    Interval = TimeSpan.Zero,
                    ExecuteDuringStartup = true
                });
            }

            if (settings.SendMetricsInterval != null)
            {
                yield return(new ClientMetricsBackgroundTask(
                                 unleashApiClientFactory,
                                 settings,
                                 MetricsBucket)
                {
                    ExecuteDuringStartup = false,
                    Interval = settings.SendMetricsInterval.Value
                });
            }
        }