Skip to content

Commit 740b1a3

Browse files
Taking some changes from pull/1
2 parents 717fb37 + a4c4e20 commit 740b1a3

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

Microsoft.AspNet.NodeServices/HostingModels/OutOfProcessNodeInstance.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public abstract class OutOfProcessNodeInstance : INodeServices {
1616
private string _commandLineArguments;
1717
private Process _nodeProcess;
1818
private TaskCompletionSource<bool> _nodeProcessIsReadySource;
19-
19+
2020
protected Process NodeProcess {
2121
get {
2222
// This is only exposed to support the unreliable OutOfProcessNodeRunner, which is just to verify that
@@ -46,7 +46,7 @@ public async Task<T> InvokeExport<T>(string moduleName, string exportedFunctionN
4646
Args = args
4747
});
4848
}
49-
49+
5050
protected async Task EnsureReady() {
5151
lock (this._childProcessLauncherLock) {
5252
if (this._nodeProcess == null || this._nodeProcess.HasExited) {
@@ -58,36 +58,38 @@ protected async Task EnsureReady() {
5858
RedirectStandardError = true,
5959
WorkingDirectory = this._projectPath
6060
};
61-
61+
6262
// Append projectPath to NODE_PATH so it can locate node_modules
6363
var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
6464
if (existingNodePath != string.Empty) {
6565
existingNodePath += ":";
6666
}
67-
67+
6868
var nodePathValue = existingNodePath + Path.Combine(this._projectPath, "node_modules");
6969
#if DNX451
7070
startInfo.EnvironmentVariables.Add("NODE_PATH", nodePathValue);
7171
#else
7272
startInfo.Environment.Add("NODE_PATH", nodePathValue);
7373
#endif
74-
74+
7575
this.OnBeforeLaunchProcess();
7676
this._nodeProcess = Process.Start(startInfo);
7777
this.ConnectToInputOutputStreams();
7878
}
7979
}
80-
81-
var initializationSucceeded = await this._nodeProcessIsReadySource.Task;
80+
81+
var task = this._nodeProcessIsReadySource.Task;
82+
var initializationSucceeded = await task;
83+
8284
if (!initializationSucceeded) {
83-
throw new InvalidOperationException("The Node.js process failed to initialize");
85+
throw new InvalidOperationException("The Node.js process failed to initialize", task.Exception);
8486
}
8587
}
86-
88+
8789
private void ConnectToInputOutputStreams() {
8890
var initializationIsCompleted = false; // TODO: Make this thread-safe? (Interlocked.Exchange etc.)
8991
this._nodeProcessIsReadySource = new TaskCompletionSource<bool>();
90-
92+
9193
this._nodeProcess.OutputDataReceived += (sender, evt) => {
9294
if (evt.Data == "[Microsoft.AspNet.NodeServices:Listening]" && !initializationIsCompleted) {
9395
this._nodeProcessIsReadySource.SetResult(true);
@@ -106,18 +108,18 @@ private void ConnectToInputOutputStreams() {
106108
}
107109
}
108110
};
109-
111+
110112
this._nodeProcess.BeginOutputReadLine();
111-
this._nodeProcess.BeginErrorReadLine();
113+
this._nodeProcess.BeginErrorReadLine();
112114
}
113-
115+
114116
protected virtual void OnBeforeLaunchProcess() {
115117
}
116118

117119
protected virtual void OnOutputDataReceived(string outputData) {
118120
Console.WriteLine("[Node] " + outputData);
119121
}
120-
122+
121123
protected virtual void OnErrorDataReceived(string errorData) {
122124
Console.WriteLine("[Node] " + errorData);
123125
}
@@ -127,18 +129,18 @@ public void Dispose()
127129
Dispose(true);
128130
GC.SuppressFinalize(this);
129131
}
130-
132+
131133
protected virtual void Dispose(bool disposing)
132134
{
133135
if (!disposed) {
134136
if (disposing) {
135137
this._entryPointScript.Dispose();
136138
}
137-
139+
138140
if (this._nodeProcess != null && !this._nodeProcess.HasExited) {
139-
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");
141+
this._nodeProcess.Kill(); // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup?
140142
}
141-
143+
142144
disposed = true;
143145
}
144146
}

0 commit comments

Comments
 (0)