从作业任务中引用delayed_job作业ID

时间:2014-02-05 23:23:19

标签: ruby delayed-job

我有一个吐出文件的delayed_job任务。我希望能够在文件前面加上创建它的作业的ID,以便以后轻松引用它。

# Create a file that has the job id in the name...
job_id = thing.delay.create_file
# Now we'll use the job_id to search for the file...

这可能吗?

2 个答案:

答案 0 :(得分:1)

delayed_job使用表来排队作业,该表可以在Gem's migration file中找到。该表将有一个默认的id列,您可以查询:

ActiveRecord::Base.connection.raw_connection.prepare("Select id FROM delayed_job where handler=?","YAML Encoded string representing the object doing work")

答案 1 :(得分:1)

让我们从这里开始......

class WriteFileJob
  attr_accessor :dj_id, :file_name

  def initialize(file_name)
    @file_name = file_name
  end

  def perform
    # do something with @dj_id; don't worry, we'll set it below
  end
end

现在当你排队工作时,你应该找回一份工作:

j = Delayed::Job.enqueue(WriteFileJob.new("foo.txt"))

接下来,您加载您入队的对象,并使用您刚刚返回的ID更新它:

object_to_update = YAML.load(j.handler)
object_to_update.dj_id = j.id
# update the handler
j.handler = object_to_update.to_yaml
j.save