How to find if build is in progress?

In v1, we do not exposed an option in the UI or otherwise to check whether the build machine is free or not.  If the build machine is not free and you fire a build, you get an error back, which is fine.  However, if you want to queue builds or pick one of the machines that is free, you need a way to check whether the build machine is free or not.

You can do this using the code below.

  1. Copy/paste the code below in a file called CheckBuildMachine.cs

  2. Compile the file using following command -

    csc /r:%SystemDrive%\Windows\assembly\GAC_32\Microsoft.TeamFoundation.Build.Common\8.0.0.0__b03f5f7f11d50a3a\Microsoft.TeamFoundation.Build.Common.dll CheckBuildMachine.cs

  3. Run -
    CheckBuildMachine <your team project> <your build machine>
    to check whether the build machine is free or not.

The build machine is tied to one Team Foundation Server and can do one build per team project at a time. Multiple builds of different team project can be done simultaneously.

You can also use the zip file attached to download the complete solution and build it in VS.

Thanks,

Gautam

 /**===========================================================================
 * File: CheckBuildMachine.cs
 * ----------------------------------------------------------------------------
 *
 * This file is part of the Microsoft Visual Studio Team System Samples.
 *
 * Copyright(C)  2004  Microsoft Corporation. All rights reserved.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
 * WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 * ============================================================================
 */

using System;
using System.Diagnostics;
using Microsoft.TeamFoundation.Build.Common;

namespace Microsoft.TeamFoundation.Build.Samples
{
    class CheckBuildMachine
    {
        static void Main(string[] args)
        {
            if (args.Length == 1 && AskingForHelp(args[0]))
            {
                ShowUsage(0);
            }

            if (args.Length < 2 || args.Length > 3)
            {
                Console.Error.WriteLine("Invalid arguments.");
                ShowUsage(1);
            }

            teamProject = args[0];
            machine = args[1];
            if (args.Length == 3)
            {
                if (!int.TryParse(args[2], out port))
                {
                    Console.Error.WriteLine("Invalid port: {0}", args[2]);
                    ShowUsage(1);
                }
            }

            CheckMachine();
        }

        private static void CheckMachine()
        {
            Debug.Assert(machine != null);
            string uri = string.Format(BuildAgentUri, machine, port, typeof(IBuildAgent).FullName);

            try
            {
                IBuildAgent agent = (IBuildAgent)Activator.GetObject(typeof(IBuildAgent), uri);
                if (agent.IsBuildInProgress(teamProject))
                {
                    Console.WriteLine("The machine {0} not free.", machine);
                }
                else
                {
                    Console.WriteLine("The machine {0} is free.", machine);
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Unable to reach machine {0}. {1}", machine, ex.Message);
            }
        }

        private static bool AskingForHelp(string arg)
        {
            if (StringComparer.OrdinalIgnoreCase.Equals(arg, "/?") ||
                StringComparer.OrdinalIgnoreCase.Equals(arg, "-?") ||
                StringComparer.OrdinalIgnoreCase.Equals(arg, "/h") ||
                StringComparer.OrdinalIgnoreCase.Equals(arg, "-h"))
                return true;

            return false;
        }

        private static void ShowUsage(int exitCode)
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("\tCheckBuildMachine.exe <team project> <build machine> [<port>]");
            Console.WriteLine("\tThe default value of port is 9191.");
            Console.WriteLine("Example:");
            Console.WriteLine("\tCheckBuildMachine.exe MyProject MyBuildMachine");

            Environment.Exit(exitCode);
        }

        private static string teamProject;
        private static string machine;
        private static int port = 9191; // default port

        private const string BuildAgentUri = "tcp://{0}:{1}/{2}";
    }
}

CheckBuildMachine.zip