Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chellakarthik24/227ecef1b5a4848355e949f7d4172dc7 to your computer and use it in GitHub Desktop.

Select an option

Save chellakarthik24/227ecef1b5a4848355e949f7d4172dc7 to your computer and use it in GitHub Desktop.

Revisions

  1. @vishalTrivedi88 vishalTrivedi88 created this gist May 23, 2020.
    30 changes: 30 additions & 0 deletions CheckSlaveOnline.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    import hudson.model.*
    import hudson.AbortException;

    def resolver = build.buildVariableResolver
    // def node_names = resolver.resolve("NODE_LIST").split(",")
    timeout_seconds = 600 // 10 mins
    sleepTime=30 // seconds

    def node_name=resolver.resolve("NODE_NAME").replaceAll("\\s","")
    slave = Hudson.instance.slaves.find({it.name == node_name});
    if (slave != null) {
    computer = slave.getComputer();
    if (computer.isOffline()) {
    println "$node_name is offline.";
    computer.connect(true)
    long startTime = System.currentTimeMillis(); //fetch starting time
    while(computer.isOffline()||(System.currentTimeMillis()-startTime)<timeout_seconds*1000){
    println "$node_name still offline.";
    println "Sleep for $sleepTime seconds"
    sleep(sleepTime*1000)
    }
    if (computer.isOffline()) {
    throw new Exception('$node_name didnt come online. Kindly check')
    }
    }else {
    println "OK: $node_name is online";
    }
    } else {
    println "Slave $node_name not found!";
    }