This is the first worker which I am writing using Sidekiq. Can someone 
please review it?

I have two model classes as follows:

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

FileInfo has id and status fields. Each file can multiple file-entries 
(rows) of the type FileEntry. FileEntry has id, file_info_id (foreign key 
of FileInfo) and status fields.

class FileEntry < ActiveRecord::Base
  STATUS = {:UNAPPROVED => 1, :READY => 2 , :SENT => 3 }
  attr_accessible :id, :file_info_id, :status, :reason
  validates :status, :inclusion => {:in => STATUS.values}end

I want to write a worker to asynchronously process all files whose status 
field is APPROVED (FileInfo model). Each of the thread should process all 
file-entries for that particular file whose status is READY. The thread 
should finish once all the entries are processed for that file. Assuming 
that File-entries having status UNAPPROVED will also become READY.

Once each entry is processed, its status should be updated as SENT. Once 
all the file-entries all have SENT status for a particular file, update 
that file's status as PROCESSED. 

I have code this much so far. Not able to figure out how to code the worker:

class FileInfoObserver < ActiveRecord::Observer
  def after_save(file_info)        
    if file_info.status.eql? 2
       FileProcessingJob.perform_async(file_info.id)    
    end
  endend

The worker is as follows:

class FileProcessingJob
  include Sidekiq::Worker
  def perform(file_id)
    puts "job"
    flag =1    
    while flag==1
      count = 0
      FileRow.where("file_info_id = #{file_id}").find_each do |file_row|
        if(file_row.status == 2)
          puts "update" //Some PUT request, instead wrote a puts statement
          FileRow.update(file_row.id, :status => 3)
        elsif(file_row.status == 0 || file_row.status ==1)
          count = count + 1
        end
      end
      if(count == 0)
        flag = 0
      end
    end
  end end



*Is this correct way of doing it? How to enable retry mechanism? Is this 
code thread-safe? Can FileRow updation happen in batches instead of single 
update at a time? *

-- 
You received this message because you are subscribed to the Google Groups 
"GitLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/gitlabhq/3228d5f3-01e8-46bb-8ccb-7c0722766aae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to