/// <summary> /// Factory for deserialization. /// </summary> internal static INodePacket FactoryForDeserialization(ITranslator translator) { TaskHostConfiguration configuration = new TaskHostConfiguration(); configuration.Translate(translator); return(configuration); }
public void ConstructorWithEmptyLocation() { Assert.Throws<InternalErrorException>(() => { TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", String.Empty, null); } ); }
public void ConstructorWithNullName() { Assert.Throws<InternalErrorException>(() => { TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, null, @"c:\my tasks\mytask.dll", null); } ); }
public void TestValidConstructors() { TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", null); TaskHostConfiguration config2 = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", null); IDictionary<string, object> parameters = new Dictionary<string, object>(); TaskHostConfiguration config3 = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", parameters); IDictionary<string, object> parameters2 = new Dictionary<string, object>(); parameters2.Add("Text", "Hello!"); parameters2.Add("MyBoolValue", true); parameters2.Add("MyITaskItem", new TaskItem("ABC")); parameters2.Add("ItemArray", new ITaskItem[] { new TaskItem("DEF"), new TaskItem("GHI"), new TaskItem("JKL") }); TaskHostConfiguration config4 = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", parameters2); }
public void TestTranslationWithITaskItemArrayInDictionary() { IDictionary<string, object> parameters = new Dictionary<string, object>(); parameters.Add("TaskItemArrayValue", new ITaskItem[] { new TaskItem("Foo"), new TaskItem("Baz") }); TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", parameters); ((INodePacketTranslatable)config).Translate(TranslationHelpers.GetWriteTranslator()); INodePacket packet = TaskHostConfiguration.FactoryForDeserialization(TranslationHelpers.GetReadTranslator()); TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration; Assert.Equal(config.TaskName, deserializedConfig.TaskName); Assert.Equal(config.TaskLocation, config.TaskLocation); Assert.NotNull(deserializedConfig.TaskParameters); Assert.Equal(config.TaskParameters.Count, deserializedConfig.TaskParameters.Count); ITaskItem[] itemArray = (ITaskItem[])config.TaskParameters["TaskItemArrayValue"].WrappedParameter; ITaskItem[] deserializedItemArray = (ITaskItem[])deserializedConfig.TaskParameters["TaskItemArrayValue"].WrappedParameter; TaskHostPacketHelpers.AreEqual(itemArray, deserializedItemArray); }
public void TestTranslationWithValueTypesInDictionary() { IDictionary<string, object> parameters = new Dictionary<string, object>(); parameters.Add("Text", "Foo"); parameters.Add("BoolValue", false); TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", parameters); ((INodePacketTranslatable)config).Translate(TranslationHelpers.GetWriteTranslator()); INodePacket packet = TaskHostConfiguration.FactoryForDeserialization(TranslationHelpers.GetReadTranslator()); TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration; Assert.Equal(config.TaskName, deserializedConfig.TaskName); Assert.Equal(config.TaskLocation, config.TaskLocation); Assert.NotNull(deserializedConfig.TaskParameters); Assert.Equal(config.TaskParameters.Count, deserializedConfig.TaskParameters.Count); Assert.Equal(config.TaskParameters["Text"].WrappedParameter, deserializedConfig.TaskParameters["Text"].WrappedParameter); Assert.Equal(config.TaskParameters["BoolValue"].WrappedParameter, deserializedConfig.TaskParameters["BoolValue"].WrappedParameter); }
public void ConstructorWithEmptyName() { TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, String.Empty, @"c:\my tasks\mytask.dll", null); }
/// <summary> /// Instantiates a new MSBuild or MSBuildTaskHost process acting as a child node. /// </summary> internal bool CreateNode(TaskHostContext hostContext, INodePacketFactory factory, INodePacketHandler handler, TaskHostConfiguration configuration) { ErrorUtilities.VerifyThrowArgumentNull(factory, "factory"); ErrorUtilities.VerifyThrow(!_nodeIdToPacketFactory.ContainsKey((int)hostContext), "We should not already have a factory for this context! Did we forget to call DisconnectFromHost somewhere?"); if (AvailableNodes == 0) { ErrorUtilities.ThrowInternalError("All allowable nodes already created ({0}).", _nodeContexts.Count); return(false); } // Start the new process. We pass in a node mode with a node number of 2, to indicate that we // want to start up an MSBuild task host node. string commandLineArgs = " /nologo /nodemode:2 "; string msbuildLocation = GetMSBuildLocationFromHostContext(hostContext); // we couldn't even figure out the location we're trying to launch ... just go ahead and fail. if (msbuildLocation == null) { return(false); } CommunicationsUtilities.Trace("For a host context of {0}, spawning executable from {1}.", hostContext.ToString(), msbuildLocation ?? "MSBuild.exe"); // Make it here. NodeContext context = GetNode ( msbuildLocation, commandLineArgs, (int)hostContext, this, CommunicationsUtilities.GetTaskHostHostHandshake(hostContext), CommunicationsUtilities.GetTaskHostClientHandshake(hostContext), NodeContextTerminated ); if (null != context) { _nodeContexts[hostContext] = context; // Start the asynchronous read. context.BeginAsyncPacketRead(); _activeNodes.Add((int)hostContext); _noNodesActiveEvent.Reset(); return(true); } return(false); }
/// <summary> /// The task has been completed /// </summary> private void CompleteTask() { ErrorUtilities.VerifyThrow(_isTaskExecuting == false, "The task should be done executing before CompleteTask."); if (_nodeEndpoint.LinkStatus == LinkStatus.Active) { TaskHostTaskComplete taskCompletePacketToSend; lock (_taskCompleteLock) { ErrorUtilities.VerifyThrowInternalNull(_taskCompletePacket, "taskCompletePacket"); taskCompletePacketToSend = _taskCompletePacket; _taskCompletePacket = null; } _nodeEndpoint.SendData(taskCompletePacketToSend); } _currentConfiguration = null; // If the task has been canceled, the event will still be set. // If so, now that we've completed the task, we want to shut down // this node -- with no reuse, since we don't know whether the // task we canceled left the node in a good state or not. if (_taskCancelledEvent.WaitOne(0, false)) { _shutdownReason = NodeEngineShutdownReason.BuildComplete; _shutdownEvent.Set(); } }
/// <summary> /// Make sure a node in the requested context exists. /// </summary> internal bool AcquireAndSetUpHost(TaskHostContext hostContext, INodePacketFactory factory, INodePacketHandler handler, TaskHostConfiguration configuration) { NodeContext context = null; bool nodeCreationSucceeded = false; if (!(_nodeContexts.TryGetValue(hostContext, out context))) { nodeCreationSucceeded = CreateNode(hostContext, factory, handler, configuration); } else { // node already exists, so "creation" automatically succeeded nodeCreationSucceeded = true; } if (nodeCreationSucceeded) { context = _nodeContexts[hostContext]; _nodeIdToPacketFactory[(int)hostContext] = factory; _nodeIdToPacketHandler[(int)hostContext] = handler; // Configure the node. context.SendData(configuration); return true; } return false; }
/// <summary> /// Factory for deserialization. /// </summary> internal static INodePacket FactoryForDeserialization(INodePacketTranslator translator) { TaskHostConfiguration configuration = new TaskHostConfiguration(); configuration.Translate(translator); return configuration; }
public void ConstructorWithEmptyLocation() { TaskHostConfiguration config = new TaskHostConfiguration(1, Environment.CurrentDirectory, null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", String.Empty, null); }
public void ConstructorWithNullName() { TaskHostConfiguration config = new TaskHostConfiguration(1, Environment.CurrentDirectory, null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, null, @"c:\my tasks\mytask.dll", null); }
internal bool CreateNode(HandshakeOptions hostContext, INodePacketFactory factory, INodePacketHandler handler, TaskHostConfiguration configuration) { ErrorUtilities.VerifyThrowArgumentNull(factory, nameof(factory)); ErrorUtilities.VerifyThrow(!_nodeIdToPacketFactory.ContainsKey((int)hostContext), "We should not already have a factory for this context! Did we forget to call DisconnectFromHost somewhere?"); if (AvailableNodes <= 0) { ErrorUtilities.ThrowInternalError("All allowable nodes already created ({0}).", _nodeContexts.Count); return(false); } // Start the new process. We pass in a node mode with a node number of 2, to indicate that we // want to start up an MSBuild task host node. string commandLineArgs = $" /nologo /nodemode:2 /nodereuse:{ComponentHost.BuildParameters.EnableNodeReuse} /low:{ComponentHost.BuildParameters.LowPriority} "; string msbuildLocation = GetMSBuildLocationFromHostContext(hostContext); // we couldn't even figure out the location we're trying to launch ... just go ahead and fail. if (msbuildLocation == null) { return(false); } CommunicationsUtilities.Trace("For a host context of {0}, spawning executable from {1}.", hostContext.ToString(), msbuildLocation ?? "MSBuild.exe"); // There is always one task host per host context so we always create just 1 one task host node here. int nodeId = (int)hostContext; IList <NodeContext> nodeContexts = GetNodes( msbuildLocation, commandLineArgs, nodeId, this, new Handshake(hostContext), NodeContextCreated, NodeContextTerminated, 1); return(nodeContexts.Count == 1); }
public void ConstructorWithNullLocation() { TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", null, null); }
/// <summary> /// Configure the task host according to the information received in the /// configuration packet /// </summary> private void HandleTaskHostConfiguration(TaskHostConfiguration taskHostConfiguration) { ErrorUtilities.VerifyThrow(_isTaskExecuting == false, "Why are we getting a TaskHostConfiguration packet while we're still executing a task?"); _currentConfiguration = taskHostConfiguration; // Kick off the task running thread. _taskRunnerThread = new Thread(new ParameterizedThreadStart(RunTask)); _taskRunnerThread.Name = "Task runner for task " + taskHostConfiguration.TaskName; _taskRunnerThread.Start(taskHostConfiguration); }
/// <summary> /// Instantiates a new MSBuild or MSBuildTaskHost process acting as a child node. /// </summary> internal bool CreateNode(TaskHostContext hostContext, INodePacketFactory factory, INodePacketHandler handler, TaskHostConfiguration configuration) { ErrorUtilities.VerifyThrowArgumentNull(factory, "factory"); ErrorUtilities.VerifyThrow(!_nodeIdToPacketFactory.ContainsKey((int)hostContext), "We should not already have a factory for this context! Did we forget to call DisconnectFromHost somewhere?"); if (AvailableNodes == 0) { ErrorUtilities.ThrowInternalError("All allowable nodes already created ({0}).", _nodeContexts.Count); return false; } // Start the new process. We pass in a node mode with a node number of 2, to indicate that we // want to start up an MSBuild task host node. string commandLineArgs = " /nologo /nodemode:2 "; string msbuildLocation = GetMSBuildLocationFromHostContext(hostContext); // we couldn't even figure out the location we're trying to launch ... just go ahead and fail. if (msbuildLocation == null) { return false; } CommunicationsUtilities.Trace("For a host context of {0}, spawning executable from {1}.", hostContext.ToString(), msbuildLocation ?? "MSBuild.exe"); // Make it here. NodeContext context = GetNode ( msbuildLocation, commandLineArgs, (int)hostContext, this, CommunicationsUtilities.GetTaskHostHostHandshake(hostContext), CommunicationsUtilities.GetTaskHostClientHandshake(hostContext), NodeContextTerminated ); if (null != context) { _nodeContexts[hostContext] = context; // Start the asynchronous read. context.BeginAsyncPacketRead(); _activeNodes.Add((int)hostContext); _noNodesActiveEvent.Reset(); return true; } return false; }
/// <summary> /// Make sure a node in the requested context exists. /// </summary> internal bool AcquireAndSetUpHost(TaskHostContext hostContext, INodePacketFactory factory, INodePacketHandler handler, TaskHostConfiguration configuration) { NodeContext context = null; bool nodeCreationSucceeded = false; if (!(_nodeContexts.TryGetValue(hostContext, out context))) { nodeCreationSucceeded = CreateNode(hostContext, factory, handler, configuration); } else { // node already exists, so "creation" automatically succeeded nodeCreationSucceeded = true; } if (nodeCreationSucceeded) { context = _nodeContexts[hostContext]; _nodeIdToPacketFactory[(int)hostContext] = factory; _nodeIdToPacketHandler[(int)hostContext] = handler; // Configure the node. context.SendData(configuration); return(true); } return(false); }
public void TestTranslationWithNullDictionary() { TaskHostConfiguration config = new TaskHostConfiguration(1, Directory.GetCurrentDirectory(), null, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.CurrentUICulture, null, 1, 1, @"c:\my project\myproj.proj", _continueOnErrorDefault, "TaskName", @"c:\MyTasks\MyTask.dll", null); ((INodePacketTranslatable)config).Translate(TranslationHelpers.GetWriteTranslator()); INodePacket packet = TaskHostConfiguration.FactoryForDeserialization(TranslationHelpers.GetReadTranslator()); TaskHostConfiguration deserializedConfig = packet as TaskHostConfiguration; Assert.Equal(config.TaskName, deserializedConfig.TaskName); Assert.Equal(config.TaskLocation, config.TaskLocation); Assert.Null(deserializedConfig.TaskParameters); }
public bool Execute() { // log that we are about to spawn the task host string runtime = _taskHostParameters[XMakeAttributes.runtime]; string architecture = _taskHostParameters[XMakeAttributes.architecture]; _taskLoggingContext.LogComment(MessageImportance.Low, "ExecutingTaskInTaskHost", _taskType.Type.Name, _taskType.Assembly.AssemblyLocation, runtime, architecture); // set up the node lock (_taskHostLock) { _taskHostProvider = (NodeProviderOutOfProcTaskHost)_buildComponentHost.GetComponent(BuildComponentType.OutOfProcTaskHostNodeProvider); ErrorUtilities.VerifyThrowInternalNull(_taskHostProvider, "taskHostProvider"); } TaskHostConfiguration hostConfiguration = new TaskHostConfiguration ( _buildComponentHost.BuildParameters.NodeId, NativeMethodsShared.GetCurrentDirectory(), CommunicationsUtilities.GetEnvironmentVariables(), _buildComponentHost.BuildParameters.Culture, _buildComponentHost.BuildParameters.UICulture, #if FEATURE_APPDOMAIN _appDomainSetup, #endif BuildEngine.LineNumberOfTaskNode, BuildEngine.ColumnNumberOfTaskNode, BuildEngine.ProjectFileOfTaskNode, BuildEngine.ContinueOnError, _taskType.Type.FullName, AssemblyUtilities.GetAssemblyLocation(_taskType.Type.GetTypeInfo().Assembly), _buildComponentHost.BuildParameters.LogTaskInputs, _setParameters, new Dictionary <string, string>(_buildComponentHost.BuildParameters.GlobalProperties), _taskLoggingContext.GetWarningsAsErrors(), _taskLoggingContext.GetWarningsAsMessages() ); try { lock (_taskHostLock) { _requiredContext = CommunicationsUtilities.GetHandshakeOptions(taskHost: true, taskHostParameters: _taskHostParameters); _connectedToTaskHost = _taskHostProvider.AcquireAndSetUpHost(_requiredContext, this, this, hostConfiguration); } if (_connectedToTaskHost) { try { bool taskFinished = false; while (!taskFinished) { _packetReceivedEvent.WaitOne(); INodePacket packet = null; // Handle the packet that's coming in while (_receivedPackets.TryDequeue(out packet)) { if (packet != null) { HandlePacket(packet, out taskFinished); } } } } finally { lock (_taskHostLock) { _taskHostProvider.DisconnectFromHost(_requiredContext); _connectedToTaskHost = false; } } } else { LogErrorUnableToCreateTaskHost(_requiredContext, runtime, architecture, null); } } catch (BuildAbortedException) { LogErrorUnableToCreateTaskHost(_requiredContext, runtime, architecture, null); } catch (NodeFailedToLaunchException e) { LogErrorUnableToCreateTaskHost(_requiredContext, runtime, architecture, e); } return(_taskExecutionSucceeded); }
/// <summary> /// Executes the task. /// </summary> public bool Execute() { // log that we are about to spawn the task host string runtime = _taskHostParameters[XMakeAttributes.runtime]; string architecture = _taskHostParameters[XMakeAttributes.architecture]; _taskLoggingContext.LogComment(MessageImportance.Low, "ExecutingTaskInTaskHost", _taskType.Type.Name, _taskType.Assembly.AssemblyLocation, runtime, architecture); // set up the node lock (_taskHostLock) { _taskHostProvider = (NodeProviderOutOfProcTaskHost)_buildComponentHost.GetComponent(BuildComponentType.OutOfProcTaskHostNodeProvider); ErrorUtilities.VerifyThrowInternalNull(_taskHostProvider, "taskHostProvider"); } TaskHostConfiguration hostConfiguration = new TaskHostConfiguration ( _buildComponentHost.BuildParameters.NodeId, NativeMethodsShared.GetCurrentDirectory(), CommunicationsUtilities.GetEnvironmentVariables(), _buildComponentHost.BuildParameters.Culture, _buildComponentHost.BuildParameters.UICulture, _appDomainSetup, BuildEngine.LineNumberOfTaskNode, BuildEngine.ColumnNumberOfTaskNode, BuildEngine.ProjectFileOfTaskNode, BuildEngine.ContinueOnError, _taskType.Type.FullName, _taskType.Type.Assembly.Location, _setParameters ); try { lock (_taskHostLock) { _requiredContext = CommunicationsUtilities.GetTaskHostContext(_taskHostParameters); _connectedToTaskHost = _taskHostProvider.AcquireAndSetUpHost(_requiredContext, this, this, hostConfiguration); } if (_connectedToTaskHost) { try { bool taskFinished = false; while (!taskFinished) { _packetReceivedEvent.WaitOne(); INodePacket packet = null; int packetCount = _receivedPackets.Count; // Handle the packet that's coming in while (packetCount > 0) { lock (_receivedPackets) { if (_receivedPackets.Count > 0) { packet = _receivedPackets.Dequeue(); } else { break; } } if (packet != null) { HandlePacket(packet, out taskFinished); } } } } finally { lock (_taskHostLock) { _taskHostProvider.DisconnectFromHost(_requiredContext); _connectedToTaskHost = false; } } } else { LogErrorUnableToCreateTaskHost(_requiredContext, runtime, architecture, null); } } catch (BuildAbortedException) { LogErrorUnableToCreateTaskHost(_requiredContext, runtime, architecture, null); } catch (NodeFailedToLaunchException e) { LogErrorUnableToCreateTaskHost(_requiredContext, runtime, architecture, e); } return _taskExecutionSucceeded; }