public bool Stop() { Log.LogInfo("Stopping service"); _listener.Stop(); _cacheServer.Stop(); Log.LogInfo("Service stopped successfully"); // stop this after last log HostServices.Stop(); return(true); }
public bool Start(string instance) { try { var configFile = Constants.NodeConfigFileName; if (instance != null) { var baseName = configFile.Split('.').FirstOrDefault(); configFile = $"{baseName}_{instance}.json"; } var nodeConfig = new NodeConfig { TcpPort = Constants.DefaultPort, IsPersistent = true }; if (File.Exists(configFile)) { try { var configFromFile = SerializationHelper.FormattedSerializer.Deserialize <NodeConfig>( new JsonTextReader(new StringReader(File.ReadAllText(configFile)))); nodeConfig = configFromFile; HostServices.Start(configFromFile.DataPath); Log.LogInfo("----------------------------------------------------------"); Log.LogInfo($"Reading configuration file {configFile} "); } catch (Exception e) { Log.LogError($"Error reading configuration file {configFile} : {e.Message}"); } } else { HostServices.Start(nodeConfig.DataPath); Log.LogWarning($"Configuration file {configFile} not found. Using defaults"); } _cacheServer = new Server.Server(nodeConfig); _listener = new TcpServerChannel(); _cacheServer.Channel = _listener; _listener.Init(nodeConfig.TcpPort); _listener.Start(); var fullDataPath = Path.GetFullPath(nodeConfig.DataPath ?? Constants.DataPath); var persistentDescription = nodeConfig.IsPersistent ? fullDataPath : " NO"; Console.Title = $"Cachalot Core on port {nodeConfig.TcpPort} persistent = {persistentDescription}"; Log.LogInfo( $"Starting hosted service on port {nodeConfig.TcpPort} persistent = {persistentDescription}"); _cacheServer.StopRequired += (sender, args) => { HostServices.Stop(); Stop(); _stopEvent.Set(); }; _cacheServer.Start(); } catch (Exception e) { Log.LogError($"Failed to start host: {e}"); return(false); } Log.LogInfo("Host started successfully"); return(true); }