Skip to content

Instantly share code, notes, and snippets.

@adamcooper
Created November 8, 2010 17:02
Show Gist options
  • Select an option

  • Save adamcooper/667942 to your computer and use it in GitHub Desktop.

Select an option

Save adamcooper/667942 to your computer and use it in GitHub Desktop.
Process multi-jobs per fork of resque
Resque.after_fork do |job|
jobs_performed = 0
kill_fork_at = Time.now.to_i + (ENV['MINUTES_PER_FORK'].to_i * 60)
worker = job.worker
first_job = job
worker.procline "Processing #{job.queue} since #{Time.now.to_i} until #{kill_fork_at}"
first_job.perform
# rely on parent error handling
worker.log "done: #{first_job.inspect}"
worker.done_working
jobs_performed += 1
while Time.now.to_i < kill_fork_at && !worker.shutdown?
if job = worker.reserve
worker.working_on(job)
job.perform
# rely on parent error handling
worker.log "done: #{job.inspect}"
worker.done_working
jobs_performed += 1
else
sleep(1)
end
end
first_job.instance_eval("def perform; end")
end
@mguterl

mguterl commented Nov 18, 2010

Copy link
Copy Markdown

just curious, why do you instance_eval on the first_job?

@adamcooper

Copy link
Copy Markdown
Author

The instance_eval is to prevent the first_job from running again when this after_fork finishes.

If you look at the source of resque, the after_fork hook is called after_forking and before running the job. This after_fork hook runs the first job first, to maintain the correct job order.

@mguterl

mguterl commented Nov 20, 2010

Copy link
Copy Markdown

Thanks Adam, I hadn't really grokked too much of the callback code in Resque, but this makes sense. Your patch is working great for us in production, thanks!

@adamcooper

Copy link
Copy Markdown
Author

Glad to hear. We are using it as well in production and haven't had any issues.

@mguterl

mguterl commented Feb 1, 2011

Copy link
Copy Markdown

We recently removed this patch from our setup because it was causing the Failure backend to report incorrect failures. When a job would fail, it would sometimes have arguments from a previous job / or the class name was mismatched.

@tispratik

Copy link
Copy Markdown

mguterl have you been able to work-around the "incorrect failure reporting" thing?

@mguterl

mguterl commented Mar 3, 2011

Copy link
Copy Markdown

We're now using resque-multi-job-forks

@tispratik

Copy link
Copy Markdown

Thanks mguterl !

@tispratik

Copy link
Copy Markdown

i used resque-multi-job-forks, but the workers quit when all the jobs are completed.
$ MINUTES_PER_FORK=1 COUNT=2 QUEUE=db rake resque:workers
(in /home/my_app)
(in /home/my_app)
The signal QUIT is in use by the JVM and will not work correctly on this platform
The signal USR1 is in use by the JVM and will not work correctly on this platform
The signal QUIT is in use by the JVM and will not work correctly on this platform
The signal USR1 is in use by the JVM and will not work correctly on this platform
155 were processed in this fork
245 were processed in this fork

$ _

Do you know why could that be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment