Skip to content

Instantly share code, notes, and snippets.

@utgarda
Created July 11, 2012 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utgarda/3090987 to your computer and use it in GitHub Desktop.
Save utgarda/3090987 to your computer and use it in GitHub Desktop.
require 'sidekiq'
require 'securerandom'
require 'active_support'
# If your client is single-threaded, we just need a single connection in our Redis connection pool
class GenerateUUID
def call(worker_class, msg, queue)
puts "Here goes middleware, worker_class = #{worker_class}, msg = #{msg}, queue = #{queue}"
if (last_arg = msg['args'].last).is_a? Proc
last_arg.call SecureRandom.uuid
end
yield
end
end
Sidekiq.configure_client do |config|
config.redis = { :namespace => 'x', :size => 1, :url => 'redis://localhost:6379' }
config.client_middleware do |chain|
chain.add GenerateUUID
end
end
# Sidekiq server is multi-threaded so our Redis connection pool size defaults to concurrency (-c)
Sidekiq.configure_server do |config|
config.redis = { :namespace => 'x', :url => 'redis://localhost:6379' }
end
#Sidekiq.configure_client do |config|
#end
module Status
extend ActiveSupport::Concern
module ClassMethods
def self.extended(base)
class << base
alias_method_chain :perform_async, :uuid
end
end
def perform_async_with_uuid(*args)
puts "async_with_uuid ----------------------------------"
perform_async_without_uuid(*args)
end
end
end
# Start up sidekiq via
# ./bin/sidekiq -r ./examples/por.rb
# and then you can open up an IRB session like so:
# irb -r ./examples/por.rb
# where you can then say
# PlainOldRuby.perform_async "like a dog", 3
#
class PlainOldRuby
include Sidekiq::Worker
include Status
def perform(how_hard="super hard", how_long=1, lambda = nil)
sleep how_long
puts "Workin' #{how_hard}"
p = Proc.new {|n| puts n }
p.call SecureRandom.uuid
puts "lambda param: #{lambda}, its class: #{lambda.class}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment