/// <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(); } }
public void TestTranslationWithEmptyDictionary() { TaskHostTaskComplete complete = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.Success, new Dictionary <string, object>()), null); ((INodePacketTranslatable)complete).Translate(TranslationHelpers.GetWriteTranslator()); INodePacket packet = TaskHostTaskComplete.FactoryForDeserialization(TranslationHelpers.GetReadTranslator()); TaskHostTaskComplete deserializedComplete = packet as TaskHostTaskComplete; Assert.Equal(complete.TaskResult, deserializedComplete.TaskResult); Assert.NotNull(deserializedComplete.TaskOutputParameters); Assert.Equal(complete.TaskOutputParameters.Count, deserializedComplete.TaskOutputParameters.Count); }
/// <summary> /// Helper method for testing invalid constructors /// </summary> private void AssertInvalidConstructorThrows(Type expectedExceptionType, TaskCompleteType taskResult, Exception taskException, string taskExceptionMessage, string[] taskExceptionMessageArgs, IDictionary <string, object> taskOutputParameters, IDictionary <string, string> buildProcessEnvironment) { bool exceptionCaught = false; try { TaskHostTaskComplete complete = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(taskResult, taskOutputParameters, taskException, taskExceptionMessage, taskExceptionMessageArgs), buildProcessEnvironment); } catch (Exception e) { exceptionCaught = true; Assert.IsAssignableFrom(expectedExceptionType, e); // "Wrong exception was thrown!" } Assert.True(exceptionCaught); // "No exception was caught when one was expected!" }
public void TestTranslationWithITaskItemInDictionary() { IDictionary <string, object> parameters = new Dictionary <string, object>(); parameters.Add("TaskItemValue", new TaskItem("Foo")); TaskHostTaskComplete complete = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.Success, parameters), null); ((INodePacketTranslatable)complete).Translate(TranslationHelpers.GetWriteTranslator()); INodePacket packet = TaskHostTaskComplete.FactoryForDeserialization(TranslationHelpers.GetReadTranslator()); TaskHostTaskComplete deserializedComplete = packet as TaskHostTaskComplete; Assert.Equal(complete.TaskResult, deserializedComplete.TaskResult); Assert.NotNull(deserializedComplete.TaskOutputParameters); Assert.Equal(complete.TaskOutputParameters.Count, deserializedComplete.TaskOutputParameters.Count); TaskHostPacketHelpers.AreEqual((ITaskItem)complete.TaskOutputParameters["TaskItemValue"].WrappedParameter, (ITaskItem)deserializedComplete.TaskOutputParameters["TaskItemValue"].WrappedParameter); }
public void TestConstructors() { TaskHostTaskComplete complete = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.Success), null); TaskHostTaskComplete complete2 = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.Failure), null); TaskHostTaskComplete complete3 = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.CrashedDuringInitialization, new ArgumentOutOfRangeException()), null); TaskHostTaskComplete complete4 = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.CrashedDuringExecution, new ArgumentNullException()), null); IDictionary <string, object> parameters = new Dictionary <string, object>(); TaskHostTaskComplete complete5 = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.Success, parameters), null); 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") }); TaskHostTaskComplete complete6 = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.Success, parameters2), null); }
/// <summary> /// Task runner method /// </summary> private void RunTask(object state) { _isTaskExecuting = true; OutOfProcTaskHostTaskResult taskResult = null; TaskHostConfiguration taskConfiguration = state as TaskHostConfiguration; IDictionary <string, TaskParameter> taskParams = taskConfiguration.TaskParameters; // We only really know the values of these variables for sure once we see what we received from our parent // environment -- otherwise if this was a completely new build, we could lose out on expected environment // variables. _debugCommunications = taskConfiguration.BuildProcessEnvironment.ContainsValueAndIsEqual("MSBUILDDEBUGCOMM", "1", StringComparison.OrdinalIgnoreCase); _updateEnvironment = !taskConfiguration.BuildProcessEnvironment.ContainsValueAndIsEqual("MSBuildTaskHostDoNotUpdateEnvironment", "1", StringComparison.OrdinalIgnoreCase); _updateEnvironmentAndLog = taskConfiguration.BuildProcessEnvironment.ContainsValueAndIsEqual("MSBuildTaskHostUpdateEnvironmentAndLog", "1", StringComparison.OrdinalIgnoreCase); try { // Change to the startup directory NativeMethodsShared.SetCurrentDirectory(taskConfiguration.StartupDirectory); if (_updateEnvironment) { InitializeMismatchedEnvironmentTable(taskConfiguration.BuildProcessEnvironment); } // Now set the new environment SetTaskHostEnvironment(taskConfiguration.BuildProcessEnvironment); // Set culture Thread.CurrentThread.CurrentCulture = taskConfiguration.Culture; Thread.CurrentThread.CurrentUICulture = taskConfiguration.UICulture; string taskName = taskConfiguration.TaskName; string taskLocation = taskConfiguration.TaskLocation; // We will not create an appdomain now because of a bug // As a fix, we will create the class directly without wrapping it in a domain _taskWrapper = new OutOfProcTaskAppDomainWrapper(); taskResult = _taskWrapper.ExecuteTask ( this as IBuildEngine, taskName, taskLocation, taskConfiguration.ProjectFileOfTask, taskConfiguration.LineNumberOfTask, taskConfiguration.ColumnNumberOfTask, taskConfiguration.AppDomainSetup, taskParams ); } catch (Exception e) { if (e is ThreadAbortException) { // This thread was aborted as part of Cancellation, we will return a failure task result taskResult = new OutOfProcTaskHostTaskResult(TaskCompleteType.Failure); } else if (ExceptionHandling.IsCriticalException(e)) { throw; } else { taskResult = new OutOfProcTaskHostTaskResult(TaskCompleteType.CrashedDuringExecution, e); } } finally { try { _isTaskExecuting = false; IDictionary <string, string> currentEnvironment = CommunicationsUtilities.GetEnvironmentVariables(); currentEnvironment = UpdateEnvironmentForMainNode(currentEnvironment); if (taskResult == null) { taskResult = new OutOfProcTaskHostTaskResult(TaskCompleteType.Failure); } lock (_taskCompleteLock) { _taskCompletePacket = new TaskHostTaskComplete ( taskResult, currentEnvironment ); } foreach (TaskParameter param in taskParams.Values) { // Tell remoting to forget connections to the parameter RemotingServices.Disconnect(param); } // Restore the original clean environment CommunicationsUtilities.SetEnvironment(_savedEnvironment); } catch (Exception e) { lock (_taskCompleteLock) { // Create a minimal taskCompletePacket to carry the exception so that the TaskHostTask does not hang while waiting _taskCompletePacket = new TaskHostTaskComplete(new OutOfProcTaskHostTaskResult(TaskCompleteType.CrashedAfterExecution, e), null); } } finally { // Call CleanupTask to unload any domains and other necessary cleanup in the taskWrapper _taskWrapper.CleanupTask(); // The task has now fully completed executing _taskCompleteEvent.Set(); } } }