Jenkins - Groovy script not ending after return statement

1.3k views Asked by At

I'm trying to run scheduled Groovy script on Jenkins but I got into some trouble - It won't finish runnning although it reaches the "return" statement.

I'm using in-house dependencies and Classes and I've found the line of code that if omitted, the script returns successfully. But I can't omit this line unfortunately :(

Do you have any idea what could cause a Jenkins build step too stay stuck?

I've noticed that the "culprit" line of code, internally runs the following: this.executorService.scheduleWithFixedDelay(this.eventsPublisher, 3L, 3L, TimeUnit.SECONDS);

Is it possible that playing with the Executor, messes around with the Jenkins build steps?

I'd love some help, Thanks a lot :)

UPDATE:

Code:

import java.sql.DriverManager
import java.sql.ResultSet
import java.text.DateFormat
import java.text.SimpleDateFormat
import hudson.model.*


def verticaConn = null
def verticaStmt = null
def mongoConnection = null

try {

    println("start script: vertica_to_kafka")

    // get params
    def verticaHostName = System.getenv("verticaHostName") //dev=192.168.247.11:5433 prod=192.168.251.120:5433
    def verticaDbName = System.getenv("verticaDbName")
    def verticaTBName = System.getenv("verticaTBName")
    def bootstrapServers = System.getenv("bootstrapServers")
    def limitNum = System.getenv("limitNum").toInteger()
    def startTime = System.getenv("startTime")


    MyKafkaStringProducer producer = new MyKafkaStringProducer();
    producer.init()

    MyEventDao eventDao = new MyEventDao();
    eventDao.setStringProducer(stringProducer);

    Class.forName("com.vertica.jdbc.Driver")
    String verticaConnectionString = "jdbc:vertica://${verticaHostName}/${verticaDbName}"

    Properties verticaProp = new Properties();
    verticaProp.put("user", "user");
    verticaProp.put("password", "password");
    verticaProp.put("ConnectionLoadBalance", 1);

    verticaConn = DriverManager.getConnection(verticaConnectionString, verticaProp);
    verticaStmt = verticaConn.createStatement()

    // vertica execution timestamp
    long currentTS = System.currentTimeMillis()
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String startTS = "1970-01-01 00:00:00";

    String command= "select * from ${verticaTBName} where ts >'${startTS}' "
    if (limitNum > 0) command += "limit ${limitNum}"

    println("querying vertica")
    verticaStmt.execute(command)
    ResultSet results = verticaStmt.getResultSet()


    println("start to send data to kafka")
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    while(results.next()){
        long id = results.getLong("id");
        String domain = results.getString("domain");
        String text = results.getString("text");
        Date ts = dateFormat.parse(results.getString("ts"));
        MyEntity myEntity = new MyEntity(id, domain, text, ts);
        eventDao.saveEntity(myEntity);
    }

} catch (Exception e){
    println(e.printStackTrace())
} finally {
    println("going to release resources");
    if (verticaStmt != null){
        try{
            verticaStmt.close()
            println("vertica statement closed successfully!");
        } catch (Exception e) {
            //println("error in close the vertica statement {}", e.getMessage());
        }
    }
    if (verticaConn != null){
        try{
            verticaConn.close()
            println("vertica connection closed successfully!");
        } catch (Exception e) {
            //println("error in close the vertica connection {}", e.getMessage());
        }
    }

    if (mongoConnection != null){
        try {
            mongoConnection.getMongo().close();
            println("mongo connection closed successfully!");
        } catch (Exception e) {
            //println("error in close the mongo connection {}", e.getMessage());
        }
    }
    println("end script: vertica_to_kafka")
}
return
System.exit(0)

And in MyKafkaStringProducer I found the following:

public synchronized void init() {
    if(this.active) {
        this.initKafkaProducer();
        this.executorService.scheduleWithFixedDelay(this.eventsPublisher, 3L, 3L, TimeUnit.SECONDS);
    }

}
0

There are 0 answers