2012-06-22 212 views
2

我有一個基於ruby on rails的web應用程序,它使用this延遲作業寶石。Rails - 延遲完成作業

我有一個觸發延遲工作的功能,這又會觸發其他幾個延遲工作。有沒有辦法,窩情況是一個事件,可以表明所有與父母有關的工作已經完成?或者,當我嘗試檢索文檔時,是否需要處理任何可用的數據?

例子:

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 

那麼你可以通過檢查子作業