Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Restored Killed console message #1

Merged
merged 1 commit into from
Nov 17, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@ public abstract class OutOfProcessNodeInstance : INodeServices {
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
// other hosting/transport mechanisms are possible. This shouldn't really be exposed.
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,
ExportedFunctionName = exportedFunctionName,
Args = args
});
}

protected async Task EnsureReady() {
lock (this._childProcessLauncherLock) {
if (this._nodeProcess == null || this._nodeProcess.HasExited) {
Expand All @@ -55,36 +55,44 @@ protected async Task EnsureReady() {
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);
Expand All @@ -103,18 +111,18 @@ private void ConnectToInputOutputStreams() {
}
}
};

this._nodeProcess.BeginOutputReadLine();
this._nodeProcess.BeginErrorReadLine();
this._nodeProcess.BeginErrorReadLine();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only thing I hate about VS code....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean, the whitespaces? Arrrgghhh...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, whitespaces in empty lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I get rid of them, will you merge? :-)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was just a random comment, sorry. I only saw a demo of this thing during the MVP summit and it was very cool so I got interested. @SteveSanderson needs to look at it I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, he's right here with me! :-)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug him! 😄


protected virtual void OnBeforeLaunchProcess() {
}

protected virtual void OnOutputDataReceived(string outputData) {
Console.WriteLine("[Node] " + outputData);
}

protected virtual void OnErrorDataReceived(string errorData) {
Console.WriteLine("[Node] " + errorData);
}
Expand All @@ -124,18 +132,19 @@ public void Dispose()
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;
}
}
Expand Down