2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2012-01-23 17:05:03 -05:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
require "sidekiq/client"
|
2019-07-20 20:17:15 -04:00
|
|
|
require "sidekiq/worker/options"
|
2012-01-16 23:05:38 -05:00
|
|
|
|
2019-04-01 12:20:41 -04: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:
|
|
|
|
#
|
2018-12-06 09:10:48 -05:00
|
|
|
# class HardWorker
|
|
|
|
# include Sidekiq::Worker
|
2012-01-25 16:32:51 -05:00
|
|
|
#
|
2018-12-06 09:10:48 -05:00
|
|
|
# def perform(*args)
|
|
|
|
# # do some work
|
|
|
|
# end
|
2012-01-25 16:32:51 -05:00
|
|
|
# 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)
|
2019-07-20 20:17:15 -04:00
|
|
|
raise ArgumentError, "You can only include Sidekiq::Worker::Options in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
|
2015-07-14 00:55:24 -04:00
|
|
|
|
2019-07-20 20:17:15 -04:00
|
|
|
base.include(Options)
|
2012-01-25 16:53:00 -05:00
|
|
|
base.extend(ClassMethods)
|
|
|
|
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
|
|
|
|
|
2017-04-05 13:56:06 -04:00
|
|
|
# This helper class encapsulates the set options for `set`, e.g.
|
|
|
|
#
|
|
|
|
# SomeWorker.set(queue: 'foo').perform_async(....)
|
|
|
|
#
|
|
|
|
class Setter
|
2017-09-12 13:49:05 -04:00
|
|
|
def initialize(klass, opts)
|
|
|
|
@klass = klass
|
2017-04-05 13:56:06 -04:00
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
2019-03-04 14:16:58 -05:00
|
|
|
def set(options)
|
|
|
|
@opts.merge!(options)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-04-05 13:56:06 -04:00
|
|
|
def perform_async(*args)
|
2019-04-01 12:20:41 -04:00
|
|
|
@klass.client_push(@opts.merge("args" => args, "class" => @klass))
|
2017-04-05 13:56:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# +interval+ must be a timestamp, numeric or something that acts
|
|
|
|
# numeric (like an activesupport time interval).
|
|
|
|
def perform_in(interval, *args)
|
|
|
|
int = interval.to_f
|
|
|
|
now = Time.now.to_f
|
|
|
|
ts = (int < 1_000_000_000 ? now + int : int)
|
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
payload = @opts.merge("class" => @klass, "args" => args, "at" => ts)
|
2017-04-05 13:56:06 -04:00
|
|
|
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
2019-04-01 12:20:41 -04:00
|
|
|
payload.delete("at") if ts <= now
|
2017-09-12 13:49:05 -04:00
|
|
|
@klass.client_push(payload)
|
2017-04-05 13:56:06 -04:00
|
|
|
end
|
|
|
|
alias_method :perform_at, :perform_in
|
|
|
|
end
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
module ClassMethods
|
2015-10-12 10:53:10 -04: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)
|
2017-09-12 13:49:05 -04:00
|
|
|
Setter.new(self, options)
|
2016-01-18 10:58:58 -05:00
|
|
|
end
|
|
|
|
|
2012-01-25 16:53:00 -05:00
|
|
|
def perform_async(*args)
|
2019-04-01 12:20:41 -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
|
|
|
|
2015-12-04 14:03:16 -05:00
|
|
|
# +interval+ must be a timestamp, numeric or something that acts
|
|
|
|
# numeric (like an activesupport time interval).
|
2012-05-25 23:21:42 -04:00
|
|
|
def perform_in(interval, *args)
|
2013-09-29 17:24:25 -04:00
|
|
|
int = interval.to_f
|
2016-09-30 05:31:08 -04:00
|
|
|
now = Time.now.to_f
|
|
|
|
ts = (int < 1_000_000_000 ? now + int : int)
|
2013-09-29 17:24:25 -04:00
|
|
|
|
2019-04-01 12:20:41 -04:00
|
|
|
item = {"class" => self, "args" => args, "at" => ts}
|
2013-09-29 17:24:25 -04:00
|
|
|
|
|
|
|
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
2019-04-01 12:20:41 -04:00
|
|
|
item.delete("at") if ts <= now
|
2013-09-29 17:24:25 -04:00
|
|
|
|
|
|
|
client_push(item)
|
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:
|
|
|
|
#
|
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 23:25:46 -04: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.
|
2019-04-01 12:20:41 -04:00
|
|
|
def sidekiq_options(opts = {})
|
2019-07-20 20:17:15 -04:00
|
|
|
super
|
2012-04-01 22:53:45 -04:00
|
|
|
end
|
|
|
|
|
2012-10-18 00:00:54 -04:00
|
|
|
def client_push(item) # :nodoc:
|
2019-04-01 12:20:41 -04:00
|
|
|
pool = Thread.current[:sidekiq_via_pool] || get_sidekiq_options["pool"] || Sidekiq.redis_pool
|
2017-05-13 01:03:32 -04:00
|
|
|
# stringify
|
|
|
|
item.keys.each do |key|
|
|
|
|
item[key.to_s] = item.delete(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
Sidekiq::Client.new(pool).push(item)
|
2012-06-28 03:46:18 -04:00
|
|
|
end
|
2012-01-16 23:05:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|