1

Creating an background job with the resque_scheduler gem on Redis server.

 class Estamps::OrderAssignment < ActiveRecord::Base

  after_save :enqueue_check_status

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

 class AutoRejectionJob < ActiveJob::Base
   queue_as :default

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

On creation of OrderAssignment record or when updated after 2 minutes it should run AutoRejection Job. Here the prob is the set(wait: 2.minutes) does not seem to run, i.e.

   AutoRejectionJob.perform_later(self.id)

works perfectly fine, but

  AutoRejectionJob.set(wait: 2.minutes).perform_later(self.id)

does nothing. Haven't been able to rectify the issue. Newbie to Rails so please help.

Mahesh Mesta
  • 793
  • 1
  • 11
  • 28

2 Answers2

0

I see no problem with your code. I checked : .set(wait: 2.minutes) works as expected in rails 5.0.2 on top of ruby 2.4.0

So does your call of the job. The way I see it, you're trying to set a status used elsewhere. Probably, the error is due to the OrderAssignment being manipulated in an outside treatment (destroyed ?)

Since you said you're new to rails (I suppose that's what "newbie" means) I'm going to make some suggestions. Disregard them if you're past that ...

There also are some great debugging tools out there to help you find what's going on : byebug, better_errors, pry and of course, the rails console. Do yourself a favor : try them.

When I can't find my way around some behavior that goes beyond my grasp, I use some "puts", and some "try / catch errors" structures (begin rescue ensure in ruby)... :

def perform(*args) 
  puts "@@@@@@@ JOB TRIGGERED @@@@@@"
  begin
    order_assignment_id = args[0]
    order_assignment = Estamps::OrderAssignment.find(order_assignment_id)
    puts "#{order_assignment.inspect}"
    if order_assignment.status_id == 1 || order_assignment.status_id == nil
      order_assignment.status_id = 3
      order_assignment.save!
    end
    puts "@@@@@@@ JOB DONE @@@@@@"
  rescue StandardError => e
    # ... display e.message ...
  ensure
    #... 
  end
end
0
  1. check your rails version.
  2. check your rails logs ( log folder, all the jobs will write message to log files when performed )
Siwei
  • 19,858
  • 7
  • 75
  • 95