mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Restored Killed console message
Added Started console message Captured possible exception from node process
This commit is contained in:
@@ -15,7 +15,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
private string _commandLineArguments;
|
||||
private Process _nodeProcess;
|
||||
private TaskCompletionSource<bool> _nodeProcessIsReadySource;
|
||||
|
||||
|
||||
protected Process NodeProcess {
|
||||
get {
|
||||
// This is only exposed to support the unreliable OutOfProcessNodeRunner, which is just to verify that
|
||||
@@ -23,20 +23,20 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
return this._nodeProcess;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public OutOfProcessNodeInstance(string entryPointScript, string commandLineArguments = null)
|
||||
{
|
||||
this._childProcessLauncherLock = new object();
|
||||
this._entryPointScript = new StringAsTempFile(entryPointScript);
|
||||
this._commandLineArguments = commandLineArguments ?? string.Empty;
|
||||
}
|
||||
|
||||
|
||||
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
|
||||
|
||||
|
||||
public Task<string> Invoke(string moduleName, params object[] args) {
|
||||
return this.InvokeExport(moduleName, null, args);
|
||||
}
|
||||
|
||||
|
||||
public async Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args) {
|
||||
return await this.Invoke(new NodeInvocationInfo {
|
||||
ModuleName = moduleName,
|
||||
@@ -44,7 +44,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
Args = args
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected async Task EnsureReady() {
|
||||
lock (this._childProcessLauncherLock) {
|
||||
if (this._nodeProcess == null || this._nodeProcess.HasExited) {
|
||||
@@ -55,36 +55,44 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
|
||||
|
||||
// Append current directory to NODE_PATH so it can locate node_modules
|
||||
var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
|
||||
if (existingNodePath != string.Empty) {
|
||||
existingNodePath += ":";
|
||||
}
|
||||
|
||||
|
||||
var nodePathValue = existingNodePath + Path.Combine(Directory.GetCurrentDirectory(), "node_modules");
|
||||
#if DNX451
|
||||
startInfo.EnvironmentVariables.Add("NODE_PATH", nodePathValue);
|
||||
#else
|
||||
startInfo.Environment.Add("NODE_PATH", nodePathValue);
|
||||
#endif
|
||||
|
||||
|
||||
this.OnBeforeLaunchProcess();
|
||||
this._nodeProcess = Process.Start(startInfo);
|
||||
this.ConnectToInputOutputStreams();
|
||||
}
|
||||
}
|
||||
|
||||
var initializationSucceeded = await this._nodeProcessIsReadySource.Task;
|
||||
|
||||
var task = this._nodeProcessIsReadySource.Task;
|
||||
|
||||
var initializationSucceeded = task
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
if (!initializationSucceeded) {
|
||||
throw new InvalidOperationException("The Node.js process failed to initialize");
|
||||
throw new InvalidOperationException("The Node.js process failed to initialize", task.Exception);
|
||||
}
|
||||
else {
|
||||
Console.WriteLine("Started");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ConnectToInputOutputStreams() {
|
||||
var initializationIsCompleted = false; // TODO: Make this thread-safe? (Interlocked.Exchange etc.)
|
||||
this._nodeProcessIsReadySource = new TaskCompletionSource<bool>();
|
||||
|
||||
|
||||
this._nodeProcess.OutputDataReceived += (sender, evt) => {
|
||||
if (evt.Data == "[Microsoft.AspNet.NodeServices:Listening]" && !initializationIsCompleted) {
|
||||
this._nodeProcessIsReadySource.SetResult(true);
|
||||
@@ -103,18 +111,18 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
this._nodeProcess.BeginOutputReadLine();
|
||||
this._nodeProcess.BeginErrorReadLine();
|
||||
this._nodeProcess.BeginErrorReadLine();
|
||||
}
|
||||
|
||||
|
||||
protected virtual void OnBeforeLaunchProcess() {
|
||||
}
|
||||
|
||||
protected virtual void OnOutputDataReceived(string outputData) {
|
||||
Console.WriteLine("[Node] " + outputData);
|
||||
}
|
||||
|
||||
|
||||
protected virtual void OnErrorDataReceived(string errorData) {
|
||||
Console.WriteLine("[Node] " + errorData);
|
||||
}
|
||||
@@ -124,18 +132,19 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed) {
|
||||
if (disposing) {
|
||||
this._entryPointScript.Dispose();
|
||||
}
|
||||
|
||||
|
||||
if (this._nodeProcess != null && !this._nodeProcess.HasExited) {
|
||||
this._nodeProcess.Kill(); // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup? System.Console.WriteLine("Killed");
|
||||
this._nodeProcess.Kill(); // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup?
|
||||
System.Console.WriteLine("Killed");
|
||||
}
|
||||
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user