Skip to content
rares edited this page Sep 12, 2010 · 48 revisions

FAQ

Q: How do I set the priority of a job?

A: Jobs with a higher priority number will be run before other jobs. Normally, jobs are given a priority of 0. Here’s an example where we set the priority to “1”, which means it will run before any other jobs in the queue with a lower priority number:

Delayed::Job.enqueue(NewsletterJob.new('text', User.find(:all).collect(&:email)), 1)

Q: How do I set a job to start at a later (or specific time)?

A: Here’s an example that would set the job to run 5 minutes from now. The “0” is the default priority level for the job, the “5.minutes.from_now” sets the job to run 5 minutes from the time it’s saved in the delayed_jobs table:

Delayed::Job.enqueue(NewsletterJob.new('text', User.find(:all).collect(&:email)), 0, 5.minutes.from_now)

Q: Hello, tobi, I tried this plugin but I got 2 failing built-in specs. The “delayed_job / lib / delayed / job.rb” line 94 is “(`locked_at` IS NULL OR `locked_at` < #{quote_value(now + max_run_time)})”. Should it be “(… < #{quote_value(now – max_run_time)})”?

A: I got that same error. I think it’s a problem with postgres compatibility. It appears to be fixed with this commit. This is now fixed in master; dj should support all database adapters that ActiveRecord implements.

Q: How to a configure the plugin to save failed jobs?

A: Here’s how I configured the plugin. I also wanted to change the number of max attempts, etc:

  # config/initializers/delayed_job_config.rb

  Delayed::Job.destroy_failed_jobs = false
  silence_warnings do
    Delayed::Job.const_set("MAX_ATTEMPTS", 3)
    Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)
  end

Q: I’m having trouble keeping the job running going when jobs fail. Isn’t there a way to store the error message and move on?

A: My errors are long, so I had to change the last_error column from string to text:


class ChangeLastErrorInDelayedJobsToText < ActiveRecord::Migration
  def self.up
    change_column :delayed_jobs, :last_error, :text
  end

  def self.down
    change_column :delayed_jobs, :last_error, :string
  end
end

Q: Why do I get an error talking about Struct::Group if I use Group.find within a perform method?

A: There’s a strange edge-case with Ruby/Rails where Struct::Group already exists, so calling Group.anything from within a class that inherits from Struct will have strange results. You can workaround this issue by prefixing calls to Group with two colons. For example, ::Group.find would work as expected.

Clone this wiki locally