public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime lifetime) { Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(_configuration) .WriteTo.Console(new JsonFormatter()) .Enrich.FromLogContext() .CreateLogger(); _logger.LogInformation("Starting service"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } RunSSHDeamon(lifetime, loggerFactory.CreateLogger("sshd")); var executor = ShellHelper.Executor.WithWorkingDirectory(_configuration.GetValue <string>("REPO_LOCATION")) .ForwardEnvVariable("GIT_SSH"); var gitValidationFlow = new GitValidationFlow { Validators = { (Patterns.Manifests, new CircularDependencyValidator()), (Patterns.JPad, new CompileJPadValidator()) } }; var minioConfig = _configuration.GetSection("Minio"); var storageClient = Policy.Handle <Exception>() .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))) .ExecuteAsync(() => MinioBucketStorage.GetOrCreateBucket(CreateMinioClient(minioConfig), minioConfig.GetValue("Bucket", "tweek-ruleset"))) .Result; var natsClient = new NatsPublisher(_configuration.GetSection("Nats").GetValue <string>("Endpoint")); var versionPublisher = natsClient.GetSubjectPublisher("version"); var repoSynchronizer = new RepoSynchronizer(executor.WithUser("git").CreateCommandExecutor("git")); var storageSynchronizer = new StorageSynchronizer(storageClient, executor.WithUser("git"), new Packer()); storageSynchronizer.Sync(repoSynchronizer.CurrentHead().Result, checkForStaleRevision: false).Wait(); RunIntervalPublisher(lifetime, versionPublisher, repoSynchronizer, storageSynchronizer); var syncActor = SyncActor.Create(storageSynchronizer, repoSynchronizer, natsClient, lifetime.ApplicationStopping, loggerFactory.CreateLogger("SyncActor")); app.UseRouter(router => { router.MapGet("validate", ValidationHandler.Create(executor, gitValidationFlow, loggerFactory.CreateLogger <ValidationHandler>())); router.MapGet("sync", SyncHandler.Create(syncActor, _syncPolicy)); router.MapGet("push", PushHandler.Create(syncActor)); router.MapGet("log", async(req, res, routedata) => _logger.LogInformation(req.Query["message"])); router.MapGet("health", async(req, res, routedata) => await res.WriteAsync(JsonConvert.SerializeObject(new { }))); router.MapGet("version", async(req, res, routedata) => await res.WriteAsync(Assembly.GetEntryAssembly() .GetCustomAttribute <AssemblyInformationalVersionAttribute>().InformationalVersion)); }); }
private void RunIntervalPublisher(IApplicationLifetime lifetime, Func <string, Task> publisher, RepoSynchronizer repoSynchronizer, StorageSynchronizer storageSynchronizer) { var intervalPublisher = new IntervalPublisher(publisher); var job = intervalPublisher.PublishEvery(TimeSpan.FromSeconds(60), async() => { var commitId = await repoSynchronizer.CurrentHead(); await Policy.Handle <StaleRevisionException>() .RetryAsync(10, async(_, c) => await repoSynchronizer.SyncToLatest()) .ExecuteAsync(async() => await storageSynchronizer.Sync(commitId)); _logger.LogInformation($"SyncVersion:{commitId}"); return(commitId); }); lifetime.ApplicationStopping.Register(job.Dispose); }