Unable to abort build executing Groovy Postbuild script

29 views
Skip to first unread message

Marcin Zajączkowski

unread,
Mar 19, 2018, 11:54:58 AM3/19/18
to Jenkins Developers
Hi,

In the Groovy Postbuild plugin I call an external service in a loop waiting for some other operation to finish. It works, however, the build ignores a try to abort it using the Jenkins UI or an API.

I tried to check if the current thread isn't interupted (Thread.currentThread().isInterrupted()), but it's not. The build results (manager.getResult()) is also SUCCESS.

How can I programatically detect (probably using some Jenkins API) that it was requested to abort a build to break my loop and allow the build to be aborted?

Marcin

Ullrich Hafner

unread,
Mar 19, 2018, 12:21:04 PM3/19/18
to Jenkins Developers
You can detect if the build has been aborted by accessing Thread.interrupted() or Thread.currentThread().isInterrupted();
But your long running task needs to check this...

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/974664bf-1e64-411c-9e19-19a64304b86b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Marcin Zajączkowski

unread,
Mar 20, 2018, 5:08:50 AM3/20/18
to Jenkins Developers
On Monday, March 19, 2018 at 5:21:04 PM UTC+1, Ullrich Hafner wrote:
You can detect if the build has been aborted by accessing Thread.interrupted() or Thread.currentThread().isInterrupted();
But your long running task needs to check this...


Thanks for your quick response Ullrich. Unfortunately, I tried it before sending my message to the list and it didn't work. Sample code snippet - Groovy Postbuild:

<CODE>
int i=0

while (true) {

  if (++i > 20) {    //to do not create a zombie build :)
    manager.listener.logger.println "Timeout. Aborting..."
    break
  }

  if (Thread.currentThread().isInterrupted()) {
    manager.listener.logger.println "Interrupted. Aborting..."
    break
  }

  manager.listener.logger.println "Not aborted - ${manager.getResult()} - ${Thread.currentThread().isInterrupted()}"

  sleep(1000)
}
</CODE>

As a result I have:

<OUTPUT>
Not aborted - SUCCESS - false
Not aborted - SUCCESS - false
(...)
Not aborted - SUCCESS - false Timeout. Aborting...
</OUTPUT>

I tested it also with the Groovy system script and the result is the same. I'm unable to detect if an abort was requested. It's somehow a limitation if you wait for some external event in the Groovy script.

Is it the same thread? Is there any other way to check if an abort was requested?

Marcin

 
Reply all
Reply to author
Forward
0 new messages