Skip to content

Instantly share code, notes, and snippets.

@andreaseger
Created September 6, 2011 14:17

Revisions

  1. andreaseger created this gist Sep 6, 2011.
    10 changes: 10 additions & 0 deletions config-initializers-resque.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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
    3 changes: 3 additions & 0 deletions config-resque.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    development: localhost:6379:6
    test: localhost:6379:12
    production: localhost:6379:3
    30 changes: 30 additions & 0 deletions lib-carrierwave_resque_job.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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
    4 changes: 4 additions & 0 deletions lib-tasks-resque.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    require "resque/tasks"

    #to load the rails env in every worker
    task "resque:setup" => :environment
    6 changes: 6 additions & 0 deletions more_stuff.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    ## 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'

    19 changes: 19 additions & 0 deletions worker-image_processor.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    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