File: sidekiq.rb

package info (click to toggle)
ruby-sidekiq 7.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 956 kB
  • sloc: ruby: 6,094; javascript: 526; makefile: 21; sh: 20
file content (148 lines) | stat: -rw-r--r-- 4,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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")

begin
  require "sidekiq-ent/version"
  fail <<~EOM  if Gem::Version.new(Sidekiq::Enterprise::VERSION).segments[0] != Sidekiq::MAJOR

    Sidekiq Enterprise #{Sidekiq::Enterprise::VERSION} does not work with Sidekiq #{Sidekiq::VERSION}.
    Starting with Sidekiq 7, major versions are synchronized so Sidekiq Enterprise 7 works with Sidekiq 7.
    Use `bundle up sidekiq-ent` to upgrade.

  EOM
rescue LoadError
end

begin
  require "sidekiq/pro/version"
  fail <<~EOM  if Gem::Version.new(Sidekiq::Pro::VERSION).segments[0] != Sidekiq::MAJOR

    Sidekiq Pro #{Sidekiq::Pro::VERSION} does not work with Sidekiq #{Sidekiq::VERSION}.
    Starting with Sidekiq 7, major versions are synchronized so Sidekiq Pro 7 works with Sidekiq 7.
    Use `bundle up sidekiq-pro` to upgrade.

  EOM
rescue LoadError
end

require "sidekiq/config"
require "sidekiq/logger"
require "sidekiq/client"
require "sidekiq/transaction_aware_client"
require "sidekiq/job"
require "sidekiq/iterable_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

  def self.freeze!
    @frozen = true
    @config_blocks = nil
  end

  # Creates a Sidekiq::Config instance that is more tuned for embedding
  # within an arbitrary Ruby process. Notably 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.stop
  #
  # 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.
  #
  # NB: Sidekiq only supports one instance in memory. You will get undefined behavior
  # if you try to embed Sidekiq twice in the same process.
  def self.configure_embed(&block)
    raise "Sidekiq global configuration is frozen, you must create all embedded instances BEFORE calling `run`" if @frozen

    require "sidekiq/embedded"
    cfg = default_configuration
    cfg.concurrency = 2
    @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)