Skip to content

Commit

Permalink
adding type hints for sidekiq 7.0
Browse files Browse the repository at this point in the history
a lot has changed internally in sidekiq, which should be covered by these changes.
  • Loading branch information
HoneyryderChuck committed Jan 10, 2024
1 parent 13ae8cd commit 3fd4681
Show file tree
Hide file tree
Showing 26 changed files with 909 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,6 @@
[submodule "gems/json-jwt/1.16/_src"]
path = gems/json-jwt/1.16/_src
url = https://github.com/nov/json-jwt.git
[submodule "gems/sidekiq/7.0/_src"]
path = gems/sidekiq/7.0/_src
url = https://github.com/sidekiq/sidekiq.git
22 changes: 22 additions & 0 deletions gems/sidekiq/7.0/_scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

# Exit command with non-zero status code, Output logs of every command executed, Treat unset variables as an error when substituting.
set -eou pipefail
# Internal Field Separator - Linux shell variable
IFS=$'\n\t'
# Print shell input lines
set -v

# Set RBS_DIR variable to change directory to execute type checks using `steep check`
RBS_DIR=$(cd $(dirname $0)/..; pwd)
# Set REPO_DIR variable to validate RBS files added to the corresponding folder
REPO_DIR=$(cd $(dirname $0)/../../..; pwd)
# Validate RBS files, using the bundler environment present
bundle exec rbs --repo $REPO_DIR -r sidekiq:7.0 -r connection_pool -r logger:0 -r monitor:0 -r forwardable:0 -r timeout:0 validate --silent

cd ${RBS_DIR}/_test
# Run type checks
bundle exec steep check

$(git rev-parse --show-toplevel)/bin/check-untyped-call.rb
$(git rev-parse --show-toplevel)/bin/check-manifest-yaml.rb
1 change: 1 addition & 0 deletions gems/sidekiq/7.0/_src
Submodule _src added at 7df284
17 changes: 17 additions & 0 deletions gems/sidekiq/7.0/_test/Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
D = Steep::Diagnostic

target :test do
check "."
signature "."

repo_path "../../../"
library "sidekiq:7.0"
library "logger"
library "forwardable"
library "monitor"
library "timeout"
library "connection_pool"
signature "."

configure_code_diagnostics(D::Ruby.all_error)
end
21 changes: 21 additions & 0 deletions gems/sidekiq/7.0/_test/test_1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Test fundamental APIs
# Taken from: https://github.com/mperham/sidekiq/wiki/Getting-Started
class HardWorker
include Sidekiq::Job

def perform(name, count)
puts "Performing #{name} #{count} times"
end
end
HardWorker.perform_async('bob', 5)
HardWorker.perform_at(Time.now + 5*60, 'bob', 5)

class HardJob
include Sidekiq::Job

def perform(name, count)
puts "Performing #{name} #{count} times"
end
end
HardJob.perform_async('bob', 5)
HardJob.perform_at(Time.now + 5*60, 'bob', 5)
15 changes: 15 additions & 0 deletions gems/sidekiq/7.0/_test/test_1_2.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class HardWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
end

class HardJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
end

class Hook
end

class Middleware
end
70 changes: 70 additions & 0 deletions gems/sidekiq/7.0/_test/test_2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class HardWorker
include Sidekiq::Job
end

class HardJob
include Sidekiq::Job
end

class Hook
end

class Middleware
end

HardWorker.perform_async(1, 2, 3)
HardJob.perform_async(1, 2, 3)

# Test Sidekiq::Client
client = Sidekiq::Client.new
client.middleware do |chain|
chain.add Middleware
end
Sidekiq::Client.push('class' => HardWorker, 'args' => [1, 2, 3])
Sidekiq::Client.push_bulk('class' => HardWorker, 'args' => [[1, 2, 3], [4,5,6]])
Sidekiq::Client.enqueue(HardWorker, 'foo', 1, :bat => 'bar')
Sidekiq::Client.enqueue_to(:queue_name, HardWorker, 'foo', 1, :bat => 'bar')
Sidekiq::Client.enqueue_to_in(:queue_name, Time.now + 3 * 60, HardWorker, 'foo', 1, :bat => 'bar')
Sidekiq::Client.enqueue_in(Time.now + 3 * 60, HardWorker, 'foo', 1, :bat => 'bar')

Sidekiq::Client.push('class' => HardJob, 'args' => [1, 2, 3])
Sidekiq::Client.push_bulk('class' => HardJob, 'args' => [[1, 2, 3], [4,5,6]])
Sidekiq::Client.enqueue(HardJob, 'foo', 1, :bat => 'bar')
Sidekiq::Client.enqueue_to(:queue_name, HardJob, 'foo', 1, :bat => 'bar')
Sidekiq::Client.enqueue_to_in(:queue_name, Time.now + 3 * 60, HardJob, 'foo', 1, :bat => 'bar')
Sidekiq::Client.enqueue_in(Time.now + 3 * 60, HardJob, 'foo', 1, :bat => 'bar')

# Test configuration of middleware
Sidekiq.configure_server do |config|
config.redis = { namespace: 'rails', size: 2, url: 'redis://rails:6457/0' }
config.server_middleware do |chain|
chain.add Hook
end
config.client_middleware do |chain|
chain.add Hook
end
end

# Using Redis
# Taken from: https://github.com/mperham/sidekiq/wiki/Using-Redis
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://redis.example.com:7372/0', network_timeout: 5 }
end

Sidekiq.configure_client do |config|
config.redis = { url: 'redis://redis.example.com:7372/0' }
end

Sidekiq.configure_client do |config|
config.redis = { url: 'redis://redis.example.com:7372/0', size: 5 }
end

Sidekiq.configure_server do |config|
config.redis = { url: 'redis://redis.example.com:7372/0', size: 25 }
end

Sidekiq.configure_server do |config|
config.on(:shutdown) do
puts "Shutting down!"
end
end
120 changes: 120 additions & 0 deletions gems/sidekiq/7.0/_test/test_3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Test error handling
# Taken from: https://github.com/mperham/sidekiq/wiki/Error-Handling
Sidekiq.configure_server do |config|
config.error_handlers << proc { |ex,ctx_hash| puts "#{ex} exception: #{ctx_hash} context" }
end

class LessRetryableWorker
include Sidekiq::Job
sidekiq_options retry: 5 # Only five retries and then to the Dead Job Queue

def perform
end
end

class LessRetryableJob
include Sidekiq::Job
sidekiq_options retry: 5 # Only five retries and then to the Dead Job Queue

def perform
end
end

class NonRetryableWorker
include Sidekiq::Job
sidekiq_options retry: false # job will be discarded if it fails

def perform
end
end

class NonRetryableJob
include Sidekiq::Job
sidekiq_options retry: false # job will be discarded if it fails

def perform
end
end

class NoDeathWorker
include Sidekiq::Job
sidekiq_options retry: 5, dead: false # will retry 5 times and then disappear

def perform
end
end

class NoDeathJob
include Sidekiq::Job
sidekiq_options retry: 5, dead: false # will retry 5 times and then disappear

def perform
end
end

class WorkerWithCustomRetry
include Sidekiq::Job
sidekiq_options retry: 5

# The current retry count and exception is yielded. The return value of the
# block must be an integer. It is used as the delay, in seconds. A return value
# of nil will use the default.
sidekiq_retry_in do |count, exception|
case exception
when Object
10 * (count + 1) # (i.e. 10, 20, 30, 40, 50)
end
end

def perform
end
end

class JobWithCustomRetry
include Sidekiq::Job
sidekiq_options retry: 5

# The current retry count and exception is yielded. The return value of the
# block must be an integer. It is used as the delay, in seconds. A return value
# of nil will use the default.
sidekiq_retry_in do |count, exception|
case exception
when Object
10 * (count + 1) # (i.e. 10, 20, 30, 40, 50)
end
end

def perform
end
end

class FailingWorker
include Sidekiq::Job

sidekiq_retries_exhausted do |msg, ex|
Sidekiq.logger.warn "Failed #{msg['class']} with #{msg['args']}: #{msg['error_message']}"
end

def perform
raise "or I don't work"
end
end

class FailingJob
include Sidekiq::Job

sidekiq_retries_exhausted do |msg, ex|
Sidekiq.logger.warn "Failed #{msg['class']} with #{msg['args']}: #{msg['error_message']}"
end

def perform
raise "or I don't work"
end
end

# this goes in your initializer
Sidekiq.configure_server do |config|
config.death_handlers << -> (job, ex) do
puts "Error!"
end
end
87 changes: 87 additions & 0 deletions gems/sidekiq/7.0/_test/test_3_4_5.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class LessRetryableWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class LessRetryableJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class NonRetryableWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class NonRetryableJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class NoDeathWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class NoDeathJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class WorkerWithCustomRetry
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
extend Sidekiq::Job::Options::ClassMethods
def perform: () -> void
end

class JobWithCustomRetry
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
extend Sidekiq::Job::Options::ClassMethods
def perform: () -> void
end

class FailingWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
extend Sidekiq::Job::Options::ClassMethods
def perform: () -> void
end

class FailingJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
extend Sidekiq::Job::Options::ClassMethods
def perform: () -> void
end

class ImportantWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class ImportantJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class YourWorker
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end

class YourJob
include Sidekiq::Job
extend Sidekiq::Job::ClassMethods
def perform: () -> void
end
23 changes: 23 additions & 0 deletions gems/sidekiq/7.0/_test/test_4.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Test advanced configurations
# Taken from: https://github.com/mperham/sidekiq/wiki/Advanced-Options
class ImportantWorker
include Sidekiq::Job
sidekiq_options queue: 'critical', retry: true, dead: true, backtrace: false, tags: ['important', 'test']

def perform
puts "Doing critical work"
end
end

class ImportantJob
include Sidekiq::Job
sidekiq_options queue: 'critical', retry: true, dead: true, backtrace: false, tags: ['important', 'test']

def perform
puts "Doing critical work"
end
end

Sidekiq.default_job_options = { 'backtrace' => true }
ImportantWorker.set(queue: :critical).perform_async('name', 15)
ImportantJob.set(queue: :critical).perform_async('name', 15)
Loading

0 comments on commit 3fd4681

Please sign in to comment.