简体   繁体   中英

Using sidekiq to read from reading database in Rails

This is model:

class DataInfo < ActiveRecord::Base
  STATUS = {:UNAPPROVED => 1, :APPROVED => 2, :PROCESSED => 3 }
  attr_accessible :id, :owner
  validates :status, :inclusion => {:in => STATUS.values}    
end

I want to write a worker which will read all rows with Status column value as :APPROVED . It will make a PUT request on those rows and upon receiving success, I want to update the status :PROCESSED . I am not understanding how to use sidekiq for active record.

When Sidekiq starts your database connections are also initialized. So you can query your database using Active Record as you normally would inside your Rails application.

Based on what you outlined it sounds like what you need is to write an observer to monitor for changes on that particular field. When that field is set to Approved send a job to Sidekiq to process. Something similar to what I have provided below:

class StatusObserver < ActiveRecord::Observer
  def after_save(datainfo)
    if datainfo.status.eql? "APPROVED"
      DataInfoWorker.perform_async datainfo.id
    end 
  end
end

class DataInfoWorker
  include Sidekiq::Worker

  def perform(datainfo_id)
    datainfo = DataInfo.find datainfo.id

    #Do your desired business logic here.
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM