Bamboo
  1. Bamboo
  2. BAM-2960

Stop build script feature - Something to run when a build is cancelled / stopped

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.6
    • Fix Version/s: 3.1
    • Component/s: Builds
    • Labels:
    • Last commented by user?:
      true
    • Comments:
      4

      Description

      Currently in Bamboo you cant stop a build once it has left the queue and is in progress. The problem is made more intractable because Bamboo doesnt really know what build processes to stop.

      I am proposing that we put a feature in Bamboo build configuration that would be a "program to run on Build cancel". This would be invoked if the user presses the Cancel build button whena build is in progress.

      This program would be given 3 arguments. The process id the build process in question, the build plan and the buld number say. Perhaps other Bamboo properties could eb stuffed into the process environment as well.

      This would allow the "stop process" to do what ever it needs to do to stop the build from running. This might be a kill -9 style script under Unix or a clean shutdown program.

      The key point here is that Bamboo does not take on the reposibility of being able to stop a build proccess (and all the support questions that would raise) but rather allows customers to have a mechanism to clean up running builds.

      Maybe a database needs to be cleanly shutdown, or a TCP connection to a POS terminal emulator. There are all sorts of clean up tasks that a build might need. We just need to give them enough information to write such a script.

      I would advocate that Bamboo have a simple "shim" program that it used to start any process. So this would produce a shim per build like

      BAMBOO ----
                          shim build123 -----
                                                     maven xxxx xxx -------
                                                                                         andsoon
                          shim build456 -----
                                                     maven xxxx xxx -------
                                                                                         andsoon
      

      The shim would simple report its process id back to Bamboo via stdout/stderr and then Bamboo knows what COULD be killed, eg all the programs of and including shim xyz

      The parameters to the shim would be the project/plan/build number so that when someone looks ate the running processes (via say ps) they can easily identify the builds. The rest of the parameters would be the build command to run.

      So for example the shim program invocation might look like

      shim "JIRA Unit Tests - HEAD" "JDK1.6" "BASIC-JDK16-3645" maven clean test

      And the first line of stdout might be

      pid=323455
        	 __  __
       	|  \/  |__ _Apache__ ___
       	| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
       	|_|  |_\__,_|\_/\___|_||_|  v. 1.0.2
       	
       	build:start:
       	
       	clean:clean:
       	    [delete] Deleting directory /home/jira2-bamboo-j2ee/bamboo-home/xml-data/build-dir/BASIC-JDK16/target
      ...
      ...
      ...
      

      then followed by the builder output.

      As for finding out the current Java process id, there are two solid approaches. The first is to use JMX on Java5 or above. This works reliably on Sun JVM's on Windows and Unix.

      ManagementFactory.getRuntimeMXBean().getName();
      

      The other is to use JNA. I have spiked this on both Windows and Linux with great success.

      http://jna.dev.java.net/

      public class JNAProcessKit
      {
          private interface CLibrary extends Library
          {
              final static CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
      
      
              /**
               * On POSIX its called getpid()
               */
              int getpid();
      
              /**
               * On Win32 C runtime its called _getpid()
               */
              int _getpid();
          }
      
          public static int getPID()
          {
              int pid = 0;
              if (Platform.isWindows())
              {
                  pid = CLibrary.INSTANCE._getpid();
              }
              else
              {
                  pid = CLibrary.INSTANCE.getpid();
              }
              return pid;
          }
      }
      

      Once we have the process id of the shim, we then have a place that peole can terminate a running. Also the user of the shim will mean the builds will turn up more easily in process viewers like
      ps.

        Issue Links

          Activity

          Hide
          Brad Baker [Atlassian] added a comment -
          Show
          Brad Baker [Atlassian] added a comment - A possible solution to http://jira.atlassian.com/browse/BAM-2221
          Hide
          Mark Chaimungkalanont [Atlassian] added a comment -

          This isn't true. Bamboo issues a Process.destroy when you try to stop a running build. This issues a SIGTERM to the running process and does actualy work most of the time. This doesn't guarantee that the process shuts down, especially in cases of well hung sub processes (e.g. A hung JIRA Server, which was launched from Cargo, which was launched from Maven, which was launched a script, which was launched from Bamboo).

          The problem with the destroy method has been raised with Sun frequently for some time and it doesn't look like a fix will come our way any time soon.

          Show
          Mark Chaimungkalanont [Atlassian] added a comment - This isn't true. Bamboo issues a Process.destroy when you try to stop a running build. This issues a SIGTERM to the running process and does actualy work most of the time. This doesn't guarantee that the process shuts down, especially in cases of well hung sub processes (e.g. A hung JIRA Server, which was launched from Cargo, which was launched from Maven, which was launched a script, which was launched from Bamboo). The problem with the destroy method has been raised with Sun frequently for some time and it doesn't look like a fix will come our way any time soon.
          Hide
          Rene Medellin [Atlassian] added a comment -

          +1

          I heart the true spirit of this request:

          So I dont think Bamboo should get in this game. Instead it should provide a mechanism to run something during a "build cancelled" so that customers can do whatever its right.

          We already have Post Build actions. I think we only need a Stop Build action type thang and we're off to the races. Let each Build decide for itself how to best kill itself.

          Show
          Rene Medellin [Atlassian] added a comment - +1 I heart the true spirit of this request: So I dont think Bamboo should get in this game. Instead it should provide a mechanism to run something during a "build cancelled" so that customers can do whatever its right. We already have Post Build actions. I think we only need a Stop Build action type thang and we're off to the races. Let each Build decide for itself how to best kill itself.
          Hide
          Mark Chaimungkalanont [Atlassian] added a comment -

          You can now add a script as a "Final Task" that will always run at the end of a build

          Show
          Mark Chaimungkalanont [Atlassian] added a comment - You can now add a script as a "Final Task" that will always run at the end of a build

            People

            • Votes:
              6 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Last commented:
                2 years, 49 weeks, 2 days ago