public void TryInitializeService(string id, ServiceInitializationOptions options) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } try { InitializeService(id, options); } catch (Exception exception) { _logger.LogError(exception, $"Error while initializing service '{id}'."); } }
public void InitializeService(string id, ServiceInitializationOptions options) { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } try { if (!_storageService.TryRead(out ServiceConfiguration configuration, ServicesDirectory, id, DefaultFilenames.Configuration)) { throw new ServiceNotFoundException(id); } if (!configuration.IsEnabled) { _logger.LogInformation($"Service '{id}' not initialized because it is disabled."); return; } if (configuration.DelayedStart && options.SkipIfDelayed) { return; } if (!configuration.DelayedStart && options.SkipIfNotDelayed) { return; } _logger.LogInformation($"Initializing service '{id}'."); var serviceInstance = CreateServiceInstance(id, configuration); serviceInstance.ExecuteFunction("initialize"); _logger.LogInformation($"Service '{id}' initialized."); lock (_services) { if (_services.TryGetValue(id, out var existingServiceInstance)) { _logger.LogInformation($"Stopping service '{id}'."); existingServiceInstance.ExecuteFunction("stop"); _logger.LogInformation($"Service '{id}' stopped."); } _services[id] = serviceInstance; _logger.LogInformation($"Starting service '{id}'."); serviceInstance.ExecuteFunction("start"); _logger.LogInformation($"Service '{id}' started."); } } catch { lock (_services) { _services.Remove(id); } throw; } }