From 41fadb5a71acfaa8eeb112a0650b7fb46f131be4 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 14 Aug 2013 15:21:06 -0300 Subject: [PATCH] Moving delayed job changes to a monkey patch --- script/delayed_job | 21 --------------------- vendor/plugins/delayed_job/lib/delayed/worker.rb | 3 --- vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb diff --git a/script/delayed_job b/script/delayed_job index 87ee978..513d609 100755 --- a/script/delayed_job +++ b/script/delayed_job @@ -10,25 +10,4 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'envi require 'daemons' require 'delayed/command' -# based on https://groups.google.com/forum/#!topic/delayed_job/ZGMUFFppNgs -class Delayed::Worker - class ExceptionNotification < ActionMailer::Base - def mail error - environment = Environment.default - - recipients NOOSFERO_CONF['exception_recipients'] - from environment.contact_email - reply_to environment.contact_email - subject "[#{environment.name}] DelayedJob: #{error.message}" - body render(:text => error.backtrace.join("\n")) - end - end - - def handle_failed_job_with_notification job, error - Delayed::Worker::ExceptionNotification.deliver_mail error if NOOSFERO_CONF['exception_recipients'].present? - handle_failed_job_without_notification job, error - end - alias_method_chain :handle_failed_job, :notification -end - Delayed::Command.new(ARGV).daemonize diff --git a/vendor/plugins/delayed_job/lib/delayed/worker.rb b/vendor/plugins/delayed_job/lib/delayed/worker.rb index 53e6607..157782f 100644 --- a/vendor/plugins/delayed_job/lib/delayed/worker.rb +++ b/vendor/plugins/delayed_job/lib/delayed/worker.rb @@ -167,7 +167,6 @@ module Delayed job.last_error = error.message + "\n" + error.backtrace.join("\n") say "#{job.name} failed with #{error.class.name}: #{error.message} - #{job.attempts} failed attempts", Logger::ERROR reschedule(job) - rescue => e # don't crash here end # Run the next job we can get an exclusive lock on. @@ -187,8 +186,6 @@ module Delayed end run(job) if job - rescue => e - handle_failed_job(job, e) end end end diff --git a/vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb b/vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb new file mode 100644 index 0000000..641c680 --- /dev/null +++ b/vendor/plugins/monkey_patches/rescue_delayed_job_crashes/init.rb @@ -0,0 +1,49 @@ +Delayed::Worker.module_eval do + # based on https://groups.google.com/forum/#!topic/delayed_job/ZGMUFFppNgs + class Delayed::Worker::ExceptionNotification < ActionMailer::Base + def mail error + environment = Environment.default + + recipients NOOSFERO_CONF['exception_recipients'] + from environment.contact_email + reply_to environment.contact_email + subject "[#{environment.name}] DelayedJob: #{error.message}" + body render(:text => error.backtrace.join("\n")) + end + end + + def handle_failed_job_with_notification(job, error) + Delayed::Worker::ExceptionNotification.deliver_mail error if NOOSFERO_CONF['exception_recipients'].present? + handle_failed_job_without_notification job, error + end + alias_method_chain :handle_failed_job, :notification + + def handle_failed_job_with_rescue(job, error) + handle_failed_job_without_rescue(job, error) + rescue => e # don't crash here + end + alias_method_chain :handle_failed_job, :rescue + + protected + + # This code must be replicated because there is no other way to pass the job + # through and use alias_method_chain as we used on the previous method + def reserve_and_run_one_job + # We get up to 5 jobs from the db. In case we cannot get exclusive access to a job we try the next. + # this leads to a more even distribution of jobs across the worker processes + job = Delayed::Job.find_available(name, 5, self.class.max_run_time).detect do |job| + if job.lock_exclusively!(self.class.max_run_time, name) + say "acquired lock on #{job.name}" + true + else + say "failed to acquire exclusive lock for #{job.name}", Logger::WARN + false + end + end + + job = Delayed::Job.new + run(job) if job + rescue => e + handle_failed_job(job, e) + end +end -- libgit2 0.21.2