Search code examples
ruby-on-rails-4sidekiq

Sidekiq-scheduler passing arguments


I have a use case where I need to do do some task 1 day after a product is uploaded. My worker class is written below:

require 'sidekiq-scheduler'
class ProductWorker < ActiveRecord::Base
   include Sidekiq::Worker
   def self.day_after_upload(product_id)
      #do something
   end 
   def self.week_after_upload(product_id)
      #do something
   end 
end

My product controller is below

class ProductsController < ApplicationController
   def create
      product=Product.new(product_params)
      if product.save! 
          Sidekiq.set_schedule('day_after_upload', { 'at' => [prduct.created_at+1.day], 'class' => 'ProductWorker' })
          Sidekiq.set_schedule('week_after_upload', { 'at' => [product.created_at+1.week], 'class' => 'ProductWorker' })
      end
    end    
end

I am having trouble passing the 'product_id' parameter to the worker. I couldn't find how to pass custom parameter on sidekiq-scheduler github page . How do I pass custom parameters and do I need to make some other changes for this to work? Any help is appreciated.


Solution

  • Scheduled jobs are built into Sidekiq, I don't know why you are using sidekiq-scheduler.

    OneDayWorker.perform_in(1.day, product_id)    
    OneWeekWorker.perform_in(1.week, product_id)
    

    https://github.com/mperham/sidekiq/wiki/Scheduled-Jobs