简体   繁体   中英

delayed_job not working on paperclip

I have this simply method that check against a URL existence and if not present try to create the photo object.

 def create_profile_photo(user_id,image_url)
   if Photo.find_by_external_url(image_url).blank?
     profile_photo = Photo.new
     profile_photo.user_id = user_id
     profile_photo.picture_from_url(image_url)
     profile_photo.save
   end   
 end

# Photo.rb
def picture_from_url(url)
  self.external_url = url
  self.attachment = URI.parse(clean_url)
end

It does work. But when I try to launch the method using delayed_job It does not work.

ruby-1.9.2-p290 :085 > MyClass.new.delay.create_profile_photo(347,'http://www.google.com/images/srpr/logo3w.png')
(0.4ms)  COMMIT
 => #<Delayed::Backend::ActiveRecord::Job id: 42, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::ProfileableMethod\nobject:...", last_error: nil, run_at: "2012-12-08 21:40:06", locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: "2012-12-08 21:40:06", updated_at: "2012-12-08 21:40:06"> 
ruby-1.9.2-p290 :085 > Photo.count
   (0.3ms)  SELECT COUNT(*) FROM "photos" 
=> 0

Any idea?

Of course there's no Photo instance in the photos table. According to your console, all you did was queue the job up.

Unless you have a DJ worker running, you can't expect the DJ queue to be processed. If the DJ queue isn't processed, you can't expect queued jobs to run. If the queued job hasn't been run, you can't expect photos to have any new records.

Run the command below and you'll see the job still queued up waiting to be run.

Delayed::Job.find(42)

In development, run the following to start a DJ daemon.

script/delayed_job

And finally, as @simonmorley suggested, make sure you kill and restart the daemon between code changes in order for the jobs to be processed by an up-to-date version of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM