Skip to content

Instantly share code, notes, and snippets.

# sysctl kern.seminfo
kern.seminfo.semmni=10
kern.seminfo.semmns=60
kern.seminfo.semmnu=30
kern.seminfo.semmsl=60
kern.seminfo.semopm=100
kern.seminfo.semume=10
kern.seminfo.semusz=112
kern.seminfo.semvmx=32767

Use delayed_paperclip to process Paperclip attachments in the background. Use Rails routes and Ajax to check when processing is done.

Live app which you can run on the fly: http://runnable.com/VR3XZVDPXUJ0k3VQ/paperclip-delayed-job (remember to run bin/delayed_jobs start before clicking the big green Run button)

STATUS: WORKING

app/models/photo.rb

Fetching affiliate products in Rails with Delayed::Job and Ajax

Straight forward fetching products from the affiliate API takes up to 8 seconds which is intolerable.

Demo app: https://github.com/dt1973/ajax-affiliates

Problem

How to set up the callback?

Replacing paperclip-ffmpeg with paperclip-av-transcoder

Live app with instructions: http://runnable.com/VR3jBtJKSwh21EKs/paperclip-av-transcoder

Problem

LoadError in Forem::TopicsController#create
cannot load such file -- /root/rails-repo/lib/paperclip_processors/qtfaststart.rb

Extracted source (around line #274):

InfiniteInterpolationError

Paperclip model which checks an upload (its temp file) whether it's an animated GIF. If so it sets attachment_is_animated in the db to true. But, because this job needs to go into the background (DelayedJob), the temp file is probably gone by the time it's needed, which is most likely what's causing the InfiniteInterpolationError.


Live app which you can run on the fly: http://runnable.com/VXMNrsiY_6Fd-YRn/paperclip-delayedjob-infiniteinterpolationerror (remember to run bin/delayed_job start before hitting the big green Run button)


What makes this an infinite loop?

class Photo < ActiveRecord::Base
  attr_accessor :applicable_styles

  before_post_process :setup_styles

  def applicable_styles
 @applicable_styles || setup_styles

paperclip-av-transcoder + paperclip-qtfaststart

Started POST "/default/topics" for ::ffff at 2015-04-05 14:05:33 +0000                                                                      
Processing by Forem::TopicsController#create as HTML                                                                                                    
  Parameters: {"utf8"=>"�~\~S", "authenticity_token"=>"+6Ma4fmBeexCEFsP9z8b5Af5vJQKarMssAxFdIHMHGEkRcP09T/zgmlVAM4Trb2iHWkHVFGFrHBqO3E8GGAzJw==", "topic
"=>{"subject"=>"sfgfgsfg", "posts_attributes"=>{"0"=>{"text"=>"sfhsg", "photos_attributes"=>{"0"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x
00000004502798 @tempfile=#<File:/tmp/RackMultipart20150405-50-183sbay.gif>, @original_filename="test.gif", @content_type="image/gif", @headers="Content-
Disposition: form-data; name=\"topic[posts_attributes][0][photos_attributes][0][attachment]\"; filename=\"test.gif\"\r\nContent-Type: imag

The way DelayedJob works is, it serializes the model, sticks it into the db … till it can be processed. Then it re-marshals it … but this process is fragile.

For example, this bit in the is_animated_gif? method: attachment.queued_for_write[:original]

… that “queued_for_write” … Paperclip stores the uploaded image in a Tmpfile until it gets uploaded to S3. This is not something that could be serialized. The Tmpfile is probably deleted before the DelayedJob runs. Also, I remember during one Paperclip upgrade, they broke that method … I don’t think it works the same way in newer versions.