Child pages
  • Display timer triggers
Skip to end of metadata
Go to start of metadata

This scripts displays the timer triggers for all the jobs in order to better arrange them.

import hudson.model.*
import hudson.triggers.*

for(item in Hudson.instance.items) {
	for(trigger in item.triggers.values()) {
		if(trigger instanceof TimerTrigger) {
			println("--- Timer trigger for " + item.name + " ---")
			println(trigger.spec + '\n')
		}
	}
}

Sample ouput produced:

--- Timer trigger for LIC-01-BuildXnetWebApp ---
# from Monday to Friday, at 6:00am
0 6 * * 1,2,3,4,5

--- Timer trigger for PTF-01-BuildXnetWebApp ---
# from Monday to Friday, at 5:00am
0 5 * * 1,2,3,4,5

--- Timer trigger for WRH-01-BuildXnetWebApp ---
# from Monday to Friday, at 7:00am
0 7 * * 1,2,3,4,5

--- Timer trigger for XNT-01-BuildCore ---
# from Monday to Friday, at 5:00am
0 5 * * 1,2,3,4,5

3 Comments

  1. Thank you for the script. Please find below an extended version which sorts by hour and minute of the timer spec

    Sorted Timer Trigger list
    import hudson.model.*
    import hudson.triggers.*
    
    class Timer {
      String name;
      String spec;
      int minute;
      int hour;
    
      Timer(pname,pspec) {
        name=pname;
        spec=pspec;
        def parts=pspec.split(" ")
        minute=parts[0].toInteger()
        hour=parts[1].toInteger()
      }
    }
    
    def timers = []
    
    for(item in Hudson.instance.items) {
      for(trigger in item.triggers.values()) {
        if(trigger instanceof TimerTrigger) {
          def timer=new Timer(item.name,trigger.spec)
          timers.add(timer)
        }
      }
    }
    
    timers.sort{a,b -> a.hour <=> b.hour ?: a.minute <=> b.minute}
    for (timer in timers) {
      printf("%02d:%02d %s\n",timer.hour,timer.minute,timer.name)
      // println(timer.spec + '\n')
    
    }
    
    sample output
    02:00 com.bitplan.rest
    02:00 com.bitplan.resthelperinterface
    02:19 com.bitplan.sqlrest
    02:55 com.bitplan.javapom
    03:00 com.bitplan.storage.jdbc
    03:08 com.bitplan.storage.simplexml
    03:15 com.bitplan.resthelper
    03:30 com.bitplan.phpbobase
    04:00 com.bitplan.notesbrowser
    04:05 com.bitplan.mac.MacAddressBook
    04:15 com.bitplan.iTunesRest
    04:25 com.bitplan.backuptool
    04:45 com.bitplan.bobase
    05:05 com.bitplan.swarchtest
    05:15 com.bitplan.testutil
    05:25 com.bitplan.bobase.gen
    05:30 com.bitplan.smartgenerator
    05:35 com.bitplan.db2obj
    
  2. The following extended version shows the goals for maven jobs and the output is formatted a bit more

    // Jenkins Groovy Script to show a sorted list of timer triggers with corresponding goals
    // 
    // derived from
    // https://wiki.jenkins-ci.org/display/JENKINS/Display+timer+triggers
    // 
    // created 2012-07-17
    // by WF (http://www.bitplan.com)
    
    
    // needed imports
    // see http://javadoc.jenkins-ci.org/
    import hudson.model.*
    import hudson.triggers.*
    import hudson.maven.*
      
    // Information about TimerTrigger and the job it belongs to
    class Timer {
      // name of job
      String name;
      // goals of (maven) - job
      String goals;
      // full timer specification e.g. 0 2 * * * for 2:00 am
      String spec;
      // extracted minute
      int minute;
      // extracted hour
      int hour;
      
      // construct a timer with the given name, goals and timer specification
      Timer(pname,pgoals,pspec) {
        name=pname;
        goals=pgoals;
        spec=pspec;
        // split the timer spec
        def parts=pspec.split(" ")
        // get the minute and hour parts
        minute=parts[0].toInteger()
        hour=parts[1].toInteger()
      }
    }
    
    // start with an empty list of timers
    def timers = [] 
    
    // loop over all Hudson jobs
    for(item in Hudson.instance.items) {
      // look for triggers
      for(trigger in item.triggers.values()) {
        // that are timer triggers
        if(trigger instanceof TimerTrigger) {
          // extract Timer information
          // check if goals are available
          def goals=""
          if (item instanceof MavenModuleSet) {
            goals=item.goals
          }
          // create a new timer information
          def timer=new Timer(item.name,goals,trigger.spec)
          // add it to the list
          timers.add(timer)
        }
      }
    }
    
    // sort all timers first by hour, then by minute
    timers.sort{a,b -> a.hour <=> b.hour ?: a.minute <=> b.minute}
    
    // print a formatted list of all timer information
    
    // print header
    printf("%-5s | %-50s | %s\n","Time","Job","Goals")
    printf("%-5s-+-%-50s-+-%s\n","-----","--------------------------------------------------","----------------------------------------")
    
    // loop over timers
    for (timer in timers) {
      // print a formatted line
      printf("%02d:%02d | %-50s | %s\n",timer.hour,timer.minute,timer.name,timer.goals)  // println(timer.spec + '\n')
        
    }
    

    Example output

    Time  | Job                                                | Goals
    ------+----------------------------------------------------+-----------------------------------------
    02:00 | com.bitplan.rest                                   | clean install deploy
    02:00 | com.bitplan.resthelperinterface                    | clean install deploy
    02:19 | com.bitplan.sqlrest                                | -U clean install deploy javadoc:javadoc
    02:55 | com.bitplan.javapom                                | -U clean install deploy
    03:00 | com.bitplan.storage.jdbc                           | install deploy
    03:08 | com.bitplan.storage.simplexml                      | clean install deploy
    03:15 | com.bitplan.resthelper                             | clean install deploy javadoc:javadoc
    03:30 | com.bitplan.phpbobase                              | 
    04:00 | com.bitplan.notesbrowser                           | clean install deploy
    04:05 | com.bitplan.mac.MacAddressBook                     | clean install deploy javadoc:javadoc
    04:15 | com.bitplan.iTunesRest                             | -U clean install deploy javadoc:javadoc
    04:25 | com.bitplan.backuptool                             | -U clean install deploy javadoc:javadoc
    04:30 | com.bitplan.bobase                                 | -U clean install deploy javadoc:javadoc
    04:35 | com.bitplan.db2obj                                 | install deploy javadoc:javadoc
    04:40 | com.bitplan.cobolgen                               | install deploy javadoc:javadoc
    04:45 | com.bitplan.NotesSql.gen                           | install deploy javadoc:javadoc
    04:50 | com.bitplan.delphigen                              | install deploy javadoc:javadoc
    05:00 | com.bitplan.test.smartgenerator                    | -Dfile.encoding=ISO-8859-1 test javadoc:javadoc
    05:05 | com.bitplan.swarchtest                             | -U clean install deploy javadoc:javadoc
    05:10 | com.bitplan.architekturFachbeispiel                | install deploy javadoc:javadoc
    05:15 | com.bitplan.testutil                               | -U clean install deploy javadoc:javadoc
    05:25 | com.bitplan.bobase.gen                             | install deploy javadoc:javadoc
    05:30 | com.bitplan.smartgenerator                         | install deploy javadoc:javadoc
    
  3. These are great... but do they actually work in a current Jenkins?

    Testing with 2.91, it seems they don't. (sad)

Write a comment…