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

114 lines
2.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "sidekiq/version"
2021-12-06 10:25:06 -08:00
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")
2014-07-10 13:49:21 +00:00
require "sidekiq/config"
require "sidekiq/logger"
require "sidekiq/client"
2022-05-18 23:41:14 +02:00
require "sidekiq/transaction_aware_client"
2021-09-08 15:42:22 -07:00
require "sidekiq/job"
2022-03-24 06:24:58 +01:00
require "sidekiq/worker_compatibility_alias"
require "sidekiq/redis_client_adapter"
2012-02-17 13:39:36 -08:00
require "json"
2012-02-17 13:39:36 -08:00
module Sidekiq
NAME = "Sidekiq"
LICENSE = "See LICENSE and the LGPL-3.0 for licensing details."
def self.°°
2021-12-06 10:30:04 -08:00
puts "Take a deep breath and count to ten..."
2013-02-08 12:42:06 -08:00
end
def self.server?
defined?(Sidekiq::CLI)
end
2012-04-22 19:22:09 -07:00
def self.load_json(string)
2013-05-12 14:33:49 -07:00
JSON.parse(string)
end
2012-04-22 19:22:09 -07:00
def self.dump_json(object)
2013-05-12 14:33:49 -07:00
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)
2012-06-26 09:01:54 -07:00
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
2014-02-24 20:47:44 -08:00
end
def self.logger
default_configuration.logger
end
def self.configure_server(&block)
2022-08-30 11:51:23 -07:00
(@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
2022-08-30 11:51:23 -07:00
# 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)
2022-08-31 10:59:42 -07:00
require "sidekiq/embedded"
cfg = default_configuration
2022-08-30 11:51:23 -07:00
cfg.concurrency = 1
2022-08-31 10:59:42 -07:00
@config_blocks&.each { |block| block.call(cfg) }
yield cfg
2022-08-31 10:59:42 -07:00
Sidekiq::Embedded.new(cfg)
end
def self.configure_client
yield default_configuration unless server?
Implement strict argument checking (#5071) * Add the outline of failing tests * Implement argument checking * Move argument checking into Sidekiq::JobUtil#validate * Refactor acceptable class definition into a constant to cut down on array allocations * Improve error message, match raise call formatting to other errors in the class * Address feedback in the Pull Request to use the JSON round-trip method of confirming the safety of job argument payloads. Cleanup commented-out code from a few years back. Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Swap out JSON.load for JSON.parse per the security CI check Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Add a few more tests cases to build up confidence around our JSON.parse/dump approach and deep structures Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Improve test case description * Warn when job arguments do not serialize safely and point folks toward how to enable strict_mode and the best practice * Reconfigure the options-hash based approach to a global Sidekiq.strict_mode! method * Add a note in the raised error on how to disable the error * Let the error message breathe a little bit * Toggle strict_mode! off to suss out a test flake * Capitalize the start of a sentence * Rename job_is_json_safe to json_safe? Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Refactor a few tests to test a single argument at a time Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Break out test cases to exercise each individual intersting case instead of all at once Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Change formatting to be more consistent, tighter when arguments are simple Co-authored-by: Eda Zhou <eda.zhou@gusto.com> * Add a flag to disable the warning message for development warning messages Co-authored-by: Eda Zhou <eda.zhou@gusto.com> Co-authored-by: Eda Zhou <eda.zhou@gusto.com>
2021-12-07 13:20:20 -08:00
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
2012-02-17 13:39:36 -08:00
end
require "sidekiq/rails" if defined?(::Rails::Engine)