public StaytusService(IConfigurationSection service, IStatusResolver resolver, CustomResolverStrategy strategy, StaytusClient staytus, ILogger logger) { _service = service; _resolver = resolver; _strategy = strategy; _staytus = staytus; _logger = logger; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Daemon started, loading plugins"); LoadPlugins(); _logger.LogInformation("Discovering service units"); var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly()?.Location); var servicesLocation = Path.Combine(assemblyLocation, "Services"); var configurationBuilder = new ConfigurationBuilder(); foreach (var file in Directory.GetFiles(servicesLocation, "*.ini")) { _logger.LogInformation("Service file {0} found. Registering.", file); configurationBuilder.AddIniFile(file); } var svcConfig = configurationBuilder.Build(); var services = svcConfig.GetChildren(); var defaultStrategy = new DefaultResolverStrategy(_config.GetSection("Defaults")); foreach (var service in services) { var serviceType = service["Type"]; _logger.LogInformation("Service '{0}' has type {1}, searching for resolver.", service.Key, serviceType); var resolver = _pluginManager.GetResolver(serviceType); _logger.LogInformation("Creating strategy from service config."); var strategy = new CustomResolverStrategy(defaultStrategy, service); _logger.LogInformation("Checks complete, starting service {0}.", service.Key); _ = DispatchServiceUpdaterAsync(service, resolver, strategy, stoppingToken); } await Task.Delay(-1, stoppingToken); }
private async Task DispatchServiceUpdaterAsync(IConfigurationSection service, IStatusResolver resolver, CustomResolverStrategy strategy, CancellationToken ct) { var context = new ResolveContext { Host = service["Host"], Logger = _loggerFactory.CreateLogger(service.Key), Port = service.GetValue <ushort>("Port", 80), ServiceConfig = service }; var serviceHost = new StaytusService(service, resolver, strategy, _staytus, _loggerFactory.CreateLogger(service.Key)); while (!ct.IsCancellationRequested) { await serviceHost.UpdateAsync(context, ct); await Task.Delay(strategy.Interval, ct); } }