private static void WarnRunningService() { var elasticsearchService = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName.Equals("Elasticsearch")); if (elasticsearchService != null && elasticsearchService.Status != ServiceControllerStatus.Stopped) { var status = Enum.GetName(typeof(ServiceControllerStatus), elasticsearchService.Status); ElasticsearchConsole.WriteLine(ConsoleColor.Blue, $"Elasticsearch already running as a service and currently: {status}. Elasticsearch might not be able to start when not passed a different clustername."); } }
static void Main(string[] args) { try { using (var service = new ElasticsearchService(args)) { if (Environment.UserInteractive) { WarnRunningService(); var handle = new ManualResetEvent(false); _consoleCtrlHandler += new ConsoleCtrlHandler(c => { ElasticsearchConsole.WriteLine(ConsoleColor.Red, "Stopping Elasticsearch..."); service.StopInteractive(); handle.Set(); return(false); }); SetConsoleCtrlHandler(_consoleCtrlHandler, true); service.StartInteractive(); handle.WaitOne(); } else { ServiceBase.Run(service); } } } catch (Exception e) { e.ToEventLog("Elasticsearch"); if (Environment.UserInteractive) { e.ToConsole("An exception occurred in Main()"); } } }
public static void ToConsole(this Exception e, string prefix) => ElasticsearchConsole.WriteLine(ConsoleColor.Red, $"{prefix}: {e}");
public void Start() { this.Stop(); var classPath = $"{this.ElasticsearchJar};{Path.Combine(this.LibDirectory, "*")}"; var arguments = JavaOptions.Split(' ') .Concat(new string[] { $"-Delasticsearch", $"-Des.path.home=\"{this.HomeDirectory}\"", $"-cp \"{classPath}\" org.elasticsearch.bootstrap.Elasticsearch", $"-E path.conf=\"{this.ConfigDirectory}\"" }) .Concat(this.AdditionalArguments) .ToList(); this._process = new ObservableProcess(this.JavaExe, arguments.ToArray()); //Create a hot observer on process that does not disposbe itself (Stop() method does this) var observable = Observable.Create <ConsoleOut>(observer => { this._disposables.Add(this._process.Start().Subscribe(observer)); return(Disposable.Empty); }) .Publish(); //promote to connectable observable //subscribe underlying observable stream this._disposables.Add(observable.Connect()); if (Environment.UserInteractive) { //subscribe to all messages and write them to console this._disposables.Add(observable.Subscribe(c => { if (c.Error) { ElasticsearchConsole.WriteLine(ConsoleColor.Red, c.Data); } else { ElasticsearchConsole.WriteLine(c.Data); } })); } //subscribe as long we are not in started state and attempt to read console out for this confirmation var handle = new ManualResetEvent(false); this._disposables.Add(observable .TakeWhile(c => !this.Started) .Select(consoleLine => new ElasticsearchMessage(this.Started, consoleLine.Data)) .Subscribe(onNext: s => HandleConsoleMessage(s, handle)) ); var timeout = TimeSpan.FromSeconds(120); if (!handle.WaitOne(TimeSpan.FromSeconds(120), true)) { this.Stop(); throw new Exception($"Could not start Elasticsearch within ({timeout}): {this.JavaExe} {string.Join(" ", arguments)}"); } }