ProjectTrigger.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Triggers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Triggers » ProjectTrigger.cs
using System;
using System.Net.Sockets;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Core.Util;
using ThoughtWorks.CruiseControl.Remote;

namespace ThoughtWorks.CruiseControl.Core.Triggers{
    /// <summary>
    /// <para>
    /// The Project Trigger is used to trigger a build when the specified dependent project has completed its build. This trigger can help you split your 
    /// build process across projects and servers. For example, you could have a CCNet project that will trigger the regression test suite once the main 
    /// development build has completed successfully. This dependent build could be running on either a local or a remote CCNet server.
    /// </para>
    /// <para>
    /// The Project Trigger works by using .NET remoting to poll the status of the dependent project. Whenever it detects that the dependent project has 
    /// completed a build, the Project Trigger will fire. The Project Trigger can be configured to fire when the dependent project build succeeded, failed 
    /// or threw an exception. In order to avoid hammering the remote project through polling, the Project Trigger is composed of an <link>Interval Trigger
    /// </link>that will set a polling interval to 5 seconds. This inner trigger can be adjusted through changing the configuration.
    /// </para>
    /// <para type="info">
    /// Like all triggers, the projectTrigger must be enclosed within a triggers element in the appropriate <link>Project Configuration Block</link>.
    /// </para>
    /// </summary>
    /// <title>Project Trigger</title>
    /// <version>1.0</version>
    /// <remarks>
    /// <para type="warning">
    /// There is currently a limitation in the Project Trigger in that it will always trigger a build when the inner trigger fires (at the end of the first 
    /// interval for an Interval Trigger). This is because the Project Trigger has no way to save its state from a previous server run. So the last time that 
    /// the build was triggered is not retrievable when the server restarts.
    /// </para>
    /// </remarks>
    /// <example>
    /// <code title="Minimalist example">
    /// &lt;projectTrigger project="Core" /&gt;
    /// </code>
    /// <code title="Full example">
    /// &lt;projectTrigger serverUri="tcp://server:21234/CruiseManager.rem" project="Server"&gt;
    /// &lt;triggerStatus&gt;Success&lt;/triggerStatus&gt;
    /// &lt;innerTrigger type="intervalTrigger" seconds="30" buildCondition="ForceBuild"/&gt;
    /// &lt;/projectTrigger&gt;
    /// </code>
    /// </example>
    [ReflectorType("projectTrigger")]
  public class ProjectTrigger : ITrigger
  {
    public const string DefaultServerUri = RemoteCruiseServer.DefaultManagerUri;
    private const int DefaultIntervalSeconds = 5;

    private readonly ICruiseManagerFactory managerFactory;
    private ProjectStatus lastStatus;
    private ProjectStatus currentStatus;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectTrigger"/> class.
        /// </summary>
    public ProjectTrigger() : this(new RemoteCruiseManagerFactory())
    {}

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectTrigger"/> class.
        /// </summary>
        /// <param name="managerFactory">The manager factory.</param>
    public ProjectTrigger(ICruiseManagerFactory managerFactory)
    {
      this.managerFactory = managerFactory;
    }

        /// <summary>
        /// The name of the dependent project to trigger a build from.
        /// </summary>
        /// <version>1.0</version>
        /// <default>n/a</default>
    [ReflectorProperty("project")]
    public string Project;

        /// <summary>
        /// The URI for the CCNet server containing the dependent project.
        /// </summary>
        /// <version>1.0</version>
        /// <default>tcp://localhost:21234/CruiseManager.rem</default>
    [ReflectorProperty("serverUri", Required=false)]
    public string ServerUri = DefaultServerUri;

        /// <summary>
        /// The status of the dependent project that will be used to trigger the build. For example, if this value is set to Success then a build will 
        /// be triggered when the dependent project completes a successful build.
        /// </summary>
        /// <version>1.0</version>
        /// <default>Success</default>
    [ReflectorProperty("triggerStatus", Required=false)]
    public IntegrationStatus TriggerStatus = IntegrationStatus.Success;

        /// <summary>
        /// The trigger used to modulate the polling interval for the ProjectTrigger. By default, this is set to a ForceBuild IntervalTrigger that will cause 
        /// the trigger to check the status of the dependent project every 5 seconds.
        /// </summary>
        /// <version>1.0</version>
        /// <default>5 second ForceBuild intervalTrigger</default>
    [ReflectorProperty("innerTrigger", InstanceTypeKey="type", Required=false)]
    public ITrigger InnerTrigger = NewIntervalTrigger();

        /// <summary>
        /// Whether to trigger on the first time or not.
        /// </summary>
        /// <version>1.0</version>
        /// <default>false</default>
    [ReflectorProperty("triggerFirstTime", Required = false)]
    public bool TriggerFirstTime = false;

    public void IntegrationCompleted()
    {
      lastStatus = currentStatus;
      InnerTrigger.IntegrationCompleted();
    }

    private ProjectStatus GetCurrentProjectStatus()
    {
      Log.Debug("Retrieving ProjectStatus from server: " + ServerUri);
      ProjectStatus[] currentStatuses = managerFactory.GetCruiseManager(ServerUri).GetProjectStatus();
      foreach (ProjectStatus projectStatus in currentStatuses)
      {
        if (projectStatus.Name == Project)
        {
                    Log.Debug("Found status for dependent project {0} is {1}",projectStatus.Name,projectStatus.BuildStatus);
          return projectStatus;
        }
      }
      throw new NoSuchProjectException(Project);
    }

    public DateTime NextBuild
    {
      get
      {
                return InnerTrigger.NextBuild;
      }
    }

    public IntegrationRequest Fire()
    {
      IntegrationRequest request = InnerTrigger.Fire();
      if (request == null) return null;
      InnerTrigger.IntegrationCompleted(); // reset inner trigger (timer)

            try
            {
                currentStatus = GetCurrentProjectStatus();
                if (lastStatus == null)
                {
                    lastStatus = currentStatus;
                    if (TriggerFirstTime && currentStatus.BuildStatus == TriggerStatus)
                    {
                        return request;
                    }
                    return null;
                }
                if (currentStatus.LastBuildDate > lastStatus.LastBuildDate && currentStatus.BuildStatus == TriggerStatus)
                {
                    return request;
                }
            }
            catch (SocketException)
            {
                Log.Warning("Skipping Fire() because ServerUri " + ServerUri + " was not found.");
            }

            return null;    
    }

    private static ITrigger NewIntervalTrigger()
    {
      IntervalTrigger trigger = new IntervalTrigger();
      trigger.IntervalSeconds = DefaultIntervalSeconds;
      trigger.BuildCondition = BuildCondition.ForceBuild;
      return trigger;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.