2016-11-22 23:39:00 -05:00
# frozen_string_literal: true
2018-12-03 22:24:37 +01:00
2019-04-01 18:20:41 +02:00
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
2022-08-25 10:15:11 -07:00
require " sidekiq/config "
2019-04-01 18:20:41 +02:00
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 "
2022-05-11 12:24:22 -07:00
require " sidekiq/redis_client_adapter "
2012-02-17 13:39:36 -08:00
2019-04-01 18:20:41 +02:00
require " json "
2012-05-25 20:21:42 -07:00
2012-02-17 13:39:36 -08:00
module Sidekiq
2019-04-01 18:20:41 +02:00
NAME = " Sidekiq "
LICENSE = " See LICENSE and the LGPL-3.0 for licensing details. "
2012-02-18 12:12:05 -08:00
2013-03-19 23:54:55 +01:00
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
2012-02-20 09:46:28 -08:00
def self . server?
defined? ( Sidekiq :: CLI )
2012-02-19 13:02:32 -08:00
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 )
2012-04-22 14:02:35 -07:00
end
2019-02-11 22:59:52 +01:00
2012-04-22 19:22:09 -07:00
def self . dump_json ( object )
2013-05-12 14:33:49 -07:00
JSON . generate ( object )
2012-04-22 14:02:35 -07:00
end
2022-08-25 10:15:11 -07:00
def self . pro?
defined? ( Sidekiq :: Pro )
2019-02-11 22:59:52 +01:00
end
2022-08-25 10:15:11 -07:00
def self . ent?
defined? ( Sidekiq :: Enterprise )
2018-12-29 00:05:51 +01:00
end
2022-08-25 10:15:11 -07:00
def self . redis_pool
( Thread . current [ :sidekiq_capsule ] || default_configuration ) . redis_pool
2012-05-15 19:44:35 -07:00
end
2018-12-29 00:05:51 +01:00
2022-08-25 10:15:11 -07:00
def self . redis ( & block )
( Thread . current [ :sidekiq_capsule ] || default_configuration ) . redis ( & block )
2012-06-26 09:01:54 -07:00
end
2022-08-25 10:15:11 -07:00
def self . strict_args! ( mode = :raise )
Sidekiq :: Config :: DEFAULTS [ :on_complex_arguments ] = mode
2019-11-14 10:06:53 -08:00
end
2022-08-25 10:15:11 -07:00
def self . default_job_options = ( hash )
@default_job_options = default_job_options . merge ( hash . transform_keys ( & :to_s ) )
2022-03-04 10:16:42 -08:00
end
2022-08-25 10:15:11 -07:00
def self . default_job_options
@default_job_options || = { " retry " = > true , " queue " = > " default " }
2015-05-04 10:38:10 -07:00
end
2022-08-25 10:15:11 -07:00
def self . default_configuration
@config || = Sidekiq :: Config . new
2014-02-24 20:47:44 -08:00
end
2022-08-29 09:18:23 -07:00
def self . logger
default_configuration . logger
end
def self . configure_server ( & block )
2022-08-30 11:51:23 -07:00
( @config_blocks || = [ ] ) << block
2022-08-25 10:15:11 -07:00
yield default_configuration if server?
2014-03-10 20:46:19 -07:00
end
2015-10-09 22:21:39 -07:00
2022-08-29 09:18:23 -07:00
# 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.
2022-08-29 09:18:23 -07:00
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 ) }
2022-08-29 09:18:23 -07:00
yield cfg
2022-08-31 10:59:42 -07:00
Sidekiq :: Embedded . new ( cfg )
2022-08-29 09:18:23 -07:00
end
2022-08-25 10:15:11 -07:00
def self . configure_client
yield default_configuration unless server?
2021-12-07 13:20:20 -08:00
end
2022-03-03 12:37:25 -08:00
# We are shutting down Sidekiq but what about threads that
2015-10-09 22:21:39 -07:00
# are working on some long job? This error is
2022-03-03 12:37:25 -08:00
# raised in jobs that have not finished within the hard
2015-10-09 22:21:39 -07:00
# timeout limit. This is needed to rollback db transactions,
# otherwise Ruby's Thread#kill will commit. See #377.
2022-03-03 12:37:25 -08:00
# DO NOT RESCUE THIS ERROR IN YOUR JOBS
2015-10-09 22:21:39 -07:00
class Shutdown < Interrupt ; end
2012-02-17 13:39:36 -08:00
end
2013-04-18 09:11:49 -07:00
2019-04-01 18:20:41 +02:00
require " sidekiq/rails " if defined? ( :: Rails :: Engine )