1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Swap Sidekiq::Worker and Sidekiq::Job

This commit is contained in:
Adam Niedzielski 2022-03-24 06:24:58 +01:00 committed by Mike Perham
parent b17ac3e03f
commit cea11b5077
4 changed files with 28 additions and 28 deletions

View file

@ -5,8 +5,8 @@ fail "Sidekiq #{Sidekiq::VERSION} does not support Ruby versions below 2.7.0." i
require "sidekiq/logger" require "sidekiq/logger"
require "sidekiq/client" require "sidekiq/client"
require "sidekiq/worker"
require "sidekiq/job" require "sidekiq/job"
require "sidekiq/worker_compatibility_alias"
require "sidekiq/redis_connection" require "sidekiq/redis_connection"
require "json" require "json"

View file

@ -4,11 +4,11 @@ require "sidekiq/client"
module Sidekiq module Sidekiq
## ##
# Include this module in your worker class and you can easily create # Include this module in your job class and you can easily create
# asynchronous jobs: # asynchronous jobs:
# #
# class HardWorker # class HardJob
# include Sidekiq::Worker # include Sidekiq::Job
# sidekiq_options queue: 'critical', retry: 5 # sidekiq_options queue: 'critical', retry: 5
# #
# def perform(*args) # def perform(*args)
@ -18,22 +18,22 @@ module Sidekiq
# #
# Then in your Rails app, you can do this: # Then in your Rails app, you can do this:
# #
# HardWorker.perform_async(1, 2, 3) # HardJob.perform_async(1, 2, 3)
# #
# Note that perform_async is a class method, perform is an instance method. # Note that perform_async is a class method, perform is an instance method.
# #
# Sidekiq::Worker also includes several APIs to provide compatibility with # Sidekiq::Job also includes several APIs to provide compatibility with
# ActiveJob. # ActiveJob.
# #
# class SomeWorker # class SomeJob
# include Sidekiq::Worker # include Sidekiq::Job
# queue_as :critical # queue_as :critical
# #
# def perform(...) # def perform(...)
# end # end
# end # end
# #
# SomeWorker.set(wait_until: 1.hour).perform_async(123) # SomeJob.set(wait_until: 1.hour).perform_async(123)
# #
# Note that arguments passed to the job must still obey Sidekiq's # Note that arguments passed to the job must still obey Sidekiq's
# best practice for simple, JSON-native data types. Sidekiq will not # best practice for simple, JSON-native data types. Sidekiq will not
@ -41,7 +41,7 @@ module Sidekiq
# this reason, we don't implement `perform_later` as our call semantics # this reason, we don't implement `perform_later` as our call semantics
# are very different. # are very different.
# #
module Worker module Job
## ##
# The Options module is extracted so we can include it in ActiveJob::Base # The Options module is extracted so we can include it in ActiveJob::Base
# and allow native AJs to configure Sidekiq features/internals. # and allow native AJs to configure Sidekiq features/internals.
@ -57,11 +57,11 @@ module Sidekiq
ACCESSOR_MUTEX = Mutex.new ACCESSOR_MUTEX = Mutex.new
## ##
# Allows customization for this type of Worker. # Allows customization for this type of Job.
# Legal options: # Legal options:
# #
# queue - name of queue to use for this job type, default *default* # queue - name of queue to use for this job type, default *default*
# retry - enable retries for this Worker in case of error during execution, # retry - enable retries for this Job in case of error during execution,
# *true* to use the default or *Integer* count # *true* to use the default or *Integer* count
# backtrace - whether to save any error backtrace in the retry payload to display in web UI, # 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* # can be true, false or an integer number of lines to save, default *false*
@ -156,7 +156,7 @@ module Sidekiq
attr_accessor :jid attr_accessor :jid
def self.included(base) def self.included(base)
raise ArgumentError, "Sidekiq::Worker cannot be included in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" } raise ArgumentError, "Sidekiq::Job cannot be included in an ActiveJob: #{base.name}" if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
base.include(Options) base.include(Options)
base.extend(ClassMethods) base.extend(ClassMethods)
@ -168,7 +168,7 @@ module Sidekiq
# This helper class encapsulates the set options for `set`, e.g. # This helper class encapsulates the set options for `set`, e.g.
# #
# SomeWorker.set(queue: 'foo').perform_async(....) # SomeJob.set(queue: 'foo').perform_async(....)
# #
class Setter class Setter
include Sidekiq::JobUtil include Sidekiq::JobUtil
@ -266,15 +266,15 @@ module Sidekiq
module ClassMethods module ClassMethods
def delay(*args) def delay(*args)
raise ArgumentError, "Do not call .delay on a Sidekiq::Worker class, call .perform_async" raise ArgumentError, "Do not call .delay on a Sidekiq::Job class, call .perform_async"
end end
def delay_for(*args) def delay_for(*args)
raise ArgumentError, "Do not call .delay_for on a Sidekiq::Worker class, call .perform_in" raise ArgumentError, "Do not call .delay_for on a Sidekiq::Job class, call .perform_in"
end end
def delay_until(*args) def delay_until(*args)
raise ArgumentError, "Do not call .delay_until on a Sidekiq::Worker class, call .perform_at" raise ArgumentError, "Do not call .delay_until on a Sidekiq::Job class, call .perform_at"
end end
def queue_as(q) def queue_as(q)
@ -307,13 +307,13 @@ module Sidekiq
# #
# Example (3 Redis round trips): # Example (3 Redis round trips):
# #
# SomeWorker.perform_async(1) # SomeJob.perform_async(1)
# SomeWorker.perform_async(2) # SomeJob.perform_async(2)
# SomeWorker.perform_async(3) # SomeJob.perform_async(3)
# #
# Would instead become (1 Redis round trip): # Would instead become (1 Redis round trip):
# #
# SomeWorker.perform_bulk([[1], [2], [3]]) # SomeJob.perform_bulk([[1], [2], [3]])
# #
def perform_bulk(*args, **kwargs) def perform_bulk(*args, **kwargs)
Setter.new(self, {}).perform_bulk(*args, **kwargs) Setter.new(self, {}).perform_bulk(*args, **kwargs)
@ -336,11 +336,11 @@ module Sidekiq
alias_method :perform_at, :perform_in alias_method :perform_at, :perform_in
## ##
# Allows customization for this type of Worker. # Allows customization for this type of Job.
# Legal options: # Legal options:
# #
# queue - use a named queue for this Worker, default 'default' # queue - use a named queue for this Job, default 'default'
# retry - enable the RetryJobs middleware for this Worker, *true* to use the default # retry - enable the RetryJobs middleware for this Job, *true* to use the default
# or *Integer* count # or *Integer* count
# backtrace - whether to save any error backtrace in the retry payload to display in web UI, # 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* # can be true, false or an integer number of lines to save, default *false*

View file

@ -1,4 +1,4 @@
require "sidekiq/worker" require "sidekiq/job"
module Sidekiq module Sidekiq
# Sidekiq::Job is a new alias for Sidekiq::Worker as of Sidekiq 6.3.0. # Sidekiq::Job is a new alias for Sidekiq::Worker as of Sidekiq 6.3.0.
@ -9,5 +9,5 @@ module Sidekiq
# process a "worker". Some people call the thread that executes jobs a # process a "worker". Some people call the thread that executes jobs a
# "worker". This change brings Sidekiq closer to ActiveJob where your job # "worker". This change brings Sidekiq closer to ActiveJob where your job
# classes extend ApplicationJob. # classes extend ApplicationJob.
Job = Worker Worker = Job
end end

View file

@ -13,13 +13,13 @@ describe "ActiveJob" do
ActiveJob::Base.send(:include, ::Sidekiq::Worker::Options) unless ActiveJob::Base.respond_to?(:sidekiq_options) ActiveJob::Base.send(:include, ::Sidekiq::Worker::Options) unless ActiveJob::Base.respond_to?(:sidekiq_options)
end end
it "does not allow Sidekiq::Worker in AJ::Base classes" do it "does not allow Sidekiq::Job in AJ::Base classes" do
ex = assert_raises ArgumentError do ex = assert_raises ArgumentError do
Class.new(ActiveJob::Base) do Class.new(ActiveJob::Base) do
include Sidekiq::Worker include Sidekiq::Worker
end end
end end
assert_includes ex.message, "Sidekiq::Worker cannot be included" assert_includes ex.message, "Sidekiq::Job cannot be included"
end end
it "loads Sidekiq::Worker::Options in AJ::Base classes" do it "loads Sidekiq::Worker::Options in AJ::Base classes" do