Pergunta

I've got a delayed_job task that spits out a file. I'd like to be able to prefix the file with the ID of the job that created it, to easily reference it later on.

# 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...

Is this possible?

Foi útil?

Solução

Let's take it from here...

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

Now when you enqueue the job, you should get a job back:

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

Next, you load the object that you enqueued, and updated it with the id you just got back:

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

Outras dicas

delayed_job uses a table to queue jobs and that table can be found in the Gem's migration file. That table will have a default id column which you can query for:

ActiveRecord::Base.connection.raw_connection.prepare("Select id FROM delayed_job where handler=?","YAML Encoded string representing the object doing work")
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top