LaunchCruiseControlBuildTask.cs :  » Build-Systems » CruiseControl.NET » LoGeek » BuildServer » CustomTasks » 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 » LoGeek » BuildServer » CustomTasks » LaunchCruiseControlBuildTask.cs
using System;
using System.Runtime.Remoting;
using System.Threading;
using NAnt.Core;
using NAnt.Core.Attributes;
using ThoughtWorks.CruiseControl.Remote;

namespace LoGeek.BuildServer.CustomTasks{
  /// <summary>
  /// A NAnt task to launch a build on a CCNet server.
  /// </summary>
  [TaskName("launchccnetbuild")]
  public class LaunchCruiseControlBuildTask : Task
  {
    #region Private Instance Fields

    private string _serverUrl;
    private string _projectName;
    private int _timeOutInSeconds = 30*60;
    private int _pollingIntervalInSeconds = 5;
    private ICruiseManager _cruiseManager;

    #endregion Private Instance Fields

    #region Public Instance Properties

    [TaskAttribute("serverurl", Required=true)]
    public string ServerUrl
    {
      get { return _serverUrl; }
      set { _serverUrl = value; }
    }

    [TaskAttribute("projectname", Required=true)]
    public string ProjectName
    {
      get { return _projectName; }
      set { _projectName = value; }
    }

    /// <summary>
    /// Time out in seconds. Default to 1800 seconds (30 minutes)
    /// </summary>
    [TaskAttribute("timeoutinseconds", Required=false)]
    public int TimeOut
    {
      get { return _timeOutInSeconds; }
      set { _timeOutInSeconds = value; }
    }

    /// <summary>
    /// Polling interval in seconds. Default to 5 seconds.
    /// </summary>
    [TaskAttribute("pollinginterval", Required=false)]
    public int PollingInterval
    {
      get { return _pollingIntervalInSeconds; }
      set { _pollingIntervalInSeconds = value; }
    }

    #endregion Public Instance Properties

    #region implementation of Task

    protected override void ExecuteTask()
    {
      Log(Level.Info, "Connecting to CCNet server " + ServerUrl);
      _cruiseManager = (ICruiseManager) RemotingServices.Connect(typeof (ICruiseManager), ServerUrl);

      IntegrationStatus status = LaunchBuild(_cruiseManager, ProjectName, PollingInterval, TimeOut);
      if (status != IntegrationStatus.Success)
        throw new BuildException(string.Format("Project '{0}' failed : {1}", ProjectName, status));
    }

    #endregion 

    /// <summary>
    /// Return the current status of the project. Return null if the project does not exist.
    /// </summary>
    private ProjectStatus GetCurrentProjectStatus(ICruiseManager cruiseManager, string name)
    {
      ProjectStatus[] allStatus = cruiseManager.GetProjectStatus();
      foreach (ProjectStatus status in allStatus)
      {
        if (status.Name == name)
          return status;
      }
      return null;
    }

    public IntegrationStatus LaunchBuild(ICruiseManager cruiseManager, string projectName, int pollingIntervalInSeconds, int timeOutInSeconds)
    {
      ProjectStatus status = GetCurrentProjectStatus(cruiseManager, projectName);
      if (status == null)
        throw new BuildException(string.Format("Project '{0}' not found on the build server.", projectName));

      if (status.Activity != ProjectActivity.Sleeping)
        throw new BuildException(string.Format("Project '{0}' activity is '{1}' instead of expected '{2}'", projectName, status.Activity, ProjectActivity.Sleeping));

      Log(Level.Info, "Forcing build for project '{0}'", projectName);
      cruiseManager.ForceBuild(projectName);

      DateTime startTime = DateTime.Now;
      TimeSpan timeout = new TimeSpan(0, 0, timeOutInSeconds);
      while (true)
      {
        TimeSpan elapsed = DateTime.Now - startTime;
        if (elapsed >= timeout)
          throw new BuildException(string.Format("Project '{0}' build timed-out (lasted more than {1} seconds)", projectName, timeOutInSeconds));
        Thread.Sleep(pollingIntervalInSeconds*1000);
        // check current integration status to decide what to do
        status = GetCurrentProjectStatus(cruiseManager, projectName);
        switch (status.Activity)
        {
          case ProjectActivity.Building:
            // fine, keep it rolling
            break;
          case ProjectActivity.CheckingModifications:
            // fine
            break;
          case ProjectActivity.Sleeping:
            // the build is finished (may be successful or have failed)
            return status.BuildStatus;
          default:
            throw new Exception(string.Format("Unknown ProjectActivity '{0}'",status.Activity));
        }
      }
    }
  }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.