kue icon indicating copy to clipboard operation
kue copied to clipboard

Queue Events Emitted to All Cluster Workers

Open seriallos opened this issue 12 years ago • 11 comments

Using Node's built-in cluster module, it looks like the events that can be handled by the Queue object are handled for every child process.

Here's the simplest test case (in coffee script):

cluster = require 'cluster'
kue = require 'kue'

if cluster.isMaster
  cpus = require('os').cpus().length
  for [1..cpus]
    cluster.fork( process.env )

  kue.app.listen 3001
else
  jobs = kue.createQueue()

  jobs.on 'job complete', ( id ) ->
    console.log "Worker #{cluster.worker.id}: Job #{id} complete"

  jobs.process 'test', ( job, done ) ->
    console.log "Worker #{cluster.worker.id}: Running test job"
    done()

Create a 'test' job using the JSON API:

$ curl -H "Content-Type: application/json" -X POST -d '{ "type":"test" }' http://localhost:3001/job

Here's the output from the running process:

$ coffee index.coffee
Worker 3: Running test job
Worker 3: Job 1874 complete
Worker 1: Job 1874 complete
Worker 2: Job 1874 complete
Worker 7: Job 1874 complete
Worker 5: Job 1874 complete
Worker 8: Job 1874 complete
Worker 6: Job 1874 complete
Worker 4: Job 1874 complete

My expectation was that only the child process that executed the job would handle the complete event.

Right now I'm looking at maintaing a global list of job IDs that the current child process has started and only take action if that process ran the job but it seems like that should be something that Kue handles itself.

seriallos avatar Mar 05 '14 21:03 seriallos

This is the default and correct behavior @seriallos (since that comes from redis subscriptions) job complete events are global queue events, and subscribers are receiving them. you can have a single master listener, or keep track of each worker's job id's, or even not use this handler and use process's done instead.

behrad avatar Mar 06 '14 19:03 behrad

or even not use this handler and use process's done instead.

Is there a way I can hook into this that I'm not seeing?

It definitely seems like there's no way I can add a listener to error/complete to a job that comes in from the API and only have the child process that executes it catch it. Instead, I have to track all the IDs myself or duplicate a bunch of logic/code into every single job I write (of which I have many).

seriallos avatar Mar 06 '14 20:03 seriallos

Nothing special, just at the end of your worker child, call your job complete logic handler after calling done, or bind to job complete in master not the children.

behrad avatar Mar 06 '14 22:03 behrad

IMO, Queue events should only be those actually happened inside that instance, not globally by pub/sub, it doesn't make sense, usually you only want to handle them once in one place. With node cluster, sure you can only handle them in master, but what about distributed deployment across multiple machines?

inetfuture avatar Apr 06 '14 07:04 inetfuture

What I need is one place to remove all completed jobs, problem is I don't have a singleton master, all my processes are equal to each other across multiple machines, so kue.on('job complete') is not ideal, currently I'm attaching common event handlers to every job instance after creating them.

I get your point that Consider n worker processes which none are Kue workers (they are just using kue.on('job complete') ), yeah the current behavior is reasonable.

inetfuture avatar Apr 06 '14 08:04 inetfuture

why don't you remove them in your specific job event handlers? job.on('complete') -> remove after n seconds...

Another suggestion is to write a simple kue sweeper/maintenance housekepper to periodically remove garbages. We use this method in production. you can find related talks on issues...

behrad avatar Apr 06 '14 09:04 behrad

When use job.on('complete'), what if the process who created the job get restarted before the job get done, no one will ever respond to that job's complete event

Another type of process like housekepper just make it harder to maintain

I still think an fire once global event handler could be convenient, and that's not hard to accomplish, just change/add a few lines code

inetfuture avatar Apr 06 '14 09:04 inetfuture

When use job.on('complete'), what if the process who created the job get restarted before the job get done, no one will ever respond to that job's complete event

This is yet true for the master process also.

Another type of process like housekepper just make it harder to maintain

Why?

behrad avatar Apr 06 '14 09:04 behrad

another point noticable for your case is that, you can blindly run remove job multiple times for a specific job. So performance-unwise that already works.

behrad avatar Apr 06 '14 09:04 behrad

I'm in a small team, so I'm trying my best to make everything as simple as possible, if you I add one special type of process, I can imagine that one day my teammate would call me for help because he forget to start that process(or it crashed)...

remove job multiple times for a specific job, no, I kind of having ocd :unamused: , I want everything perfect.

inetfuture avatar Apr 06 '14 09:04 inetfuture

you can write a simple sweep script and schedule it via cron.

behrad avatar Apr 06 '14 13:04 behrad