Пример #1
0
        public MinioRulesDriver(IRulesClient rulesClient, MinioRulesDriverSettings settings, ILogger logger = null, IScheduler scheduler = null)
        {
            logger    = logger ?? NullLogger.Instance;
            scheduler = scheduler ?? DefaultScheduler.Instance;

            _pipeline = Observable.Create <Dictionary <string, RuleDefinition> >(async(sub, ct) =>
            {
                try
                {
                    while (!ct.IsCancellationRequested)
                    {
                        var latestVersion = await rulesClient.GetVersion(ct);
                        LastCheckTime     = scheduler.Now.UtcDateTime;
                        if (latestVersion != CurrentLabel)
                        {
                            var ruleset = await rulesClient.GetRuleset(latestVersion, ct);
                            sub.OnNext(ruleset);
                            CurrentLabel = latestVersion;
                        }
                        await Observable.Return(Unit.Default).Delay(settings.SampleInterval, scheduler);
                    }
                }
                catch (Exception ex)
                {
                    sub.OnError(ex);
                }
            })
                        .SubscribeOn(scheduler)
                        .Catch((Exception exception) =>
            {
                logger.LogWarning(exception, "Failed to update rules");
                return(Observable.Empty <Dictionary <string, RuleDefinition> >()
                       .Delay(settings.FailureDelay));
            })
                        .Repeat()
                        .Replay(1);

            _subscription = new CompositeDisposable(
                _pipeline.Subscribe(rules => OnRulesChange?.Invoke(rules)),
                _pipeline.Connect()
                );
        }
Пример #2
0
        public void Configure(IServiceCollection services, IConfiguration configuration)
        {
            var minioConfiguration = configuration.GetSection("Rules:Minio");

            var settings = new MinioRulesDriverSettings
            {
                SampleIntervalInMs = minioConfiguration.GetValue("SampleIntervalInMs", 30000),
                FailureDelayInMs   = minioConfiguration.GetValue("FailureDelayInMs", 60000),
            };

            services.AddSingleton(new MinioRulesClient(
                                      minioConfiguration.GetValue("Bucket", "tweek-ruleset"),
                                      minioConfiguration.GetValue <string>("Endpoint"),
                                      minioConfiguration.GetValueFromEnvOrFile("AccessKey", "AccessKeyPath"),
                                      minioConfiguration.GetValueFromEnvOrFile("SecretKey", "SecretKeyPath"),
                                      minioConfiguration.GetValue("Secure", false)
                                      ));

            services.AddSingleton <IRulesDriver>(ctx => new MinioRulesDriver(ctx.GetService <MinioRulesClient>(),
                                                                             settings, ctx.GetService <ILoggerFactory>().CreateLogger("RulesMinioDriver")));
        }