How to sleep inside a MasterToSlaveCallable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to sleep inside a MasterToSlaveCallable

Daniel Anechitoaie
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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: How to sleep inside a MasterToSlaveCallable

Matt Sicker
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 <[hidden email]> 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 [hidden email].
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

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CAEot4oyATHEqUcuHNx29FJWmG7vQTM4iEETk8SF0Xmcfir2zWg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to sleep inside a MasterToSlaveCallable

Daniel Anechitoaie
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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZIRm8_UxCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">danie...@...> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="ZIRm8_UxCwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jenkinsci-de...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/jenkinsci-dev/75e070c4-2011-465b-8fc9-e7c3b2face10%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-dev/75e070c4-2011-465b-8fc9-e7c3b2face10%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/jenkinsci-dev/75e070c4-2011-465b-8fc9-e7c3b2face10%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/jenkinsci-dev/75e070c4-2011-465b-8fc9-e7c3b2face10%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Matt Sicker
Software Engineer, CloudBees

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/c6b75cf6-51a2-408b-b3c7-e289eb14207c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to sleep inside a MasterToSlaveCallable

Jesse Glick-4
In reply to this post by Matt Sicker
On Fri, May 25, 2018 at 11:48 AM, Matt Sicker <[hidden email]> 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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr3NDe6Hy5%3DpjZARDcG%2Bq-vcA8J%2BFmNB3u0dXXMf_ExKCA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.