Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sidekiq::Job #4956

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions Changes.md
Expand Up @@ -5,6 +5,15 @@
HEAD
---------

- Add `Sidekiq::Job` alias for `Sidekiq::Worker` [#4995]
mperham marked this conversation as resolved.
Show resolved Hide resolved
`Sidekiq::Job` is recommended for future code; it is functionally
identical to `Sidekiq::Worker` but better reflects current terminology.
There is no plan to deprecate `Sidekiq::Worker` at this time.
```ruby
class MyJob
include Sidekiq::Job
sidekiq_options ...
```
- Minimize scheduler load on Redis at scale [#4882]
- Improve logging of delay jobs [#4904, BuonOno]
- Minor CSS improvements for buttons and tables, design PRs always welcome!
Expand Down
6 changes: 3 additions & 3 deletions lib/generators/sidekiq/templates/worker.rb.erb
@@ -1,9 +1,9 @@
<% module_namespacing do -%>
class <%= class_name %>Worker
include Sidekiq::Worker
class <%= class_name %>Job
include Sidekiq::Job

def perform(*args)
# Do something
end
end
<% end -%>
<% end -%>
2 changes: 1 addition & 1 deletion lib/generators/sidekiq/templates/worker_spec.rb.erb
@@ -1,6 +1,6 @@
require 'rails_helper'
<% module_namespacing do -%>
RSpec.describe <%= class_name %>Worker, type: :worker do
RSpec.describe <%= class_name %>Job, type: :worker do
pending "add some examples to (or delete) #{__FILE__}"
end
<% end -%>
2 changes: 1 addition & 1 deletion lib/generators/sidekiq/templates/worker_test.rb.erb
@@ -1,6 +1,6 @@
require 'test_helper'
<% module_namespacing do -%>
class <%= class_name %>WorkerTest < Minitest::Test
class <%= class_name %>JobTest < Minitest::Test
def test_example
skip "add some examples to (or delete) #{__FILE__}"
end
Expand Down
12 changes: 6 additions & 6 deletions lib/generators/sidekiq/worker_generator.rb
Expand Up @@ -3,16 +3,16 @@
module Sidekiq
module Generators # :nodoc:
class WorkerGenerator < ::Rails::Generators::NamedBase # :nodoc:
desc "This generator creates a Sidekiq Worker in app/workers and a corresponding test"
desc "This generator creates a Sidekiq::Job in app/workers and a corresponding test"

check_class_collision suffix: "Worker"
check_class_collision suffix: "Job"

def self.default_generator_root
File.dirname(__FILE__)
end

def create_worker_file
template "worker.rb.erb", File.join("app/workers", class_path, "#{file_name}_worker.rb")
template "worker.rb.erb", File.join("app/workers", class_path, "#{file_name}_job.rb")
end

def create_test_file
Expand All @@ -31,7 +31,7 @@ def create_worker_spec
template_file = File.join(
"spec/workers",
class_path,
"#{file_name}_worker_spec.rb"
"#{file_name}_job_spec.rb"
)
template "worker_spec.rb.erb", template_file
end
Expand All @@ -40,13 +40,13 @@ def create_worker_test
template_file = File.join(
"test/workers",
class_path,
"#{file_name}_worker_test.rb"
"#{file_name}_job_test.rb"
)
template "worker_test.rb.erb", template_file
end

def file_name
@_file_name ||= super.sub(/_?worker\z/i, "")
@_file_name ||= super.sub(/_?job\z/i, "")
end

def test_framework
Expand Down
16 changes: 9 additions & 7 deletions lib/sidekiq.rb
Expand Up @@ -38,7 +38,9 @@ module Sidekiq
reloader: proc { |&block| block.call }
}

DEFAULT_WORKER_OPTIONS = {
# Do not use this constant, use:
# Sidekiq.default_job_options = { ...options... }
DEFAULT_JOB_OPTIONS = {
"retry" => true,
"queue" => "default"
}
Expand Down Expand Up @@ -154,13 +156,13 @@ def self.default_server_middleware
Middleware::Chain.new
end

def self.default_worker_options=(hash)
def self.default_job_options=(hash)
# stringify
@default_worker_options = default_worker_options.merge(hash.transform_keys(&:to_s))
@default_job_options = default_job_options.merge(hash.transform_keys(&:to_s))
end

def self.default_worker_options
defined?(@default_worker_options) ? @default_worker_options : DEFAULT_WORKER_OPTIONS
def self.default_job_options
defined?(@default_job_options) ? @default_job_options : DEFAULT_JOB_OPTIONS
end

##
Expand Down Expand Up @@ -252,10 +254,10 @@ def self.on(event, &block)

# We are shutting down Sidekiq but what about workers that
# are working on some long job? This error is
# raised in workers that have not finished within the hard
# raised in jobs that have not finished within the hard
# timeout limit. This is needed to rollback db transactions,
# otherwise Ruby's Thread#kill will commit. See #377.
# DO NOT RESCUE THIS ERROR IN YOUR WORKERS
# DO NOT RESCUE THIS ERROR IN YOUR JOBS
class Shutdown < Interrupt; end
end

Expand Down
10 changes: 5 additions & 5 deletions lib/sidekiq/api.rb
Expand Up @@ -205,7 +205,7 @@ def date_stat_hash(stat)
#
# queue = Sidekiq::Queue.new("mailer")
# queue.each do |job|
# job.klass # => 'MyWorker'
# job.klass # => 'MyJob'
# job.args # => [1, 2, 3]
# job.delete if job.jid == 'abcdef1234567890'
# end
Expand Down Expand Up @@ -267,7 +267,7 @@ def each
break if entries.empty?
page += 1
entries.each do |entry|
yield Job.new(entry, @name)
yield JobRecord.new(entry, @name)
end
deleted_size = initial_size - size
end
Expand Down Expand Up @@ -298,9 +298,9 @@ def clear
# sorted set.
#
# The job should be considered immutable but may be
# removed from the queue via Job#delete.
# removed from the queue via JobRecord#delete.
#
class Job
class JobRecord
attr_reader :item
attr_reader :value

Expand Down Expand Up @@ -457,7 +457,7 @@ def uncompress_backtrace(backtrace)
end
end

class SortedEntry < Job
class SortedEntry < JobRecord
attr_reader :score
attr_reader :parent

Expand Down
4 changes: 2 additions & 2 deletions lib/sidekiq/cli.rb
Expand Up @@ -287,7 +287,7 @@ def validate!
(File.directory?(options[:require]) && !File.exist?("#{options[:require]}/config/application.rb"))
logger.info "=================================================================="
logger.info " Please point Sidekiq to a Rails application or a Ruby file "
logger.info " to load your worker classes with -r [DIR|FILE]."
logger.info " to load your job classes with -r [DIR|FILE]."
logger.info "=================================================================="
logger.info @parser
die(1)
Expand Down Expand Up @@ -328,7 +328,7 @@ def option_parser(opts)
parse_queue opts, queue, weight
end

o.on "-r", "--require [PATH|DIR]", "Location of Rails application with workers or file to require" do |arg|
o.on "-r", "--require [PATH|DIR]", "Location of Rails application with jobs or file to require" do |arg|
opts[:require] = arg
end

Expand Down
32 changes: 16 additions & 16 deletions lib/sidekiq/client.rb
Expand Up @@ -12,7 +12,7 @@ class Client
# client.middleware do |chain|
# chain.use MyClientMiddleware
# end
# client.push('class' => 'SomeWorker', 'args' => [1,2,3])
# client.push('class' => 'SomeJob', 'args' => [1,2,3])
#
# All client instances default to the globally-defined
# Sidekiq.client_middleware but you can change as necessary.
Expand Down Expand Up @@ -46,16 +46,16 @@ def initialize(redis_pool = nil)
# The main method used to push a job to Redis. Accepts a number of options:
#
# queue - the named queue to use, default 'default'
# class - the worker class to call, required
# class - the job class to call, required
# args - an array of simple arguments to the perform method, must be JSON-serializable
# at - timestamp to schedule the job (optional), must be Numeric (e.g. Time.now.to_f)
# retry - whether to retry this job if it fails, default true or an integer number of retries
# backtrace - whether to save any error backtrace, default false
#
# If class is set to the class name, the jobs' options will be based on Sidekiq's default
# worker options. Otherwise, they will be based on the job class's options.
# job options. Otherwise, they will be based on the job class's options.
#
# Any options valid for a worker class's sidekiq_options are also available here.
# Any options valid for a job class's sidekiq_options are also available here.
#
# All options must be strings, not symbols. NB: because we are serializing to JSON, all
# symbols in 'args' will be converted to strings. Note that +backtrace: true+ can take quite a bit of
Expand All @@ -64,7 +64,7 @@ def initialize(redis_pool = nil)
# Returns a unique Job ID. If middleware stops the job, nil will be returned instead.
#
# Example:
# push('queue' => 'my_queue', 'class' => MyWorker, 'args' => ['foo', 1, :bat => 'bar'])
# push('queue' => 'my_queue', 'class' => MyJob, 'args' => ['foo', 1, :bat => 'bar'])
#
def push(item)
normed = normalize_item(item)
Expand Down Expand Up @@ -116,8 +116,8 @@ def push_bulk(items)
#
# pool = ConnectionPool.new { Redis.new }
# Sidekiq::Client.via(pool) do
# SomeWorker.perform_async(1,2,3)
# SomeOtherWorker.perform_async(1,2,3)
# SomeJob.perform_async(1,2,3)
# SomeOtherJob.perform_async(1,2,3)
# end
#
# Generally this is only needed for very large Sidekiq installs processing
Expand All @@ -142,10 +142,10 @@ def push_bulk(items)
end

# Resque compatibility helpers. Note all helpers
# should go through Worker#client_push.
# should go through Job#client_push.
#
# Example usage:
# Sidekiq::Client.enqueue(MyWorker, 'foo', 1, :bat => 'bar')
# Sidekiq::Client.enqueue(MyJob, 'foo', 1, :bat => 'bar')
#
# Messages are enqueued to the 'default' queue.
#
Expand All @@ -154,14 +154,14 @@ def enqueue(klass, *args)
end

# Example usage:
# Sidekiq::Client.enqueue_to(:queue_name, MyWorker, 'foo', 1, :bat => 'bar')
# Sidekiq::Client.enqueue_to(:queue_name, MyJob, 'foo', 1, :bat => 'bar')
#
def enqueue_to(queue, klass, *args)
klass.client_push("queue" => queue, "class" => klass, "args" => args)
end

# Example usage:
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyWorker, 'foo', 1, :bat => 'bar')
# Sidekiq::Client.enqueue_to_in(:queue_name, 3.minutes, MyJob, 'foo', 1, :bat => 'bar')
#
def enqueue_to_in(queue, interval, klass, *args)
int = interval.to_f
Expand All @@ -175,7 +175,7 @@ def enqueue_to_in(queue, interval, klass, *args)
end

# Example usage:
# Sidekiq::Client.enqueue_in(3.minutes, MyWorker, 'foo', 1, :bat => 'bar')
# Sidekiq::Client.enqueue_in(3.minutes, MyJob, 'foo', 1, :bat => 'bar')
#
def enqueue_in(interval, klass, *args)
klass.perform_in(interval, *args)
Expand Down Expand Up @@ -211,10 +211,10 @@ def atomic_push(conn, payloads)
end
end

def process_single(worker_class, item)
def process_single(job_class, item)
queue = item["queue"]

middleware.invoke(worker_class, item, queue, @redis_pool) do
middleware.invoke(job_class, item, queue, @redis_pool) do
item
end
end
Expand Down Expand Up @@ -249,10 +249,10 @@ def normalize_item(item)

def normalized_hash(item_class)
if item_class.is_a?(Class)
raise(ArgumentError, "Message must include a Sidekiq::Worker class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?("get_sidekiq_options")
raise(ArgumentError, "Message must include a Sidekiq::Job class, not class name: #{item_class.ancestors.inspect}") unless item_class.respond_to?("get_sidekiq_options")
item_class.get_sidekiq_options
else
Sidekiq.default_worker_options
Sidekiq.default_job_options
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/extensions/action_mailer.rb
Expand Up @@ -13,7 +13,7 @@ module Extensions
# UserMailer.delay_for(5.days).send_welcome_email(new_user)
# UserMailer.delay_until(5.days.from_now).send_welcome_email(new_user)
class DelayedMailer
include Sidekiq::Worker
include Sidekiq::Job

def perform(yml)
(target, method_name, args) = YAML.load(yml)
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/extensions/active_record.rb
Expand Up @@ -15,7 +15,7 @@ module Extensions
# object to Redis. Your Sidekiq jobs should pass IDs, not entire instances.
# This is here for backwards compatibility with Delayed::Job only.
class DelayedModel
include Sidekiq::Worker
include Sidekiq::Job

def perform(yml)
(target, method_name, args) = YAML.load(yml)
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/extensions/class_methods.rb
Expand Up @@ -13,7 +13,7 @@ module Extensions
# Wikipedia.delay.download_changes_for(Date.today)
#
class DelayedClass
include Sidekiq::Worker
include Sidekiq::Job

def perform(yml)
(target, method_name, args) = YAML.load(yml)
Expand Down