Search code examples
ruby-on-railsruby-on-rails-3delayed-job

Why won't the Delayed Job error callback execute?


I am trying to follow this guide about handling API rate limits with Delayed Job.

Here is my job:

# app/jobs/mailer_job.rb
class MailerJob < Struct.new(:custom_id)

  def perform
    # Intentionally throw an error to test error callback
    raise StandardError
  end

  def error(job, exception)
    puts 'Error!'
  end

end

Here are the potentially related gems I have installed. I am using Ruby 1.9.3.

gem 'rails', '3.0.20'
gem 'passenger', '5.0.21'
gem 'delayed_job_active_record', '4.1.1'
gem 'delayed_job', '4.1.2'
gem 'foreman', '0.83.0'

I see the following in the Delayed Job log:

[Worker(host:ubuntu pid:9912)] Starting job worker
[Worker(host:ubuntu pid:9912)] Job MailerJob (id=1720) RUNNING
[Worker(host:ubuntu pid:9912)] Job MailerJob (id=1720) FAILED (0 prior attempts) with StandardError: StandardError
[Worker(host:ubuntu pid:9912)] 1 jobs processed at 12.9574 j/s, 1 failed

I never see the error callback happen. However, if I kill the rails server with CTRL+C then it instantly prints the error callback puts statement.

Why do the error callbacks on the custom Delayed Job not execute while the server is running?


Solution

  • It's likely that the error callback is working fine. Standard output of most OSs is buffered. So I'm pretty sure the string is being buffered until server shutdown, which flushes the buffer.

    Try STDERR.puts. On most OSs, standard error is not buffered.