private void StartWorker() { Worker = new Process() { StartInfo = new ProcessStartInfo(Path.Combine(AbsolutePathToServer, "node.exe")) { FileName = Path.Combine(AbsolutePathToServer, "node.exe"), WorkingDirectory = AbsolutePathToServer, Arguments = "server.js " + " --httpPort=" + _port + SerializeConfiguration(), UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, } }; Worker.StartInfo.EnvironmentVariables.Remove("NODE_ENV"); Worker.StartInfo.EnvironmentVariables.Add("NODE_ENV", "production"); Worker.OutputDataReceived += (sender, e) => { if (e.Data != null) { ServerStandardOutput += e.Data + "\n"; if (ServerStandardOutput.Length > 10000) { ServerStandardOutput = ServerStandardOutput.Substring(ServerStandardOutput.Length - 10000); } } }; Worker.ErrorDataReceived += (sender, e) => { if (e.Data != null) { ServerErrorOutput += e.Data + "\n"; if (ServerErrorOutput.Length > 10000) { ServerErrorOutput = ServerErrorOutput.Substring(ServerErrorOutput.Length - 10000); } } }; Worker.Start(); Worker.BeginOutputReadLine(); Worker.BeginErrorReadLine(); }
private async Task WaitForStarted() { _stopping = false; _stopped = false; var timeoutSw = new Stopwatch(); timeoutSw.Start(); bool done = false; var client = CreateClient(); var tcs = new TaskCompletionSource <object>(); Task.Run(async() => { while (!done) { if (_stopping || _stopped) { return; } if (!done && timeoutSw.Elapsed > StartTimeout) { await StopAsync(); tcs.SetException( new EmbeddedReportingServerException( "Failed to start jsreport server, output: " + ServerErrorOutput + ServerStandardOutput) { ServerErrorOutput = ServerErrorOutput, ServerStandardOutput = ServerStandardOutput }); return; } try { HttpResponseMessage response = client.GetAsync("/api/ping").Result; response.EnsureSuccessStatusCode(); string res = response.Content.ReadAsStringAsync().Result; if (!done && ServerStandardOutput.Contains("successfully started")) { done = true; tcs.SetResult(new object()); } } catch (Exception) { //waiting for server to startup } Thread.Sleep(500); } }); await tcs.Task.ConfigureAwait(false); }