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)
|
2015-07-14 00:55:24 -04: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 16:53:00 -05:00
|
|
|
base.extend(ClassMethods)
|
2012-06-16 23:56:55 -04:00
|
|
|
base.class_attribute :sidekiq_options_hash
|
2013-06-26 00:10:46 -04: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 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
|
|
|
|
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
|
|
|
|
|
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)
|
2013-09-29 17:24:25 -04:00
|
|
|
int = interval.to_f
|
MyWorker.perform_in(1.month) does not always schedule job in one month.
At the moment, `MyWorker.perform_in(1.month)` always schedules a job in 30
days. When added to a date, `1.month` will add 1 to the date's month count,
which means that it will add 28, 29, 30, or 31 days depending on the month and
year.
When I call `MyWorker.perform_in(1.month)`, I would expect the job to be
scheduled next month, same day of the month, all the time. At the moment, it is
true only four months in the year.
My pull request tries to fix this by converting the interval object to a Float
at the last possible moment.
Plaese note that the test I wrote will fail only during months that do not have
30 days. Ideally, I would add a dependency to Timecop and freeze time to any
day in a month of 28, 29 or 31 days. This could also avoid using
`#assert_in_delta`, in favour of `#assert_equal`.
Feel free to read my blog post [Rails' `1.month` has a variable
length](http://dstosik.github.io/rails/2015/02/19/rails-1month-variable-length/)
for more details.
2015-02-19 19:25:46 -05:00
|
|
|
now = Time.now
|
|
|
|
ts = (int < 1_000_000_000 ? (now + interval).to_f : int)
|
2013-09-29 17:24:25 -04:00
|
|
|
|
|
|
|
item = { 'class' => self, 'args' => args, 'at' => ts }
|
|
|
|
|
|
|
|
# Optimization to enqueue something now that is scheduled to go out now or in the past
|
MyWorker.perform_in(1.month) does not always schedule job in one month.
At the moment, `MyWorker.perform_in(1.month)` always schedules a job in 30
days. When added to a date, `1.month` will add 1 to the date's month count,
which means that it will add 28, 29, 30, or 31 days depending on the month and
year.
When I call `MyWorker.perform_in(1.month)`, I would expect the job to be
scheduled next month, same day of the month, all the time. At the moment, it is
true only four months in the year.
My pull request tries to fix this by converting the interval object to a Float
at the last possible moment.
Plaese note that the test I wrote will fail only during months that do not have
30 days. Ideally, I would add a dependency to Timecop and freeze time to any
day in a month of 28, 29 or 31 days. This could also avoid using
`#assert_in_delta`, in favour of `#assert_equal`.
Feel free to read my blog post [Rails' `1.month` has a variable
length](http://dstosik.github.io/rails/2015/02/19/rails-1month-variable-length/)
for more details.
2015-02-19 19:25:46 -05:00
|
|
|
item.delete('at'.freeze) if ts <= now.to_f
|
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:
|
|
|
|
#
|
|
|
|
# :queue - use a named queue for this Worker, default 'default'
|
|
|
|
# :retry - enable the RetryJobs middleware for this Worker, default *true*
|
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*
|
2014-03-25 14:56:15 -04:00
|
|
|
# :pool - use the given Redis connection pool to push this type of job to a given shard.
|
2012-04-01 22:53:45 -04:00
|
|
|
def sidekiq_options(opts={})
|
2015-04-04 20:17:13 -04:00
|
|
|
self.sidekiq_options_hash = get_sidekiq_options.merge(opts.stringify_keys)
|
2012-04-01 22:53:45 -04:00
|
|
|
end
|
|
|
|
|
2013-06-25 11:07:45 -04:00
|
|
|
def sidekiq_retry_in(&block)
|
2013-06-26 00:10:46 -04: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 22:53:45 -04:00
|
|
|
def get_sidekiq_options # :nodoc:
|
2013-09-07 12:54:13 -04:00
|
|
|
self.sidekiq_options_hash ||= Sidekiq.default_worker_options
|
2012-04-01 22:53:45 -04:00
|
|
|
end
|
|
|
|
|
2012-10-18 00:00:54 -04:00
|
|
|
def client_push(item) # :nodoc:
|
2014-03-26 23:35:57 -04:00
|
|
|
pool = Thread.current[:sidekiq_via_pool] || get_sidekiq_options['pool'] || Sidekiq.redis_pool
|
2014-03-25 14:56:15 -04:00
|
|
|
Sidekiq::Client.new(pool).push(item.stringify_keys)
|
2012-06-28 03:46:18 -04:00
|
|
|
end
|
|
|
|
|
2012-01-16 23:05:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|