Catching a job cancel

20 views
Skip to first unread message

John Westcott IV

unread,
Sep 26, 2019, 4:17:23 PM9/26/19
to Jenkins Developers
Is it possible for a plugin to get a notification if a Jenkins job is cancelled while the plugin is working?
i.e. is there some kind of "public void on_job_cancel()" method I can implement to take some action if my plugin is actively running when a job is canceled?
I'm not seeing anything in the docs but wanted to double check in case I am missing something.

-John

Jeff Pearce

unread,
Sep 26, 2019, 4:20:26 PM9/26/19
to jenkin...@googlegroups.com

You can probably get the info if you implement the RunListener extension point

 

From: <jenkin...@googlegroups.com> on behalf of John Westcott IV <jowe...@redhat.com>
Reply-To: "jenkin...@googlegroups.com" <jenkin...@googlegroups.com>
Date: Thursday, September 26, 2019 at 1:17 PM
To: Jenkins Developers <jenkin...@googlegroups.com>
Subject: Catching a job cancel

 

Notice: This email is from an external sender.

 

--
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/6296320b-1e84-4caa-9686-475650e8d334%40googlegroups.com.

Robert Sandell

unread,
Sep 30, 2019, 5:06:23 AM9/30/19
to Jenkins Developer List
When a build is canceled the executor threads will be interrupted. So if your plugin is doing a remote call, waiting on something, or sleeping an InterruptedException will be thrown.
Otherwise you can check the status of the current thread: Thread.currentThread().isInterrupted()

/B



--
Robert Sandell
Software Engineer
CloudBees, Inc.
CloudBees-Logo.png
Twitter: robert_sandell

John Westcott

unread,
Oct 1, 2019, 8:36:44 PM10/1/19
to jenkin...@googlegroups.com
Thanks for the tip on this. Unfortunately, my code does more than just sleep its also gathering log information as well as running status checks. Is there anyway to put a larger try/catch block around all of that code rather than just the sleep method? Or a way to register an action on the builder itself for an on-cancel event? Or would that have to be a request to Jenkins core?

Does anyone have an example of implementing the RunListener extension point?

-John

You received this message because you are subscribed to a topic in the Google Groups "Jenkins Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-dev/bD22cVOyp60/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CALzHZS2RPku1jJR39z1_%3DLUkrKgsSvrZkBsgnMtaPZnYCmYPUA%40mail.gmail.com.

Robert Sandell

unread,
Oct 2, 2019, 4:01:00 AM10/2/19
to Jenkins Developer List
If you are doing a lot of on master calculations you should either consider moving that out to the agent (workspace.act) or, once in a while, check if the current thread is interrupted for example on the top of every loop.

Examples of extensionpoint implementations can be found among the documentation: https://jenkins.io/doc/developer/extensions/jenkins-core/#runlistener

/B

John Westcott

unread,
Oct 14, 2019, 8:33:23 AM10/14/19
to jenkin...@googlegroups.com
Thanks for the help with this, I didn’t realize that the thread would continue to run once canceled; I assumed it would have been killed off. 

-John

Reply all
Reply to author
Forward
0 new messages