Scheduler Job not running in Rails 4

6 views
Skip to first unread message

Mahesh Mesta

unread,
Feb 23, 2017, 3:39:36 AM2/23/17
to BANGALORE RUG-Ruby Users Group

Hi,


I have a rails app where an order is assigned to a vendor and the vendor has to accept the order within the next 15 minutes (for test purpose, I am keeping it to two minutes).Once the order is assigned to a vendor, status attribute in the order-assignment table for orders and vendors is updated to assigned. Now the vendor has to accept or reject the order based on which the status is updated to accepted and rejected respectively. If the vendor does not accepts/rejects the order in the next 15 minutes from the time of assignment, the status is automatically updated to rejected in the order-assignment table. Here are the snippets

OrderAssignment model:

class Estamps::OrderAssignment < ActiveRecord::Base
belongs_to :vendor, class_name: 'Estamps::Vendor'
belongs_to :order, class_name: 'Estamps::Order'
belongs_to :status, class_name: 'Estamps::OrderAssignments::Status'


after_save :enqueue_check_status 

def enqueue_check_status 
   AutoRejectionJob.set(wait: 2.minutes).perform_later(self.id)
   if self.status_id == 3
    OnRejectionJob.perform_later(self.id)
   end
end

end

Order model:

has_many :order_assignments
belongs_to :vendor

Vendor Model:

has_many :order_assignments
has_many :orders

Auto_rejection_job

class AutoRejectionJob < ActiveJob::Base
queue_as :auto_rejection

def perform(order_assignment_id)
    order_assignment = Estamps::OrderAssignment.find(order_assignment_id) 
    if order_assignment 
         if order_assignment.status_id == 1 
             order_assignment.update_attribute('status_id', '3')
             order_assignment.save!
         end  
    end
end
end         

OrderAssignment schema:

 create_table "estamps_order_assignments", force: :cascade do |t|
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer  "vendor_id"
  t.integer  "order_id"
  t.integer  "status_id"
 end

Here the status_id comes from another table which contains various statuses.

Now after creation of an order assignment record, where the status is initially assigned(status_id: 1), if the vendor does not updates the status, then the status_id is automatically updated to status_id:3(rejected). This I tried achieving in the auto_rejection_job. But although, the job is showed as enqued, the status_id is not updated. The job did run correctly when I ran it on console as

AutoRejectionJob.set(wait: 2.minutes).**perform_now**(an id), 

then the status_id is updated, but does not work for the specified scheduled time.Where am I going wrong? HOw can I achieve this? Newbie to rails, so please help.


Regards,
Mahesh

Reply all
Reply to author
Forward
0 new messages