Switch to using DI to acquire Node instances. Bump versions to alpha2.

This commit is contained in:
SteveSandersonMS
2015-11-02 13:35:14 -08:00
parent 301657a207
commit de991b9858
23 changed files with 103 additions and 92 deletions

View File

@@ -0,0 +1,24 @@
using Microsoft.Framework.DependencyInjection;
namespace Microsoft.AspNet.NodeServices {
public static class Configuration {
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeHostingModel hostingModel = NodeHostingModel.Http) {
serviceCollection.AddSingleton(typeof(INodeServices), (serviceProvider) => {
return CreateNodeServices(hostingModel);
});
}
private static INodeServices CreateNodeServices(NodeHostingModel hostingModel)
{
switch (hostingModel)
{
case NodeHostingModel.Http:
return new HttpNodeInstance();
case NodeHostingModel.InputOutputStream:
return new InputOutputStreamNodeInstance();
default:
throw new System.ArgumentException("Unknown hosting model: " + hostingModel.ToString());
}
}
}
}

View File

@@ -6,7 +6,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNet.NodeServices {
internal class HttpNodeHost : OutOfProcessNodeRunner {
internal class HttpNodeInstance : OutOfProcessNodeInstance {
private readonly static Regex PortMessageRegex = new Regex(@"^\[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
@@ -15,8 +15,8 @@ namespace Microsoft.AspNet.NodeServices {
private int _portNumber;
public HttpNodeHost(int port = 0)
: base(EmbeddedResourceReader.Read(typeof(HttpNodeHost), "/Content/Node/entrypoint-http.js"), port.ToString())
public HttpNodeInstance(int port = 0)
: base(EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), port.ToString())
{
}

View File

@@ -15,7 +15,7 @@ namespace Microsoft.AspNet.NodeServices {
// Instead of directly using stdin/stdout, we could use either regular sockets (TCP) or use named pipes
// on Windows and domain sockets on Linux / OS X, but either way would need a system for framing the
// requests, associating them with responses, and scheduling use of the comms channel.
internal class InputOutputStreamNodeHost : OutOfProcessNodeRunner
internal class InputOutputStreamNodeInstance : OutOfProcessNodeInstance
{
private SemaphoreSlim _invocationSemaphore = new SemaphoreSlim(1);
private TaskCompletionSource<string> _currentInvocationResult;
@@ -24,8 +24,8 @@ namespace Microsoft.AspNet.NodeServices {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
public InputOutputStreamNodeHost()
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeHost), "/Content/Node/entrypoint-stream.js"))
public InputOutputStreamNodeInstance()
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeInstance), "/Content/Node/entrypoint-stream.js"))
{
}

View File

@@ -1,10 +0,0 @@
using System.Threading.Tasks;
namespace Microsoft.AspNet.NodeServices {
public abstract class NodeHost : System.IDisposable
{
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
public abstract void Dispose();
}
}

View File

@@ -8,7 +8,7 @@ namespace Microsoft.AspNet.NodeServices {
* Class responsible for launching the Node child process, determining when it is ready to accept invocations,
* and finally killing it when the parent process exits. Also it restarts the child process if it dies.
*/
internal abstract class OutOfProcessNodeRunner : NodeHost {
public abstract class OutOfProcessNodeInstance : INodeServices {
private object _childProcessLauncherLock;
private bool disposed;
private StringAsTempFile _entryPointScript;
@@ -18,19 +18,33 @@ namespace Microsoft.AspNet.NodeServices {
protected Process NodeProcess {
get {
// This is only exposed to support the UnreliableStreamNodeHost, which is just to verify that
// 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 OutOfProcessNodeRunner(string entryPointScript, string commandLineArguments = null)
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) {
@@ -104,8 +118,8 @@ namespace Microsoft.AspNet.NodeServices {
protected virtual void OnErrorDataReceived(string errorData) {
Console.WriteLine("[Node] " + errorData);
}
public override void Dispose()
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
@@ -125,8 +139,8 @@ namespace Microsoft.AspNet.NodeServices {
disposed = true;
}
}
~OutOfProcessNodeRunner() {
~OutOfProcessNodeInstance() {
Dispose (false);
}
}

View File

@@ -0,0 +1,10 @@
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNet.NodeServices {
public interface INodeServices : IDisposable {
Task<string> Invoke(string moduleName, params object[] args);
Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args);
}
}

View File

@@ -1,38 +0,0 @@
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNet.NodeServices {
public class NodeInstance : IDisposable {
private readonly NodeHost _nodeHost;
public NodeInstance(NodeHostingModel hostingModel = NodeHostingModel.Http) {
switch (hostingModel) {
case NodeHostingModel.Http:
this._nodeHost = new HttpNodeHost();
break;
case NodeHostingModel.InputOutputStream:
this._nodeHost = new InputOutputStreamNodeHost();
break;
default:
throw new ArgumentException("Unknown hosting model: " + hostingModel.ToString());
}
}
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._nodeHost.Invoke(new NodeInvocationInfo {
ModuleName = moduleName,
ExportedFunctionName = exportedFunctionName,
Args = args
});
}
public void Dispose()
{
this._nodeHost.Dispose();
}
}
}

View File

@@ -1,5 +1,5 @@
{
"version": "1.0.0-alpha1",
"version": "1.0.0-alpha2",
"description": "Microsoft.AspNet.NodeServices",
"authors": [ "Microsoft" ],
"tags": [""],
@@ -8,7 +8,8 @@
"dependencies": {
"System.Net.Http": "4.0.1-beta-23409",
"Newtonsoft.Json": "8.0.1-beta1"
"Newtonsoft.Json": "8.0.1-beta1",
"Microsoft.Framework.DependencyInjection": "1.0.0-beta8"
},
"frameworks": {