Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't delayed job process enqueued job?

I have stripped down my example as much as possible. In my application I create a dummy class and try to enqueue the call method. This gets added to the database fine and with delayed_jobs running in the background it picks it up and updates it to locked. But it doesn't actually finish running the job. It just stays in the locked state.

pry(main)> class DummyClass
pry(main)*   def self.call
pry(main)*     puts 'will this ever work?'
pry(main)*   end
pry(main)* end
=> :call

pry(main)> DummyClass.delay.call

(0.2ms)  BEGIN
SQL (0.3ms)  INSERT INTO `delayed_jobs` (`created_at`, `handler`, `queue`, `run_at`, `updated_at`) VALUES ('2014-12-19 12:11:40.006107', '--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/class \'DummyClass\'\nmethod_name: :call\nargs: []\n', 'default', '2014-12-19 12:11:40.005811', '2014-12-19 12:11:40.006107')
(16.2ms)  COMMIT

=> #<Delayed::Backend::ActiveRecord::Job:0x007fd0fdec0e20
 id: 22,
 priority: 0,
 attempts: 0,
 handler: "--- !ruby/object:Delayed::PerformableMethod\nobject: !ruby/class 'DummyClass'\nmethod_name: :call\nargs: []\n",
 last_error: nil,
 run_at: Fri, 19 Dec 2014 14:11:40 CAT +02:00,
 locked_at: nil,
 failed_at: nil,
 locked_by: nil,
 queue: "default",
 created_at: Fri, 19 Dec 2014 14:11:40 CAT +02:00,
 updated_at: Fri, 19 Dec 2014 14:11:40 CAT +02:00>

Locks the task here

  SQL (0.8ms)  UPDATE `delayed_jobs` SET `delayed_jobs`.`locked_at` = '2014-12-19 12:17:20.031925', `delayed_jobs`.`locked_by` = 'delayed_job host:Ryan-Mes-MacBook-Pro-2.local pid:60080' WHERE ((run_at <= '2014-12-19 12:17:20.031003' AND (locked_at IS NULL OR locked_at < '2014-12-19 12:12:20.031154') OR locked_by = 'delayed_job host:Ryan-Mes-MacBook-Pro-2.local pid:60080') AND failed_at IS NULL) ORDER BY priority ASC, run_at ASC LIMIT 1

Then it just hangs. I don't get why such a simple task is not working.

Note this pry console is running on my existing rails application. It might be a application configuration issue, but I have not been able to find it.

Any ideas? I can try give more information, but I think this is everything.

The actual code I am using is below

module Events
  class ForwardRequestToPulse
    def self.call
      puts 'will this ever work'
    end
  end
end

class MyTestController < ApplicationController
  def index
    Events::ForwardRequestToPulse.delay.call
  end
end

The record is added to the delayed_jobs table find. When I run bin/delayed_job run the record is locked but not processed.

Below is an image of the locked record Locked record in database

like image 863
Ryan-Neal Mes Avatar asked Dec 19 '14 10:12

Ryan-Neal Mes


Video Answer


2 Answers

I'm more familiar with Sidekiq but I think the problem is that you are defining a class to run in the rails console and the workers are run in a separate process. The workers are unaware of a DummyClass and fail to execute the job. I recommend:

  1. Make this class a Rails model (Dummy etc.)
  2. Restart delayed job
  3. Insert the record into the delayed_job table
  4. Check for locked state
like image 178
eabraham Avatar answered Nov 03 '22 00:11

eabraham


After fiddling around in the gem I narrowed the problem down to delayed_job not looking for locked records correctly. It was including milliseconds in the filter for locked_at. mySql stores datetimes without the milliseconds so nothing was being picked up. After a bit more research in the gem in github delayed_job_active_record (I thought the problem was me!) I found this fix. This resolves my issue.

The reason it was happening for me was I was referencing the latest gem 4.0.2 before this fix! Just my luck.

So if you are using 4.0.2 ... just upgrade and it should disappear.

like image 31
Ryan-Neal Mes Avatar answered Nov 02 '22 23:11

Ryan-Neal Mes