Skip to content

Instantly share code, notes, and snippets.

@andreaseger
Created September 6, 2011 14:17
Show Gist options
  • Save andreaseger/1197661 to your computer and use it in GitHub Desktop.
Save andreaseger/1197661 to your computer and use it in GitHub Desktop.
carrierwave resque background image processing foo
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'
resque_config = YAML.load_file(rails_root + '/config/resque.yml')
Resque.redis = resque_config[rails_env]
#secure the admin view of resque
Resque::Server.use(Rack::Auth::Basic) do |user, password|
password == "secret"
end
development: localhost:6379:6
test: localhost:6379:12
production: localhost:6379:3
module CarrierWave
module Resque
module Job
module ActiveRecordInterface
def delay_carrierwave
@delay_carrierwave ||= true
end
def delay_carrierwave=(delay)
@delay_carrierwave = delay
end
end
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def self.extended(base)
base.send(:include, InstanceMethods)
base.alias_method_chain :process!, :delay
::ActiveRecord::Base.send(:include, CarrierWave::Resque::Job::ActiveRecordInterface)
end
module InstanceMethods
def process_with_delay!(new_file)
process_without_delay!(new_file) unless model.delay_carrierwave
end
end
end
end
end
end
require "resque/tasks"
#to load the rails env in every worker
task "resque:setup" => :environment
## at some point do this(id is my picture_id)
Resque.enqueue(ImageProcessor, id)
#mount the nice user interface(add this to config/routes.rb)
mount Resque::Server.new, :at => '/resque'
class ImageProcessor
@queue = :image_proccesor_queue
def self.perform(picture_id)
picture = Picture.find(picture_id)
`convert public#{picture.image_url} -resize 64x64\\> public#{picture.image_url(:thumb)}`
end
end
### or better but didn't work or me
class ImageProcessor
@queue = :image_proccesor_queue
def self.perform(picture_id)
picture = Picture.find(picture_id)
picture.image.versions.each_pair do |key,value|
value.process_without_delay!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment