public void InvalidNodeIdOnNodeLoggingContext() { Assert.Throws <InternalErrorException>(() => { _ = new NodeLoggingContext(new MockLoggingService(), -2, true); } ); }
public void BuildRequest(NodeLoggingContext context, BuildRequestEntry entry) { Assert.Null(_builderThread); // "Received BuildRequest while one was in progress" _continueEvent = new AutoResetEvent(false); _cancelEvent = new AutoResetEvent(false); _entry = entry; entry.Continue(); _builderThread = new Thread(BuilderThreadProc); _builderThread.Start(); }
public void HasLoggedErrors() { NodeLoggingContext context = new NodeLoggingContext(new MockLoggingService(_output.WriteLine), 1, true); context.HasLoggedErrors.ShouldBeFalse(); context.LogCommentFromText(Framework.MessageImportance.High, "Test message"); context.HasLoggedErrors.ShouldBeFalse(); context.LogWarningFromText(null, null, null, null, "Test warning"); context.HasLoggedErrors.ShouldBeFalse(); context.LogErrorFromText(null, null, null, null, "Test error"); context.HasLoggedErrors.ShouldBeTrue(); }
public void CreateValidNodeLoggingContexts() { NodeLoggingContext context = new NodeLoggingContext(new MockLoggingService(_output.WriteLine), 1, true); context.IsInProcNode.ShouldBeTrue(); context.IsValid.ShouldBeTrue(); context.LogBuildFinished(true); context.IsValid.ShouldBeFalse(); context.BuildEventContext.NodeId.ShouldBe(1); NodeLoggingContext context2 = new NodeLoggingContext(new MockLoggingService(_output.WriteLine), 2, false); context2.IsInProcNode.ShouldBeFalse(); context2.IsValid.ShouldBeTrue(); context2.LogBuildFinished(true); context2.IsValid.ShouldBeFalse(); context2.BuildEventContext.NodeId.ShouldBe(2); }
public void CreateValidNodeLoggingContexts() { NodeLoggingContext context = new NodeLoggingContext(new MockLoggingService(), 1, true); Assert.Equal(true, context.IsInProcNode); Assert.True(context.IsValid); context.LogBuildFinished(true); Assert.False(context.IsValid); Assert.Equal(1, context.BuildEventContext.NodeId); NodeLoggingContext context2 = new NodeLoggingContext(new MockLoggingService(), 2, false); Assert.Equal(false, context2.IsInProcNode); Assert.True(context2.IsValid); context2.LogBuildFinished(true); Assert.False(context2.IsValid); Assert.Equal(2, context2.BuildEventContext.NodeId); }
private void HandleNodeConfiguration(NodeConfiguration configuration) { // Grab the system parameters. _buildParameters = configuration.BuildParameters; _buildParameters.ProjectRootElementCache = s_projectRootElementCacheBase; // Snapshot the current environment _savedEnvironment = CommunicationsUtilities.GetEnvironmentVariables(); // Change to the startup directory try { NativeMethodsShared.SetCurrentDirectory(BuildParameters.StartupDirectory); } catch (DirectoryNotFoundException) { // Somehow the startup directory vanished. This can happen if build was started from a USB Key and it was removed. NativeMethodsShared.SetCurrentDirectory(BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory); } // Replicate the environment. First, unset any environment variables set by the previous configuration. if (_currentConfiguration != null) { foreach (string key in _currentConfiguration.BuildParameters.BuildProcessEnvironment.Keys) { Environment.SetEnvironmentVariable(key, null); } } // Now set the new environment foreach (KeyValuePair <string, string> environmentPair in _buildParameters.BuildProcessEnvironment) { Environment.SetEnvironmentVariable(environmentPair.Key, environmentPair.Value); } // We want to make sure the global project collection has the toolsets which were defined on the parent // so that any custom toolsets defined can be picked up by tasks who may use the global project collection but are // executed on the child node. ICollection <Toolset> parentToolSets = _buildParameters.ToolsetProvider.Toolsets; if (parentToolSets != null) { ProjectCollection.GlobalProjectCollection.RemoveAllToolsets(); foreach (Toolset toolSet in parentToolSets) { ProjectCollection.GlobalProjectCollection.AddToolset(toolSet); } } // Set the culture. CultureInfo.CurrentCulture = _buildParameters.Culture; CultureInfo.CurrentUICulture = _buildParameters.UICulture; // Get the node ID. _buildParameters.NodeId = configuration.NodeId; _buildParameters.IsOutOfProc = true; #if FEATURE_APPDOMAIN // And the AppDomainSetup _buildParameters.AppDomainSetup = configuration.AppDomainSetup; #endif // Set up the logging service. LoggingServiceFactory loggingServiceFactory = new LoggingServiceFactory(LoggerMode.Asynchronous, configuration.NodeId); _componentFactories.ReplaceFactory(BuildComponentType.LoggingService, loggingServiceFactory.CreateInstance); _loggingService = _componentFactories.GetComponent(BuildComponentType.LoggingService) as ILoggingService; BuildEventArgTransportSink sink = new BuildEventArgTransportSink(SendPacket); _shutdownException = null; if (configuration.LoggingNodeConfiguration.IncludeEvaluationMetaprojects) { _loggingService.IncludeEvaluationMetaprojects = true; } if (configuration.LoggingNodeConfiguration.IncludeEvaluationProfiles) { _loggingService.IncludeEvaluationProfile = true; } if (configuration.LoggingNodeConfiguration.IncludeTaskInputs) { _loggingService.IncludeTaskInputs = true; } if (configuration.LoggingNodeConfiguration.IncludeEvaluationPropertiesAndItems) { _loggingService.IncludeEvaluationPropertiesAndItems = true; } try { // If there are no node loggers to initialize dont do anything if (configuration.LoggerDescriptions?.Length > 0) { _loggingService.InitializeNodeLoggers(configuration.LoggerDescriptions, sink, configuration.NodeId); } } catch (Exception ex) when(!ExceptionHandling.IsCriticalException(ex)) { OnEngineException(ex); } _loggingService.OnLoggingThreadException += OnLoggingThreadException; string forwardPropertiesFromChild = Environment.GetEnvironmentVariable("MSBUILDFORWARDPROPERTIESFROMCHILD"); string[] propertyListToSerialize = null; // Get a list of properties which should be serialized if (!String.IsNullOrEmpty(forwardPropertiesFromChild)) { propertyListToSerialize = forwardPropertiesFromChild.Split(MSBuildConstants.SemicolonChar, StringSplitOptions.RemoveEmptyEntries); } _loggingService.PropertiesToSerialize = propertyListToSerialize; _loggingService.RunningOnRemoteNode = true; string forwardAllProperties = Environment.GetEnvironmentVariable("MSBUILDFORWARDALLPROPERTIESFROMCHILD"); if (String.Equals(forwardAllProperties, "1", StringComparison.OrdinalIgnoreCase) || _buildParameters.LogInitialPropertiesAndItems) { _loggingService.SerializeAllProperties = true; } else { _loggingService.SerializeAllProperties = false; } // Now prep the buildRequestEngine for the build. _loggingContext = new NodeLoggingContext(_loggingService, configuration.NodeId, false /* inProcNode */); if (_shutdownException != null) { HandleShutdown(out Exception exception); throw exception; } _buildRequestEngine.InitializeForBuild(_loggingContext); // Finally store off this configuration packet. _currentConfiguration = configuration; }
private NodeEngineShutdownReason HandleShutdown(out Exception exception) { CommunicationsUtilities.Trace("Shutting down with reason: {0}, and exception: {1}.", _shutdownReason, _shutdownException); // Clean up the engine if (_buildRequestEngine != null && _buildRequestEngine.Status != BuildRequestEngineStatus.Uninitialized) { _buildRequestEngine.CleanupForBuild(); if (_shutdownReason == NodeEngineShutdownReason.BuildCompleteReuse) { ((IBuildComponent)_buildRequestEngine).ShutdownComponent(); } } // Signal the SDK resolver service to shutdown ((IBuildComponent)_sdkResolverService).ShutdownComponent(); // Dispose of any build registered objects IRegisteredTaskObjectCache objectCache = (IRegisteredTaskObjectCache)(_componentFactories.GetComponent(BuildComponentType.RegisteredTaskObjectCache)); objectCache.DisposeCacheObjects(RegisteredTaskObjectLifetime.Build); if (_shutdownReason != NodeEngineShutdownReason.BuildCompleteReuse) { // Dispose of any node registered objects. ((IBuildComponent)objectCache).ShutdownComponent(); } // Shutdown any Out Of Proc Nodes Created _taskHostNodeManager.ShutdownConnectedNodes(_shutdownReason == NodeEngineShutdownReason.BuildCompleteReuse); // On Windows, a process holds a handle to the current directory, // so reset it away from a user-requested folder that may get deleted. NativeMethodsShared.SetCurrentDirectory(BuildEnvironmentHelper.Instance.CurrentMSBuildToolsDirectory); // Restore the original environment. // If the node was never configured, this will be null. if (_savedEnvironment != null) { foreach (KeyValuePair <string, string> entry in CommunicationsUtilities.GetEnvironmentVariables()) { if (!_savedEnvironment.ContainsKey(entry.Key)) { Environment.SetEnvironmentVariable(entry.Key, null); } } foreach (KeyValuePair <string, string> entry in _savedEnvironment) { Environment.SetEnvironmentVariable(entry.Key, entry.Value); } } try { // Shut down logging, which will cause all queued logging messages to be sent. if (_loggingContext != null && _loggingService != null) { _loggingContext.LogBuildFinished(true); ((IBuildComponent)_loggingService).ShutdownComponent(); } } finally { // Shut down logging, which will cause all queued logging messages to be sent. if (_loggingContext != null && _loggingService != null) { _loggingContext.LoggingService.OnLoggingThreadException -= OnLoggingThreadException; _loggingContext = null; } exception = _shutdownException; if (_nodeEndpoint.LinkStatus == LinkStatus.Active) { // Notify the BuildManager that we are done. _nodeEndpoint.SendData(new NodeShutdown(_shutdownReason == NodeEngineShutdownReason.Error ? NodeShutdownReason.Error : NodeShutdownReason.Requested, exception)); // Flush all packets to the pipe and close it down. This blocks until the shutdown is complete. _nodeEndpoint.OnLinkStatusChanged -= OnLinkStatusChanged; } _nodeEndpoint.Disconnect(); CleanupCaches(); } CommunicationsUtilities.Trace("Shut down complete."); return(_shutdownReason); }
/// <summary> /// Not Implemented /// </summary> public void BuildRequest(NodeLoggingContext nodeLoggingContext, BuildRequestEntry entry) { throw new NotImplementedException(); }
/// <summary> /// Perform necessary actions to shut down the node. /// </summary> private NodeEngineShutdownReason HandleShutdown(out Exception exception) { if (_debugCommunications) { using (StreamWriter writer = File.CreateText(String.Format(CultureInfo.CurrentCulture, Path.Combine(Path.GetTempPath(), @"MSBuild_NodeShutdown_{0}.txt"), Process.GetCurrentProcess().Id))) { writer.WriteLine("Node shutting down with reason {0} and exception: {1}", _shutdownReason, _shutdownException); } } // Clean up the engine if (null != _buildRequestEngine && _buildRequestEngine.Status != BuildRequestEngineStatus.Uninitialized) { _buildRequestEngine.CleanupForBuild(); if (_shutdownReason == NodeEngineShutdownReason.BuildCompleteReuse) { ((IBuildComponent)_buildRequestEngine).ShutdownComponent(); } } // Dispose of any build registered objects IRegisteredTaskObjectCache objectCache = (IRegisteredTaskObjectCache)(_componentFactories.GetComponent(BuildComponentType.RegisteredTaskObjectCache)); objectCache.DisposeCacheObjects(RegisteredTaskObjectLifetime.Build); if (_shutdownReason != NodeEngineShutdownReason.BuildCompleteReuse) { // Dispose of any node registered objects. ((IBuildComponent)objectCache).ShutdownComponent(); } // Shutdown any Out Of Proc Nodes Created _taskHostNodeManager.ShutdownConnectedNodes(_shutdownReason == NodeEngineShutdownReason.BuildCompleteReuse); // Restore the original current directory. NativeMethodsShared.SetCurrentDirectory(Environment.SystemDirectory); // Restore the original environment. // If the node was never configured, this will be null. if (_savedEnvironment != null) { foreach (KeyValuePair <string, string> entry in CommunicationsUtilities.GetEnvironmentVariables()) { if (!_savedEnvironment.ContainsKey(entry.Key)) { Environment.SetEnvironmentVariable(entry.Key, null); } } foreach (KeyValuePair <string, string> entry in _savedEnvironment) { Environment.SetEnvironmentVariable(entry.Key, entry.Value); } } try { // Shut down logging, which will cause all queued logging messages to be sent. if (null != _loggingContext && null != _loggingService) { _loggingContext.LogBuildFinished(true); ((IBuildComponent)_loggingService).ShutdownComponent(); } } finally { // Shut down logging, which will cause all queued logging messages to be sent. if (null != _loggingContext && null != _loggingService) { _loggingContext.LoggingService.OnLoggingThreadException -= new LoggingExceptionDelegate(OnLoggingThreadException); _loggingContext = null; } exception = _shutdownException; if (_nodeEndpoint.LinkStatus == LinkStatus.Active) { // Notify the BuildManager that we are done. _nodeEndpoint.SendData(new NodeShutdown(_shutdownReason == NodeEngineShutdownReason.Error ? NodeShutdownReason.Error : NodeShutdownReason.Requested, exception)); // Flush all packets to the pipe and close it down. This blocks until the shutdown is complete. _nodeEndpoint.OnLinkStatusChanged -= new LinkStatusChangedDelegate(OnLinkStatusChanged); } _nodeEndpoint.Disconnect(); CleanupCaches(); } return(_shutdownReason); }
public void InvalidNodeIdOnNodeLoggingContext() { NodeLoggingContext context = new NodeLoggingContext(new MockLoggingService(), -2, true); }