Do Workers = Long Running Processes?

20 views
Skip to first unread message

jc...@migrate.io

unread,
Apr 26, 2013, 11:15:09 AM4/26/13
to rin...@googlegroups.com
I have a long running process which will sit and poll a REST API for a status change. Traditionally, I had just created a Runnable instance and fed it to a Java Thread, but thought I might look into Ringo's Worker and WorkerPromise api.

So my Worker module will have an onmessage() handler to receive some control messages which will tell the polling loop whether to start, stop, shutdown. So a WorkerPromise is out for me as it has a one-time use syntax. I have other types of workers where WorkerPromise will work nicely.

But, my worker that polls does not look like it will fit well in the Worker or WorkerPromise world. At some point in the operation of this Worker I need a loop that functions like this:

function poll() {
    while (notShutdown) {
        while (isPolling) {
            <do the polling work>
        }
        java.lang.Thread.sleep(1000);
    }
}

Is this just a bad fit for the Worker, or is there a technique that I am missing? 

si...@nekapuzer.at

unread,
Apr 26, 2013, 12:27:31 PM4/26/13
to rin...@googlegroups.com
On Fri, Apr 26, 2013 at 08:15:09AM -0700, jc...@migrate.io wrote:
> I have a long running process which will sit and poll a REST API for a
> status change.

that's a perfect fit for a worker.

>
> function poll() {
> while (notShutdown) {
> while (isPolling) {
> <do the polling work>
> }
> java.lang.Thread.sleep(1000);
> }
> }
>
> Is this just a bad fit for the Worker, or is there a technique that I
> am missing?

similar to how you would do it in a browser: you don't want to block the
event loop with an endless loop. instead, use setinterval/timeout calls
for repeated tasks. e.g.:

function poll() {
<do polling>
if not shutdown
setTimeout(poll, 1000)
}

setTimeout(poll, 1000)

hope that helps...
simon
Reply all
Reply to author
Forward
0 new messages