Cluster question

179 views
Skip to first unread message

Ω Alisson

unread,
Dec 8, 2014, 8:35:35 PM12/8/14
to nod...@googlegroups.com
I'm running a Node app that fetches Redis data into another database. It´s using the cluster module, everything works fine but I need to ensure that workers will finish properly their jobs(they use BRPOP then allocate into a object then insert in batches cleaning the objects), how do I do this?

Issac Roth

unread,
Dec 9, 2014, 10:00:03 AM12/9/14
to nod...@googlegroups.com
There are many ways to do this but we created strong-mq with this kind of use case in mind. What’s nice is you can use a common message-passing pattern between workers that runs natively over node-cluster without installing anything else, and if you end up scaling across servers you can not change your code but go to a different messaging backend.

Another way would be to implement a simple semaphore kind of setup using strong-store-cluster. Or do the same in Redis.

Issac                  

Create APIs in Node in minutesLoopBack

On Monday, December 8, 2014 at 3:47 PM, Ω Alisson wrote:

I'm running a Node app that fetches Redis data into another database. It´s using the cluster module, everything works fine but I need to ensure that workers will finish properly their jobs(they use BRPOP then allocate into a object then insert in batches cleaning the objects), how do I do this?

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CACZE8Y5XARF0mzr7H30jVqoS33Kf7KzR_yFE4FOuh%2Bybj%3DJuSw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ω Alisson

unread,
Dec 9, 2014, 1:26:54 PM12/9/14
to nod...@googlegroups.com
if I do something like this on a worker process, wouldn't be effective?

process.on("message", function(m) {
  if(m.cmd === "disconnect") {
     // cleanup logic
  }
})

John Fitzgerald

unread,
Dec 9, 2014, 3:13:04 PM12/9/14
to nod...@googlegroups.com
Yes, that works pretty well actually, I've used it a lot to synchronize a small in-memory cache to each process and distribute jobs to workers. 



For more options, visit https://groups.google.com/d/optout.



--
John R. Fitzgerald

Sam Roberts

unread,
Dec 10, 2014, 7:01:40 PM12/10/14
to nod...@googlegroups.com
>> On Monday, December 8, 2014 at 3:47 PM, Ω Alisson wrote:
>>
>> I'm running a Node app that fetches Redis data into another database. It´s
>> using the cluster module, everything works fine but I need to ensure that
>> workers will finish properly their jobs(they use BRPOP then allocate into a
>> object then insert in batches cleaning the objects), how do I do this?


On Tue, Dec 9, 2014 at 7:33 AM, Ω Alisson <thelin...@gmail.com> wrote:
> if I do something like this on a worker process, wouldn't be effective?
>
> process.on("message", function(m) {
> if(m.cmd === "disconnect") {
> // cleanup logic
> }
> })

If I understand correctly, you want workers to be given a chance to
finish what they are doing when disconnected using node cluster?

In principle this works, though that message doesn't exist (unless you
send it?). You don't say how/when you fork and/or disconnect workers.

Generally, this would happen in gracefull shutdown by calling
`cluster.disconnect()` in the node master. Unfortunately, while that
waits
for cluster worker ***servers*** to close, it doesn't wait for
clients, like redis clients. LIke yours, likely.

You can trap the disconnect message if you are willing to use node
internals: https://gist.github.com/sam-github/8185222

node cluster should really expose that event, IMO, but so it goes, for
the moment.

Most supervisors, strongloops's included, offer a way to inform the
worker that it should cleanup. `slc run` sends a shutdown message,
https://github.com/strongloop/strong-cluster-control/blob/master/api.md#controlcmdshutdown,
for example, when shutting down, and gives 60seconds grace to the
worker (configurable), before SIGTERMing it.

Cheers,
Sam

Ω Alisson

unread,
Dec 10, 2014, 11:34:45 PM12/10/14
to nod...@googlegroups.com
Interesting Sam, but what if I set a flag that is checked every process.nextTick and suspends redis updates, so what's left is finishing iterating over in-memory items?

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.

Sam Roberts

unread,
Dec 16, 2014, 9:59:11 PM12/16/14
to nod...@googlegroups.com
On Wed, Dec 10, 2014 at 8:15 PM, Ω Alisson <thelin...@gmail.com> wrote:
> Interesting Sam, but what if I set a flag that is checked every
> process.nextTick

You will eat 100% CPU, rather than waiting on event from the master
saying that its time to go away, and being able to do the cleanup you
want:

Ω Alisson

unread,
Dec 16, 2014, 11:25:27 PM12/16/14
to nod...@googlegroups.com
Well I did it and the CPU is running at 0.2%

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.

Ryan Graham

unread,
Dec 17, 2014, 12:59:49 AM12/17/14
to nodejs
Can you share how you accomplished this? (in code, if possible.. JS is way better than English with the diversity of backgrounds and native languages seen on this list)

I'm not aware of a JS level API in node core that provides what you described, so like Sam, I assumed you meant a busy loop which generally would indeed use 100% CPU and block your event loop.

Did you mean an arbitrarily long process.nextTick() chain, instead? The behaviour of which is so different from "once per tick" that it's a shame the name hasn't been changed! Your check would be called up to 1000 times (by default) per tick and then be queued into the next tick.

Or did you mean sprinkling one-off process.nextTick() calls throughout your code so that a check is queued at least once per event loop tick?

~Ryan

On Tue, Dec 16, 2014 at 7:39 PM, Ω Alisson <thelin...@gmail.com> wrote:
Well I did it and the CPU is running at 0.2%

On Tue, Dec 16, 2014 at 11:22 PM, Sam Roberts <s...@strongloop.com> wrote:
On Wed, Dec 10, 2014 at 8:15 PM, Ω Alisson <thelin...@gmail.com> wrote:
> Interesting Sam, but what if I set a flag that is checked every
> process.nextTick

You will eat 100% CPU, rather than waiting on event from the master
saying that its time to go away, and being able to do the cleanup you
want:

> suspends redis updates, so what's left is finishing
> iterating over in-memory items?
 

--

Ω Alisson

unread,
Apr 17, 2015, 10:09:35 AM4/17/15
to nod...@googlegroups.com
Digging this topic again, I'm unable to make the worker process log anything on shutdown...tried with internalMessage, message(worker.send()), nothing happens.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages