mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
113 lines
2.9 KiB
Ruby
113 lines
2.9 KiB
Ruby
# frozen_string_literal: true
|
||
|
||
require "sidekiq/version"
|
||
fail "Sidekiq #{Sidekiq::VERSION} does not support Ruby versions below 2.7.0." if RUBY_PLATFORM != "java" && Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.7.0")
|
||
|
||
require "sidekiq/config"
|
||
require "sidekiq/logger"
|
||
require "sidekiq/client"
|
||
require "sidekiq/transaction_aware_client"
|
||
require "sidekiq/job"
|
||
require "sidekiq/worker_compatibility_alias"
|
||
require "sidekiq/redis_client_adapter"
|
||
|
||
require "json"
|
||
|
||
module Sidekiq
|
||
NAME = "Sidekiq"
|
||
LICENSE = "See LICENSE and the LGPL-3.0 for licensing details."
|
||
|
||
def self.❨╯°□°❩╯︵┻━┻
|
||
puts "Take a deep breath and count to ten..."
|
||
end
|
||
|
||
def self.server?
|
||
defined?(Sidekiq::CLI)
|
||
end
|
||
|
||
def self.load_json(string)
|
||
JSON.parse(string)
|
||
end
|
||
|
||
def self.dump_json(object)
|
||
JSON.generate(object)
|
||
end
|
||
|
||
def self.pro?
|
||
defined?(Sidekiq::Pro)
|
||
end
|
||
|
||
def self.ent?
|
||
defined?(Sidekiq::Enterprise)
|
||
end
|
||
|
||
def self.redis_pool
|
||
(Thread.current[:sidekiq_capsule] || default_configuration).redis_pool
|
||
end
|
||
|
||
def self.redis(&block)
|
||
(Thread.current[:sidekiq_capsule] || default_configuration).redis(&block)
|
||
end
|
||
|
||
def self.strict_args!(mode = :raise)
|
||
Sidekiq::Config::DEFAULTS[:on_complex_arguments] = mode
|
||
end
|
||
|
||
def self.default_job_options=(hash)
|
||
@default_job_options = default_job_options.merge(hash.transform_keys(&:to_s))
|
||
end
|
||
|
||
def self.default_job_options
|
||
@default_job_options ||= {"retry" => true, "queue" => "default"}
|
||
end
|
||
|
||
def self.default_configuration
|
||
@config ||= Sidekiq::Config.new
|
||
end
|
||
|
||
def self.logger
|
||
default_configuration.logger
|
||
end
|
||
|
||
def self.configure_server(&block)
|
||
(@config_blocks ||= []) << block
|
||
yield default_configuration if server?
|
||
end
|
||
|
||
# Creates a Sidekiq::Config instance that is more tuned for embedding
|
||
# within an arbitrary Ruby process. Noteably it reduces concurrency by
|
||
# default so there is less contention for CPU time with other threads.
|
||
#
|
||
# inst = Sidekiq.configure_embed do |config|
|
||
# config.queues = %w[critical default low]
|
||
# end
|
||
# inst.run
|
||
# sleep 10
|
||
# inst.terminate
|
||
#
|
||
# NB: it is really easy to overload a Ruby process with threads due to the GIL.
|
||
# I do not recommend setting concurrency higher than 2-3.
|
||
def self.configure_embed(&block)
|
||
require "sidekiq/embedded"
|
||
cfg = default_configuration
|
||
cfg.concurrency = 1
|
||
@config_blocks&.each { |block| block.call(cfg) }
|
||
yield cfg
|
||
|
||
Sidekiq::Embedded.new(cfg)
|
||
end
|
||
|
||
def self.configure_client
|
||
yield default_configuration unless server?
|
||
end
|
||
|
||
# We are shutting down Sidekiq but what about threads that
|
||
# are working on some long job? This error is
|
||
# 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 JOBS
|
||
class Shutdown < Interrupt; end
|
||
end
|
||
|
||
require "sidekiq/rails" if defined?(::Rails::Engine)
|