2012-06-22 4 views
2

나는 ruby ​​on rail을 기반으로하는 웹 어플리케이션을 가지고 있는데, 이는 this 작업 젬을 지연 시켰습니다.레일 - 지연된 작업 완료

지연된 작업을 트리거하는 기능이있어서 차례대로 다른 지연된 작업이 트리거됩니다. 부모와 관련된 모든 작업이 완료되었음을 나타낼 수있는 이벤트 인 중첩 케이스가 있습니까? 아니면 내가 문서를 검색하려고 할 때 사용할 수있는 데이터가 무엇이든간에 작업해야합니까?

예 : 어쩌면

def create_vegetable 
#..... creates some vegetable 
end 

def create_vegetable_asynchronously id 
    Farm.delay(priority: 30, owner: User.where("authentication.auth_token" => token).first, class_name: "Farm", job_name:create "create_vegetable_asynchronously").create_vegetable(id) 
end 

def create_farm_asynchronously data 
    data.each do |vegetable| 
    create_vegetable_asynchronously vegetable.id 
    end 
end 

handle_asynchronously :create_farm_asynchoronously 

답변

3

조금 잔인한 사람, 당신은 명시 적으로 계층 적 작업을 구성하고 하위 작업에 상위 작업 ID를 전달하는 후크 전에 사용 할 수 있지만. 같은 뭔가 : 당신이 팜을 만들 시작할 때

class Job < Delayed::Job 
    belongs_to :parent, class: 'Job' #also add db column for parent_id 
end 

class CreateVegetable 

    def initialize(id) 
    @id = id 
    end 

    def perform 
    Farm.create_vegetable(id) 
    end 

end 


class CreateFarm 
    def initialize(vegetable_ids,owner_id) 
    @vegetable_ids = vegetable_ids 
    @owner_id = owner_id 
    end 

    def before(job) 
    @job_id = job.id 
    end 

    def perform 
    @vegetable_ids.each { |id| Job.enqueue CreateVegetable.new(id), priority: 30, owner_id = @owner_id, :parent_id = @job_id } 
    end 

end 

그런 다음, 어떻게 든 작업 ID를 기억한다.

Job.where(parent_id: @parent.id) 
:

def create_farm_asynchronously data 
    owner_id = User.where("authentications.auth_token" => token).first.id 
    @parent = Job.enqueue CreateFarm.new(data.map(&:id), owner_id) 
end 

당신은에 의해 하위 작업을 확인할 수 있습니다

관련 문제