5

I need to perform a job wherein each time an order is created it is assigned to a vendor and if the vendor does not accept the order and updates the status within a specified time, the order is auto-rejected and the status updated to rejected. The problem which I am facing is that the job goes to the delayed queue as shown in the resque web view but does not moves to the main queue after the specified time for it to delay lapses

Here is my job.

  class AutoRejectionJob < ActiveJob::Base
    queue_as :auto_rejection_queue

   def perform(*args) 
    assignment_id = args[0]
    order_assignment = Estamps::Assignment.find(assignment_id)
    if order_assignment.status_id == 1 || order_assignment.status_id == nil
      order_assignment.status_id = 3
      order_assignment.save!
    end
   end
  end         

In my assignment model:

class Estamps::Assignment < ActiveRecord::Base
 after_create :enqueue_check_status 

def enqueue_check_status #  
  AutoRejectionJob.set(wait: 2.minutes).perform_later(self.id)
end    
end

Here once an assignment record is created the status is usually kept as "assigned" at time of its creation. Now from the time of its creation, if the user does not update the status within the specified time, the job has to automatically update the status to "rejected".

I tried this method too.

def enqueue_check_status 
  Resque.enqueue_at_with_queue('auto_rejection_queue', 2.minutes.from_now, 
   AutoRejectionJob, assignment_id: self.id)  
end

Both of them send the job to the resque delayed queue but do not or bring the job to the main queue to perform.

AutoRejection Job enqueuing in delayed jobs list

Also, the time stamp for the job shows no jobs listed to be scheduled when I click on the all schedules link for the delayed job

All schedules for the first job in the delayed list

I am stuck with this issue for almost two weeks. Please, help! If any more info is needed, let me know. Having a tough time with this one.

Mahesh Mesta
  • 793
  • 1
  • 11
  • 28
  • 1
    What happens when you run `rake resque:scheduler`? It's this that does the work to poll these queues and move them across. Presumably something is going wrong at that point. Perhaps it isn't loading the environment correctly? – stef Jul 27 '17 at 15:29
  • There is completed example in https://github.com/resque/resque-scheduler. with 1552 star. –  Jul 30 '17 at 09:42
  • I want to help you and understand your issue. There would be problem in your own project. Could you provide full project of yours? – Pig and Cat Jul 30 '17 at 09:47
  • @stef I have a rake task to run scheduler as rake resque:restart_workers.. i ve include my tasks in this run and running this rake does not yield any error – Mahesh Mesta Aug 05 '17 at 16:38
  • @MaheshMesta, perhaps you could mark my answer as correct for others that may encounter this problem? – Ash Aug 09 '17 at 01:26

1 Answers1

4

I have solved this issue.

Basically having the workers running isn't enough in itself to process the delayed jobs. There is a special rake task you must run:

$ PIDFILE=./resque-scheduler.pid BACKGROUND=yes \
    rake resque:scheduler

The resque:scheduler rake task checks the scheduled jobs and the delayed jobs (by default every 5 seconds) and moves them to be processed by your workers. You can read more about how to configure and setup the resque:scheduler rake task and its settings, on the official resque-scheduler gem readme.

In my case, I was using capistrano-resque gem to deploy and manage the workers (restart them when capistrano deploys a new release) by adding the line to the deploy.rb file:

after "deploy:restart", "resque:restart"

While this loaded my workers (and restarted them as it should), you need to add the additional line (to deploy.rb):

after 'passenger:restart', 'resque:scheduler:restart'

To get capistrano to run the resque:scheduler task for you.

After I did this, everything worked exactly perfectly (and actually loaded the delayed jobs retrospectively that should have been processed but hadn't been yet).

As indicated however, if you do have problems make sure that your rake resque:scheduler task has access to the environment variables - or it wont work.

Ash
  • 24,276
  • 34
  • 107
  • 152