1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/worker.rb
Mike Perham 2080412119 Client API update:
- Add API for configuring options per Worker class
- Removed the Client API issues preventing it working on Ruby 1.8
- Cleanups to various APIs for upcoming 1.0 release.
2012-04-01 19:53:45 -07:00

60 lines
1.6 KiB
Ruby

require 'sidekiq/client'
module Sidekiq
##
# Include this module in your worker class and you can easily create
# asynchronous jobs:
#
# class HardWorker
# include Sidekiq::Worker
#
# def perform(*args)
# # do some work
# end
# end
#
# Then in your Rails app, you can do this:
#
# HardWorker.perform_async(1, 2, 3)
#
# Note that perform_async is a class method, perform is an instance method.
module Worker
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def perform_async(*args)
Sidekiq::Client.push('class' => self, 'args' => args)
end
def queue(name)
puts "DEPRECATED: `queue :name` is now `sidekiq_options :queue => :name`"
Sidekiq::Client.queue_mappings[self.name] = name.to_s
end
##
# Allows customization for this type of Worker.
# Legal options:
#
# :unique - enable the UniqueJobs middleware for this Worker, default *true*
# :queue - use a named queue for this Worker, default 'default'
# :retry - enable the RetryJobs middleware for this Worker, default *true*
def sidekiq_options(opts={})
@sidekiq_options = get_sidekiq_options.merge(stringify_keys(opts || {}))
end
def get_sidekiq_options # :nodoc:
@sidekiq_options || { 'unique' => true, 'retry' => true, 'queue' => 'default' }
end
def stringify_keys(hash) # :nodoc:
hash.keys.each do |key|
hash[key.to_s] = hash.delete(key)
end
hash
end
end
end
end