2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2012-01-25 13:32:51 -08:00
|
|
|
require 'sidekiq/client'
|
2012-06-16 20:56:55 -07:00
|
|
|
require 'sidekiq/core_ext'
|
2012-01-23 14:05:03 -08:00
|
|
|
|
2012-01-16 20:05:38 -08:00
|
|
|
module Sidekiq
|
|
|
|
|
2012-01-25 13:32:51 -08: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-03 22:05:37 -07:00
|
|
|
attr_accessor :jid
|
|
|
|
|
2012-01-25 13:53:00 -08:00
|
|
|
def self.included(base)
|
2015-07-13 21:55:24 -07:00
|
|
|
raise ArgumentError, "You cannot include Sidekiq::Worker in an ActiveJob: #{base.name}" if base.ancestors.any? {|c| c.name == 'ActiveJob::Base' }
|
|
|
|
|
2012-01-25 13:53:00 -08:00
|
|
|
base.extend(ClassMethods)
|
2012-06-16 20:56:55 -07:00
|
|
|
base.class_attribute :sidekiq_options_hash
|
2013-06-25 21:10:46 -07:00
|
|
|
base.class_attribute :sidekiq_retry_in_block
|
2013-06-26 13:48:24 -04:00
|
|
|
base.class_attribute :sidekiq_retries_exhausted_block
|
2012-01-25 13:53:00 -08:00
|
|
|
end
|
|
|
|
|
2012-05-12 14:00:42 -07:00
|
|
|
def logger
|
2012-05-15 19:44:35 -07:00
|
|
|
Sidekiq.logger
|
2012-05-12 14:00:42 -07:00
|
|
|
end
|
|
|
|
|
2012-01-25 13:53:00 -08:00
|
|
|
module ClassMethods
|
2012-10-27 12:48:34 -07:00
|
|
|
|
2015-10-12 07:53:10 -07:00
|
|
|
def delay(*args)
|
|
|
|
raise ArgumentError, "Do not call .delay on a Sidekiq::Worker class, call .perform_async"
|
|
|
|
end
|
|
|
|
|
|
|
|
def delay_for(*args)
|
|
|
|
raise ArgumentError, "Do not call .delay_for on a Sidekiq::Worker class, call .perform_in"
|
|
|
|
end
|
|
|
|
|
|
|
|
def delay_until(*args)
|
|
|
|
raise ArgumentError, "Do not call .delay_until on a Sidekiq::Worker class, call .perform_at"
|
|
|
|
end
|
|
|
|
|
2016-01-18 10:58:58 -05:00
|
|
|
def set(options)
|
|
|
|
Thread.current[:sidekiq_worker_set] = options
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-01-25 13:53:00 -08:00
|
|
|
def perform_async(*args)
|
2012-06-28 08:46:18 +01:00
|
|
|
client_push('class' => self, 'args' => args)
|
2012-01-25 13:53:00 -08:00
|
|
|
end
|
2012-02-10 20:20:01 -08:00
|
|
|
|
2015-12-04 11:03:16 -08:00
|
|
|
# +interval+ must be a timestamp, numeric or something that acts
|
|
|
|
# numeric (like an activesupport time interval).
|
2012-05-25 20:21:42 -07:00
|
|
|
def perform_in(interval, *args)
|
2013-09-29 14:24:25 -07:00
|
|
|
int = interval.to_f
|
2016-09-30 17:31:08 +08:00
|
|
|
now = Time.now.to_f
|
|
|
|
ts = (int < 1_000_000_000 ? now + int : int)
|
2013-09-29 14:24:25 -07:00
|
|
|
|
|
|
|
item = { 'class' => self, 'args' => args, 'at' => ts }
|
|
|
|
|
|
|
|
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
2016-09-30 17:31:08 +08:00
|
|
|
item.delete('at'.freeze) if ts <= now
|
2013-09-29 14:24:25 -07:00
|
|
|
|
|
|
|
client_push(item)
|
2012-05-25 20:21:42 -07:00
|
|
|
end
|
|
|
|
alias_method :perform_at, :perform_in
|
|
|
|
|
2012-04-01 19:53:45 -07:00
|
|
|
##
|
|
|
|
# Allows customization for this type of Worker.
|
|
|
|
# Legal options:
|
|
|
|
#
|
2016-01-18 10:59:09 -05:00
|
|
|
# queue - use a named queue for this Worker, default 'default'
|
|
|
|
# retry - enable the RetryJobs middleware for this Worker, *true* to use the default
|
|
|
|
# or *Integer* count
|
|
|
|
# backtrace - whether to save any error backtrace in the retry payload to display in web UI,
|
2012-04-27 20:25:46 -07:00
|
|
|
# can be true, false or an integer number of lines to save, default *false*
|
2016-01-18 10:59:09 -05:00
|
|
|
# pool - use the given Redis connection pool to push this type of job to a given shard.
|
|
|
|
#
|
|
|
|
# In practice, any option is allowed. This is the main mechanism to configure the
|
|
|
|
# options for a specific job.
|
2012-04-01 19:53:45 -07:00
|
|
|
def sidekiq_options(opts={})
|
2015-04-05 03:17:13 +03:00
|
|
|
self.sidekiq_options_hash = get_sidekiq_options.merge(opts.stringify_keys)
|
2012-04-01 19:53:45 -07:00
|
|
|
end
|
|
|
|
|
2013-06-25 11:07:45 -04:00
|
|
|
def sidekiq_retry_in(&block)
|
2013-06-25 21:10:46 -07:00
|
|
|
self.sidekiq_retry_in_block = block
|
2013-06-25 11:07:45 -04:00
|
|
|
end
|
|
|
|
|
2013-06-26 13:48:24 -04:00
|
|
|
def sidekiq_retries_exhausted(&block)
|
|
|
|
self.sidekiq_retries_exhausted_block = block
|
|
|
|
end
|
|
|
|
|
2012-04-01 19:53:45 -07:00
|
|
|
def get_sidekiq_options # :nodoc:
|
2013-09-07 18:54:13 +02:00
|
|
|
self.sidekiq_options_hash ||= Sidekiq.default_worker_options
|
2012-04-01 19:53:45 -07:00
|
|
|
end
|
|
|
|
|
2012-10-18 12:00:54 +08:00
|
|
|
def client_push(item) # :nodoc:
|
2014-03-26 20:35:57 -07:00
|
|
|
pool = Thread.current[:sidekiq_via_pool] || get_sidekiq_options['pool'] || Sidekiq.redis_pool
|
2016-01-18 10:58:58 -05:00
|
|
|
hash = if Thread.current[:sidekiq_worker_set]
|
|
|
|
x, Thread.current[:sidekiq_worker_set] = Thread.current[:sidekiq_worker_set], nil
|
|
|
|
x.stringify_keys.merge(item.stringify_keys)
|
|
|
|
else
|
|
|
|
item.stringify_keys
|
|
|
|
end
|
|
|
|
Sidekiq::Client.new(pool).push(hash)
|
2012-06-28 08:46:18 +01:00
|
|
|
end
|
|
|
|
|
2012-01-16 20:05:38 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|