2012-01-25 16:32:51 -05:00
|
|
|
require 'sidekiq/client'
|
2012-06-16 23:56:55 -04:00
|
|
|
require 'sidekiq/core_ext'
|
2012-01-23 17:05:03 -05:00
|
|
|
|
2012-01-16 23:05:38 -05:00
|
|
|
module Sidekiq
|
|
|
|
|
2012-01-25 16:32:51 -05:00
|
|
|
##
|
|
|
|
# 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
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
2012-06-16 23:56:55 -04:00
|
|
|
base.class_attribute :sidekiq_options_hash
|
2012-01-25 16:53:00 -05:00
|
|
|
end
|
|
|
|
|
2012-05-12 17:00:42 -04:00
|
|
|
def logger
|
2012-05-15 22:44:35 -04:00
|
|
|
Sidekiq.logger
|
2012-05-12 17:00:42 -04:00
|
|
|
end
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
module ClassMethods
|
2012-10-27 15:48:34 -04:00
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
def perform_async(*args)
|
2012-06-28 03:46:18 -04:00
|
|
|
client_push('class' => self, 'args' => args)
|
2012-01-25 16:53:00 -05:00
|
|
|
end
|
2012-02-10 23:20:01 -05:00
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
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)
|
2012-05-25 23:21:42 -04:00
|
|
|
end
|
|
|
|
alias_method :perform_at, :perform_in
|
|
|
|
|
2012-04-01 22:53:45 -04:00
|
|
|
##
|
|
|
|
# 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*
|
2012-04-26 11:40:07 -04:00
|
|
|
# :timeout - timeout the perform method after N seconds, default *nil*
|
2012-04-27 23:25:46 -04:00
|
|
|
# :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*
|
2012-04-01 22:53:45 -04:00
|
|
|
def sidekiq_options(opts={})
|
2012-10-27 15:48:34 -04:00
|
|
|
self.sidekiq_options_hash = get_sidekiq_options.merge((opts || {}).stringify_keys)
|
2012-04-01 22:53:45 -04:00
|
|
|
end
|
|
|
|
|
2012-07-20 23:11:16 -04:00
|
|
|
DEFAULT_OPTIONS = { 'retry' => true, 'queue' => 'default' }
|
2012-06-16 23:56:55 -04:00
|
|
|
|
2012-04-01 22:53:45 -04:00
|
|
|
def get_sidekiq_options # :nodoc:
|
2012-06-16 23:56:55 -04:00
|
|
|
self.sidekiq_options_hash ||= DEFAULT_OPTIONS
|
2012-04-01 22:53:45 -04:00
|
|
|
end
|
|
|
|
|
2012-10-18 00:00:54 -04:00
|
|
|
def client_push(item) # :nodoc:
|
2012-10-27 15:48:34 -04:00
|
|
|
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
|