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

79 lines
2.2 KiB
Ruby
Raw Normal View History

require 'sidekiq/client'
require 'sidekiq/core_ext'
2012-01-16 23:05:38 -05:00
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
2012-11-04 01:05:37 -04:00
attr_accessor :jid
def self.included(base)
base.extend(ClassMethods)
base.class_attribute :sidekiq_options_hash
2013-06-25 11:07:45 -04:00
base.class_attribute :sidekiq_retry_with
end
def logger
Sidekiq.logger
end
module ClassMethods
def perform_async(*args)
2012-06-28 03:46:18 -04:00
client_push('class' => self, 'args' => args)
end
def perform_in(interval, *args)
int = interval.to_f
ts = (int < 1_000_000_000 ? Time.now.to_f + int : int)
2012-06-28 03:46:18 -04:00
client_push('class' => self, 'args' => args, 'at' => ts)
end
alias_method :perform_at, :perform_in
##
# Allows customization for this type of Worker.
# Legal options:
#
# :queue - use a named queue for this Worker, default 'default'
# :retry - enable the RetryJobs middleware for this Worker, default *true*
# :backtrace - whether to save any error backtrace in the retry payload to display in web UI,
# can be true, false or an integer number of lines to save, default *false*
def sidekiq_options(opts={})
self.sidekiq_options_hash = get_sidekiq_options.merge((opts || {}).stringify_keys)
::Sidekiq.logger.warn("#{self.name} - :timeout is unsafe and support has been removed from Sidekiq, see http://bit.ly/OtYpK for details") if opts.include? :timeout
end
2013-06-25 11:07:45 -04:00
def sidekiq_retry_in(&block)
self.sidekiq_retry_with = block
end
2012-07-20 23:11:16 -04:00
DEFAULT_OPTIONS = { 'retry' => true, 'queue' => 'default' }
def get_sidekiq_options # :nodoc:
self.sidekiq_options_hash ||= DEFAULT_OPTIONS
end
2012-10-18 00:00:54 -04:00
def client_push(item) # :nodoc:
Sidekiq::Client.push(item.stringify_keys)
2012-06-28 03:46:18 -04:00
end
2012-01-16 23:05:38 -05:00
end
end
end