How to sleep inside a MasterToSlaveCallable

15 views
Skip to first unread message

Daniel Anechitoaie

unread,
May 25, 2018, 10:21:32 AM5/25/18
to Jenkins Developers
Hi,

I have a plugin that calls a MasterToSlaveCallable with the following signature "private static class DataImportCallable extends MasterToSlaveCallable<Void, IOException>".
Inside this MasterToSlaveCallable I zip some files and then upload them to a server using Apache HTTPClient and then make some REST API calls where I need to periodically (every 1 minute)
check if work is done with the ZIP I have uploaded.

My problem is that MasterToSlaveCallable seems to only be allowing one exception to be thrown and calling Thread.sleep(...) can throw InterruptedException which does not match the signature of my MasterToSlaveCallable.

How can I "sleep" the thread inside the MasterToSlaveCallable until work is done?

Matt Sicker

unread,
May 25, 2018, 11:48:36 AM5/25/18
to jenkin...@googlegroups.com
try {
  Thread.sleep(...);
} catch (InterruptedException ex) {
  Thread.currentThread().interrupt();
}

That's the normal Java idiom at least. The InterruptedException is checked purely so you are forced to deal with it like this because it's the safer way of killing a thread.

--
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-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/75e070c4-2011-465b-8fc9-e7c3b2face10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Sicker
Software Engineer, CloudBees

Daniel Anechitoaie

unread,
May 25, 2018, 12:07:45 PM5/25/18
to Jenkins Developers
I was concerned that calling Thread.currentThread().interrupt(); to not cause any issues with Jenkins, like crash the job or something like that.
It's clear that it will stop the job, as it should. Just that it was not clear to me how Jenkins handles this and if me doing this won't break Jenkins in any way.

Thank you. 



On Friday, May 25, 2018 at 6:48:36 PM UTC+3, Matt Sicker wrote:
try {
  Thread.sleep(...);
} catch (InterruptedException ex) {
  Thread.currentThread().interrupt();
}

That's the normal Java idiom at least. The InterruptedException is checked purely so you are forced to deal with it like this because it's the safer way of killing a thread.
On Fri, May 25, 2018 at 9:21 AM, Daniel Anechitoaie <danie...@gmail.com> wrote:
Hi,

I have a plugin that calls a MasterToSlaveCallable with the following signature "private static class DataImportCallable extends MasterToSlaveCallable<Void, IOException>".
Inside this MasterToSlaveCallable I zip some files and then upload them to a server using Apache HTTPClient and then make some REST API calls where I need to periodically (every 1 minute)
check if work is done with the ZIP I have uploaded.

My problem is that MasterToSlaveCallable seems to only be allowing one exception to be thrown and calling Thread.sleep(...) can throw InterruptedException which does not match the signature of my MasterToSlaveCallable.

How can I "sleep" the thread inside the MasterToSlaveCallable until work is done?

--
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.

Jesse Glick

unread,
May 25, 2018, 4:05:37 PM5/25/18
to Jenkins Dev
On Fri, May 25, 2018 at 11:48 AM, Matt Sicker <msi...@cloudbees.com> wrote:
> try {
> Thread.sleep(...);
> } catch (InterruptedException ex) {
> Thread.currentThread().interrupt();
> }

If you receive an `InterruptedException`, I think you would rather
want to just abort the loop. Just be sure to test what happens when
the build is aborted—this will interrupt the thread on the master
calling `call()`, but I cannot remember if Remoting automatically
forwards that interrupt to the agent side and waits for the agent to
handle it, or if `call()` returns immediately, or what. Whichever the
case, you may need for the master-side thread to fail with an
`InterruptedException` in order for the build to be properly marked as
`ABORTED`. Pipeline and freestyle may have different behaviors here,
so test both if you support both.
Reply all
Reply to author
Forward
0 new messages