Reputation: 31
Currently I'm working with delayed_job to process pdf files and I need to know if a file has been processed.
Once completed I need to change a value in the database to notify the user that their file has been processed correctly.
I'm using delayed_job with mongoid and paperclip as processor.
Upvotes: 1
Views: 447
Reputation: 832
I've had similar problem lately, although my code was using different convention of job execution. Instead of using Job class it uses delay method e.g. myobj.report.delay.generate_downloaded_version
all you need is hidden in handler in such case because myobj is being serialized to YAML, therefore:
irb(main):018:0> job = Delayed::Job.find("53c64778bd19836c5f00009b")
irb(main):020:0> YAML::load(job.handler)
=> #<Delayed::PerformableMethod:0x7f6a569d4230 @object=#<Analysis::Report _id: 53b161c2bd198338cb000030, analysis_id: BSON::ObjectId('53b143f4bd19835c96000016'), created_at: Mon Jun 30 13:10:26 UTC 2014, updated_at: Wed Jul 16 12:31:11 UTC 2014, validator_ids: ["links"], page_patern: "*", _type: nil, pages: nil, entries: [], report_type: "detail", message_levels: ["errors", "warnings"], locale: "pl", state: "enqueued">, @args=[], @method_name=:generate_downloaded_version>
irb(main):021:0>
Upvotes: 0
Reputation: 31
I tried this callback and it works:
# config/intializers/delayed_paperclip
module DelayedPaperclip
module Jobs
class DelayedJob
def success
# here comes database change value
end
end
end
end
But the problem is that I can't refer to the parent of this job to get the object id and change the database value to processed = true.
This is the job object, without parent reference:
{
"_id" : ObjectId("53f504e06d696e5fc5010000"),
"priority" : 0,
"attempts" : 0,
"queue" : null,
"handler" : "--- !ruby/struct:DelayedPaperclip::Jobs::DelayedJob\ninstance_klass: Note\ninstance_id: !ruby/object:BSON::ObjectId\n raw_data: !binary |-\n U/UE321pbl/FAAAA\nattachment_name: :file\n",
"run_at" : ISODate("2014-08-20T20:28:16.178Z"),
"updated_at" : ISODate("2014-08-20T20:28:16.178Z"),
"created_at" : ISODate("2014-08-20T20:28:16.178Z")
}
How Can I refer to the creator of the job?
Is there any other solution? (obviously yes, I suppose)
Thanks,
Upvotes: 1
Reputation: 1711
You should be able to simply define a method which first processes the pdf and then changes the database values or notifies a user and run that as the delayed job.
Upvotes: 0