sidekiq 5.2.7 → 8.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Changes.md +845 -8
- data/LICENSE.txt +9 -0
- data/README.md +54 -54
- data/bin/multi_queue_bench +271 -0
- data/bin/sidekiq +22 -3
- data/bin/sidekiqload +219 -112
- data/bin/sidekiqmon +11 -0
- data/bin/webload +69 -0
- data/lib/active_job/queue_adapters/sidekiq_adapter.rb +120 -0
- data/lib/generators/sidekiq/job_generator.rb +59 -0
- data/lib/generators/sidekiq/templates/{worker.rb.erb → job.rb.erb} +2 -2
- data/lib/generators/sidekiq/templates/{worker_spec.rb.erb → job_spec.rb.erb} +1 -1
- data/lib/generators/sidekiq/templates/{worker_test.rb.erb → job_test.rb.erb} +1 -1
- data/lib/sidekiq/api.rb +757 -373
- data/lib/sidekiq/capsule.rb +132 -0
- data/lib/sidekiq/cli.rb +210 -233
- data/lib/sidekiq/client.rb +145 -103
- data/lib/sidekiq/component.rb +128 -0
- data/lib/sidekiq/config.rb +315 -0
- data/lib/sidekiq/deploy.rb +64 -0
- data/lib/sidekiq/embedded.rb +64 -0
- data/lib/sidekiq/fetch.rb +49 -42
- data/lib/sidekiq/iterable_job.rb +56 -0
- data/lib/sidekiq/job/interrupt_handler.rb +24 -0
- data/lib/sidekiq/job/iterable/active_record_enumerator.rb +53 -0
- data/lib/sidekiq/job/iterable/csv_enumerator.rb +47 -0
- data/lib/sidekiq/job/iterable/enumerators.rb +135 -0
- data/lib/sidekiq/job/iterable.rb +306 -0
- data/lib/sidekiq/job.rb +385 -0
- data/lib/sidekiq/job_logger.rb +34 -7
- data/lib/sidekiq/job_retry.rb +164 -109
- data/lib/sidekiq/job_util.rb +113 -0
- data/lib/sidekiq/launcher.rb +208 -107
- data/lib/sidekiq/logger.rb +80 -0
- data/lib/sidekiq/manager.rb +42 -46
- data/lib/sidekiq/metrics/query.rb +184 -0
- data/lib/sidekiq/metrics/shared.rb +109 -0
- data/lib/sidekiq/metrics/tracking.rb +150 -0
- data/lib/sidekiq/middleware/chain.rb +113 -56
- data/lib/sidekiq/middleware/current_attributes.rb +119 -0
- data/lib/sidekiq/middleware/i18n.rb +7 -7
- data/lib/sidekiq/middleware/modules.rb +23 -0
- data/lib/sidekiq/monitor.rb +147 -0
- data/lib/sidekiq/paginator.rb +41 -16
- data/lib/sidekiq/processor.rb +146 -127
- data/lib/sidekiq/profiler.rb +72 -0
- data/lib/sidekiq/rails.rb +46 -43
- data/lib/sidekiq/redis_client_adapter.rb +113 -0
- data/lib/sidekiq/redis_connection.rb +79 -108
- data/lib/sidekiq/ring_buffer.rb +31 -0
- data/lib/sidekiq/scheduled.rb +112 -50
- data/lib/sidekiq/sd_notify.rb +149 -0
- data/lib/sidekiq/systemd.rb +26 -0
- data/lib/sidekiq/testing/inline.rb +6 -5
- data/lib/sidekiq/testing.rb +91 -90
- data/lib/sidekiq/transaction_aware_client.rb +51 -0
- data/lib/sidekiq/version.rb +7 -1
- data/lib/sidekiq/web/action.rb +125 -60
- data/lib/sidekiq/web/application.rb +363 -259
- data/lib/sidekiq/web/config.rb +120 -0
- data/lib/sidekiq/web/csrf_protection.rb +183 -0
- data/lib/sidekiq/web/helpers.rb +241 -120
- data/lib/sidekiq/web/router.rb +62 -71
- data/lib/sidekiq/web.rb +69 -161
- data/lib/sidekiq/worker_compatibility_alias.rb +13 -0
- data/lib/sidekiq.rb +94 -182
- data/sidekiq.gemspec +26 -16
- data/web/assets/images/apple-touch-icon.png +0 -0
- data/web/assets/javascripts/application.js +150 -61
- data/web/assets/javascripts/base-charts.js +120 -0
- data/web/assets/javascripts/chart.min.js +13 -0
- data/web/assets/javascripts/chartjs-adapter-date-fns.min.js +7 -0
- data/web/assets/javascripts/chartjs-plugin-annotation.min.js +7 -0
- data/web/assets/javascripts/dashboard-charts.js +194 -0
- data/web/assets/javascripts/dashboard.js +41 -293
- data/web/assets/javascripts/metrics.js +280 -0
- data/web/assets/stylesheets/style.css +766 -0
- data/web/locales/ar.yml +72 -65
- data/web/locales/cs.yml +63 -62
- data/web/locales/da.yml +61 -53
- data/web/locales/de.yml +66 -53
- data/web/locales/el.yml +44 -24
- data/web/locales/en.yml +94 -66
- data/web/locales/es.yml +92 -54
- data/web/locales/fa.yml +66 -65
- data/web/locales/fr.yml +83 -62
- data/web/locales/gd.yml +99 -0
- data/web/locales/he.yml +66 -64
- data/web/locales/hi.yml +60 -59
- data/web/locales/it.yml +93 -54
- data/web/locales/ja.yml +75 -64
- data/web/locales/ko.yml +53 -52
- data/web/locales/lt.yml +84 -0
- data/web/locales/nb.yml +62 -61
- data/web/locales/nl.yml +53 -52
- data/web/locales/pl.yml +46 -45
- data/web/locales/{pt-br.yml → pt-BR.yml} +84 -56
- data/web/locales/pt.yml +52 -51
- data/web/locales/ru.yml +69 -63
- data/web/locales/sv.yml +54 -53
- data/web/locales/ta.yml +61 -60
- data/web/locales/tr.yml +101 -0
- data/web/locales/uk.yml +86 -61
- data/web/locales/ur.yml +65 -64
- data/web/locales/vi.yml +84 -0
- data/web/locales/zh-CN.yml +106 -0
- data/web/locales/{zh-tw.yml → zh-TW.yml} +43 -9
- data/web/views/_footer.erb +31 -19
- data/web/views/_job_info.erb +94 -75
- data/web/views/_metrics_period_select.erb +15 -0
- data/web/views/_nav.erb +14 -21
- data/web/views/_paging.erb +23 -19
- data/web/views/_poll_link.erb +3 -6
- data/web/views/_summary.erb +23 -23
- data/web/views/busy.erb +139 -87
- data/web/views/dashboard.erb +82 -53
- data/web/views/dead.erb +31 -27
- data/web/views/filtering.erb +6 -0
- data/web/views/layout.erb +15 -29
- data/web/views/metrics.erb +84 -0
- data/web/views/metrics_for_job.erb +58 -0
- data/web/views/morgue.erb +60 -70
- data/web/views/profiles.erb +43 -0
- data/web/views/queue.erb +50 -39
- data/web/views/queues.erb +45 -29
- data/web/views/retries.erb +65 -75
- data/web/views/retry.erb +32 -27
- data/web/views/scheduled.erb +58 -52
- data/web/views/scheduled_job_info.erb +1 -1
- metadata +96 -76
- data/.circleci/config.yml +0 -61
- data/.github/contributing.md +0 -32
- data/.github/issue_template.md +0 -11
- data/.gitignore +0 -15
- data/.travis.yml +0 -11
- data/3.0-Upgrade.md +0 -70
- data/4.0-Upgrade.md +0 -53
- data/5.0-Upgrade.md +0 -56
- data/COMM-LICENSE +0 -97
- data/Ent-Changes.md +0 -238
- data/Gemfile +0 -23
- data/LICENSE +0 -9
- data/Pro-2.0-Upgrade.md +0 -138
- data/Pro-3.0-Upgrade.md +0 -44
- data/Pro-4.0-Upgrade.md +0 -35
- data/Pro-Changes.md +0 -759
- data/Rakefile +0 -9
- data/bin/sidekiqctl +0 -20
- data/code_of_conduct.md +0 -50
- data/lib/generators/sidekiq/worker_generator.rb +0 -49
- data/lib/sidekiq/core_ext.rb +0 -1
- data/lib/sidekiq/ctl.rb +0 -221
- data/lib/sidekiq/delay.rb +0 -42
- data/lib/sidekiq/exception_handler.rb +0 -29
- data/lib/sidekiq/extensions/action_mailer.rb +0 -57
- data/lib/sidekiq/extensions/active_record.rb +0 -40
- data/lib/sidekiq/extensions/class_methods.rb +0 -40
- data/lib/sidekiq/extensions/generic_proxy.rb +0 -31
- data/lib/sidekiq/logging.rb +0 -122
- data/lib/sidekiq/middleware/server/active_record.rb +0 -23
- data/lib/sidekiq/util.rb +0 -66
- data/lib/sidekiq/worker.rb +0 -220
- data/web/assets/stylesheets/application-rtl.css +0 -246
- data/web/assets/stylesheets/application.css +0 -1144
- data/web/assets/stylesheets/bootstrap-rtl.min.css +0 -9
- data/web/assets/stylesheets/bootstrap.css +0 -5
- data/web/locales/zh-cn.yml +0 -68
- data/web/views/_status.erb +0 -4
@@ -0,0 +1,149 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# The MIT License
|
4
|
+
#
|
5
|
+
# Copyright (c) 2017, 2018, 2019, 2020 Agis Anastasopoulos
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
8
|
+
# this software and associated documentation files (the "Software"), to deal in
|
9
|
+
# the Software without restriction, including without limitation the rights to
|
10
|
+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
11
|
+
# the Software, and to permit persons to whom the Software is furnished to do so,
|
12
|
+
# subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
19
|
+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
20
|
+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
21
|
+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
22
|
+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
# This is a copy of https://github.com/agis/ruby-sdnotify as of commit a7d52ee
|
25
|
+
# The only changes made was "rehoming" it within the Sidekiq module to avoid
|
26
|
+
# namespace collisions and applying standard's code formatting style.
|
27
|
+
|
28
|
+
require "socket"
|
29
|
+
|
30
|
+
# SdNotify is a pure-Ruby implementation of sd_notify(3). It can be used to
|
31
|
+
# notify systemd about state changes. Methods of this package are no-op on
|
32
|
+
# non-systemd systems (eg. Darwin).
|
33
|
+
#
|
34
|
+
# The API maps closely to the original implementation of sd_notify(3),
|
35
|
+
# therefore be sure to check the official man pages prior to using SdNotify.
|
36
|
+
#
|
37
|
+
# @see https://www.freedesktop.org/software/systemd/man/sd_notify.html
|
38
|
+
module Sidekiq
|
39
|
+
module SdNotify
|
40
|
+
# Exception raised when there's an error writing to the notification socket
|
41
|
+
class NotifyError < RuntimeError; end
|
42
|
+
|
43
|
+
READY = "READY=1"
|
44
|
+
RELOADING = "RELOADING=1"
|
45
|
+
STOPPING = "STOPPING=1"
|
46
|
+
STATUS = "STATUS="
|
47
|
+
ERRNO = "ERRNO="
|
48
|
+
MAINPID = "MAINPID="
|
49
|
+
WATCHDOG = "WATCHDOG=1"
|
50
|
+
FDSTORE = "FDSTORE=1"
|
51
|
+
|
52
|
+
def self.ready(unset_env = false)
|
53
|
+
notify(READY, unset_env)
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.reloading(unset_env = false)
|
57
|
+
notify(RELOADING, unset_env)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.stopping(unset_env = false)
|
61
|
+
notify(STOPPING, unset_env)
|
62
|
+
end
|
63
|
+
|
64
|
+
# @param status [String] a custom status string that describes the current
|
65
|
+
# state of the service
|
66
|
+
def self.status(status, unset_env = false)
|
67
|
+
notify("#{STATUS}#{status}", unset_env)
|
68
|
+
end
|
69
|
+
|
70
|
+
# @param errno [Integer]
|
71
|
+
def self.errno(errno, unset_env = false)
|
72
|
+
notify("#{ERRNO}#{errno}", unset_env)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param pid [Integer]
|
76
|
+
def self.mainpid(pid, unset_env = false)
|
77
|
+
notify("#{MAINPID}#{pid}", unset_env)
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.watchdog(unset_env = false)
|
81
|
+
notify(WATCHDOG, unset_env)
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.fdstore(unset_env = false)
|
85
|
+
notify(FDSTORE, unset_env)
|
86
|
+
end
|
87
|
+
|
88
|
+
# @return [Boolean] true if the service manager expects watchdog keep-alive
|
89
|
+
# notification messages to be sent from this process.
|
90
|
+
#
|
91
|
+
# If the $WATCHDOG_USEC environment variable is set,
|
92
|
+
# and the $WATCHDOG_PID variable is unset or set to the PID of the current
|
93
|
+
# process
|
94
|
+
#
|
95
|
+
# @note Unlike sd_watchdog_enabled(3), this method does not mutate the
|
96
|
+
# environment.
|
97
|
+
def self.watchdog?
|
98
|
+
wd_usec = ENV["WATCHDOG_USEC"]
|
99
|
+
wd_pid = ENV["WATCHDOG_PID"]
|
100
|
+
|
101
|
+
return false unless wd_usec
|
102
|
+
|
103
|
+
begin
|
104
|
+
wd_usec = Integer(wd_usec)
|
105
|
+
rescue
|
106
|
+
return false
|
107
|
+
end
|
108
|
+
|
109
|
+
return false if wd_usec <= 0
|
110
|
+
return true if !wd_pid || wd_pid == $$.to_s
|
111
|
+
|
112
|
+
false
|
113
|
+
end
|
114
|
+
|
115
|
+
# Notify systemd with the provided state, via the notification socket, if
|
116
|
+
# any.
|
117
|
+
#
|
118
|
+
# Generally this method will be used indirectly through the other methods
|
119
|
+
# of the library.
|
120
|
+
#
|
121
|
+
# @param state [String]
|
122
|
+
# @param unset_env [Boolean]
|
123
|
+
#
|
124
|
+
# @return [Fixnum, nil] the number of bytes written to the notification
|
125
|
+
# socket or nil if there was no socket to report to (eg. the program wasn't
|
126
|
+
# started by systemd)
|
127
|
+
#
|
128
|
+
# @raise [NotifyError] if there was an error communicating with the systemd
|
129
|
+
# socket
|
130
|
+
#
|
131
|
+
# @see https://www.freedesktop.org/software/systemd/man/sd_notify.html
|
132
|
+
def self.notify(state, unset_env = false)
|
133
|
+
sock = ENV["NOTIFY_SOCKET"]
|
134
|
+
|
135
|
+
return nil unless sock
|
136
|
+
|
137
|
+
ENV.delete("NOTIFY_SOCKET") if unset_env
|
138
|
+
|
139
|
+
begin
|
140
|
+
Addrinfo.unix(sock, :DGRAM).connect do |s|
|
141
|
+
s.close_on_exec = true
|
142
|
+
s.write(state)
|
143
|
+
end
|
144
|
+
rescue => e
|
145
|
+
raise NotifyError, "#{e.class}: #{e.message}", e.backtrace
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Sidekiq's systemd integration allows Sidekiq to inform systemd:
|
5
|
+
# 1. when it has successfully started
|
6
|
+
# 2. when it is starting shutdown
|
7
|
+
# 3. periodically for a liveness check with a watchdog thread
|
8
|
+
#
|
9
|
+
module Sidekiq
|
10
|
+
def self.start_watchdog
|
11
|
+
usec = Integer(ENV["WATCHDOG_USEC"])
|
12
|
+
return Sidekiq.logger.error("systemd Watchdog too fast: " + usec) if usec < 1_000_000
|
13
|
+
|
14
|
+
sec_f = usec / 1_000_000.0
|
15
|
+
# "It is recommended that a daemon sends a keep-alive notification message
|
16
|
+
# to the service manager every half of the time returned here."
|
17
|
+
ping_f = sec_f / 2
|
18
|
+
Sidekiq.logger.info "Pinging systemd watchdog every #{ping_f.round(1)} sec"
|
19
|
+
Thread.new do
|
20
|
+
loop do
|
21
|
+
sleep ping_f
|
22
|
+
Sidekiq::SdNotify.watchdog
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
|
+
require "sidekiq/testing"
|
3
4
|
|
4
5
|
##
|
5
6
|
# The Sidekiq inline infrastructure overrides perform_async so that it
|
6
|
-
# actually calls perform instead. This allows
|
7
|
+
# actually calls perform instead. This allows jobs to be run inline in a
|
7
8
|
# testing environment.
|
8
9
|
#
|
9
10
|
# This is similar to `Resque.inline = true` functionality.
|
@@ -14,8 +15,8 @@ require 'sidekiq/testing'
|
|
14
15
|
#
|
15
16
|
# $external_variable = 0
|
16
17
|
#
|
17
|
-
# class
|
18
|
-
# include Sidekiq::
|
18
|
+
# class ExternalJob
|
19
|
+
# include Sidekiq::Job
|
19
20
|
#
|
20
21
|
# def perform
|
21
22
|
# $external_variable = 1
|
@@ -23,7 +24,7 @@ require 'sidekiq/testing'
|
|
23
24
|
# end
|
24
25
|
#
|
25
26
|
# assert_equal 0, $external_variable
|
26
|
-
#
|
27
|
+
# ExternalJob.perform_async
|
27
28
|
# assert_equal 1, $external_variable
|
28
29
|
#
|
29
30
|
Sidekiq::Testing.inline!
|
data/lib/sidekiq/testing.rb
CHANGED
@@ -1,27 +1,46 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require 'securerandom'
|
3
|
-
require 'sidekiq'
|
4
2
|
|
5
|
-
|
3
|
+
require "securerandom"
|
4
|
+
require "sidekiq"
|
6
5
|
|
6
|
+
module Sidekiq
|
7
7
|
class Testing
|
8
|
+
class TestModeAlreadySetError < RuntimeError; end
|
8
9
|
class << self
|
9
|
-
attr_accessor :
|
10
|
+
attr_accessor :__global_test_mode
|
10
11
|
|
12
|
+
# Calling without a block sets the global test mode, affecting
|
13
|
+
# all threads. Calling with a block only affects the current Thread.
|
11
14
|
def __set_test_mode(mode)
|
12
15
|
if block_given?
|
13
|
-
|
16
|
+
# Reentrant testing modes will lead to a rat's nest of code which is
|
17
|
+
# hard to reason about. You can set the testing mode once globally and
|
18
|
+
# you can override that global setting once per-thread.
|
19
|
+
raise TestModeAlreadySetError, "Nesting test modes is not supported" if __local_test_mode
|
20
|
+
|
21
|
+
self.__local_test_mode = mode
|
14
22
|
begin
|
15
|
-
self.__test_mode = mode
|
16
23
|
yield
|
17
24
|
ensure
|
18
|
-
self.
|
25
|
+
self.__local_test_mode = nil
|
19
26
|
end
|
20
27
|
else
|
21
|
-
self.
|
28
|
+
self.__global_test_mode = mode
|
22
29
|
end
|
23
30
|
end
|
24
31
|
|
32
|
+
def __test_mode
|
33
|
+
__local_test_mode || __global_test_mode
|
34
|
+
end
|
35
|
+
|
36
|
+
def __local_test_mode
|
37
|
+
Thread.current[:__sidekiq_test_mode]
|
38
|
+
end
|
39
|
+
|
40
|
+
def __local_test_mode=(value)
|
41
|
+
Thread.current[:__sidekiq_test_mode] = value
|
42
|
+
end
|
43
|
+
|
25
44
|
def disable!(&block)
|
26
45
|
__set_test_mode(:disable, &block)
|
27
46
|
end
|
@@ -35,35 +54,26 @@ module Sidekiq
|
|
35
54
|
end
|
36
55
|
|
37
56
|
def enabled?
|
38
|
-
|
57
|
+
__test_mode != :disable
|
39
58
|
end
|
40
59
|
|
41
60
|
def disabled?
|
42
|
-
|
61
|
+
__test_mode == :disable
|
43
62
|
end
|
44
63
|
|
45
64
|
def fake?
|
46
|
-
|
65
|
+
__test_mode == :fake
|
47
66
|
end
|
48
67
|
|
49
68
|
def inline?
|
50
|
-
|
69
|
+
__test_mode == :inline
|
51
70
|
end
|
52
71
|
|
53
72
|
def server_middleware
|
54
|
-
@server_chain ||= Middleware::Chain.new
|
73
|
+
@server_chain ||= Middleware::Chain.new(Sidekiq.default_configuration)
|
55
74
|
yield @server_chain if block_given?
|
56
75
|
@server_chain
|
57
76
|
end
|
58
|
-
|
59
|
-
def constantize(str)
|
60
|
-
names = str.split('::')
|
61
|
-
names.shift if names.empty? || names.first.empty?
|
62
|
-
|
63
|
-
names.inject(Object) do |constant, name|
|
64
|
-
constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
65
|
-
end
|
66
|
-
end
|
67
77
|
end
|
68
78
|
end
|
69
79
|
|
@@ -73,18 +83,18 @@ module Sidekiq
|
|
73
83
|
class EmptyQueueError < RuntimeError; end
|
74
84
|
|
75
85
|
module TestingClient
|
76
|
-
def
|
86
|
+
def atomic_push(conn, payloads)
|
77
87
|
if Sidekiq::Testing.fake?
|
78
88
|
payloads.each do |job|
|
79
89
|
job = Sidekiq.load_json(Sidekiq.dump_json(job))
|
80
|
-
job
|
81
|
-
Queues.push(job[
|
90
|
+
job["enqueued_at"] = ::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond) unless job["at"]
|
91
|
+
Queues.push(job["queue"], job["class"], job)
|
82
92
|
end
|
83
93
|
true
|
84
94
|
elsif Sidekiq::Testing.inline?
|
85
95
|
payloads.each do |job|
|
86
|
-
klass =
|
87
|
-
job[
|
96
|
+
klass = Object.const_get(job["class"])
|
97
|
+
job["id"] ||= SecureRandom.hex(12)
|
88
98
|
job_hash = Sidekiq.load_json(Sidekiq.dump_json(job))
|
89
99
|
klass.process_job(job_hash)
|
90
100
|
end
|
@@ -101,20 +111,20 @@ module Sidekiq
|
|
101
111
|
##
|
102
112
|
# The Queues class is only for testing the fake queue implementation.
|
103
113
|
# There are 2 data structures involved in tandem. This is due to the
|
104
|
-
# Rspec syntax of change(
|
105
|
-
# to the array. Because the array was
|
114
|
+
# Rspec syntax of change(HardJob.jobs, :size). It keeps a reference
|
115
|
+
# to the array. Because the array was derived from a filter of the total
|
106
116
|
# jobs enqueued, it appeared as though the array didn't change.
|
107
117
|
#
|
108
118
|
# To solve this, we'll keep 2 hashes containing the jobs. One with keys based
|
109
|
-
# on the queue, and another with keys of the
|
110
|
-
#
|
119
|
+
# on the queue, and another with keys of the job type, so the array for
|
120
|
+
# HardJob.jobs is a straight reference to a real array.
|
111
121
|
#
|
112
122
|
# Queue-based hash:
|
113
123
|
#
|
114
124
|
# {
|
115
125
|
# "default"=>[
|
116
126
|
# {
|
117
|
-
# "class"=>"TestTesting::
|
127
|
+
# "class"=>"TestTesting::HardJob",
|
118
128
|
# "args"=>[1, 2],
|
119
129
|
# "retry"=>true,
|
120
130
|
# "queue"=>"default",
|
@@ -124,12 +134,12 @@ module Sidekiq
|
|
124
134
|
# ]
|
125
135
|
# }
|
126
136
|
#
|
127
|
-
#
|
137
|
+
# Job-based hash:
|
128
138
|
#
|
129
139
|
# {
|
130
|
-
# "TestTesting::
|
140
|
+
# "TestTesting::HardJob"=>[
|
131
141
|
# {
|
132
|
-
# "class"=>"TestTesting::
|
142
|
+
# "class"=>"TestTesting::HardJob",
|
133
143
|
# "args"=>[1, 2],
|
134
144
|
# "retry"=>true,
|
135
145
|
# "queue"=>"default",
|
@@ -144,14 +154,14 @@ module Sidekiq
|
|
144
154
|
# require 'sidekiq/testing'
|
145
155
|
#
|
146
156
|
# assert_equal 0, Sidekiq::Queues["default"].size
|
147
|
-
#
|
157
|
+
# HardJob.perform_async(:something)
|
148
158
|
# assert_equal 1, Sidekiq::Queues["default"].size
|
149
159
|
# assert_equal :something, Sidekiq::Queues["default"].first['args'][0]
|
150
160
|
#
|
151
|
-
# You can also clear all
|
161
|
+
# You can also clear all jobs:
|
152
162
|
#
|
153
163
|
# assert_equal 0, Sidekiq::Queues["default"].size
|
154
|
-
#
|
164
|
+
# HardJob.perform_async(:something)
|
155
165
|
# Sidekiq::Queues.clear_all
|
156
166
|
# assert_equal 0, Sidekiq::Queues["default"].size
|
157
167
|
#
|
@@ -170,35 +180,36 @@ module Sidekiq
|
|
170
180
|
|
171
181
|
def push(queue, klass, job)
|
172
182
|
jobs_by_queue[queue] << job
|
173
|
-
|
183
|
+
jobs_by_class[klass] << job
|
174
184
|
end
|
175
185
|
|
176
186
|
def jobs_by_queue
|
177
187
|
@jobs_by_queue ||= Hash.new { |hash, key| hash[key] = [] }
|
178
188
|
end
|
179
189
|
|
180
|
-
def
|
181
|
-
@
|
190
|
+
def jobs_by_class
|
191
|
+
@jobs_by_class ||= Hash.new { |hash, key| hash[key] = [] }
|
182
192
|
end
|
193
|
+
alias_method :jobs_by_worker, :jobs_by_class
|
183
194
|
|
184
195
|
def delete_for(jid, queue, klass)
|
185
196
|
jobs_by_queue[queue.to_s].delete_if { |job| job["jid"] == jid }
|
186
|
-
|
197
|
+
jobs_by_class[klass].delete_if { |job| job["jid"] == jid }
|
187
198
|
end
|
188
199
|
|
189
200
|
def clear_for(queue, klass)
|
190
|
-
jobs_by_queue[queue].clear
|
191
|
-
|
201
|
+
jobs_by_queue[queue.to_s].clear
|
202
|
+
jobs_by_class[klass].clear
|
192
203
|
end
|
193
204
|
|
194
205
|
def clear_all
|
195
206
|
jobs_by_queue.clear
|
196
|
-
|
207
|
+
jobs_by_class.clear
|
197
208
|
end
|
198
209
|
end
|
199
210
|
end
|
200
211
|
|
201
|
-
module
|
212
|
+
module Job
|
202
213
|
##
|
203
214
|
# The Sidekiq testing infrastructure overrides perform_async
|
204
215
|
# so that it does not actually touch the network. Instead it
|
@@ -212,70 +223,53 @@ module Sidekiq
|
|
212
223
|
#
|
213
224
|
# require 'sidekiq/testing'
|
214
225
|
#
|
215
|
-
# assert_equal 0,
|
216
|
-
#
|
217
|
-
# assert_equal 1,
|
218
|
-
# assert_equal :something,
|
219
|
-
#
|
220
|
-
# assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
221
|
-
# MyMailer.delay.send_welcome_email('foo@example.com')
|
222
|
-
# assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
223
|
-
#
|
224
|
-
# You can also clear and drain all workers' jobs:
|
225
|
-
#
|
226
|
-
# assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
227
|
-
# assert_equal 0, Sidekiq::Extensions::DelayedModel.jobs.size
|
228
|
-
#
|
229
|
-
# MyMailer.delay.send_welcome_email('foo@example.com')
|
230
|
-
# MyModel.delay.do_something_hard
|
231
|
-
#
|
232
|
-
# assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
233
|
-
# assert_equal 1, Sidekiq::Extensions::DelayedModel.jobs.size
|
226
|
+
# assert_equal 0, HardJob.jobs.size
|
227
|
+
# HardJob.perform_async(:something)
|
228
|
+
# assert_equal 1, HardJob.jobs.size
|
229
|
+
# assert_equal :something, HardJob.jobs[0]['args'][0]
|
234
230
|
#
|
235
|
-
#
|
231
|
+
# You can also clear and drain all job types:
|
236
232
|
#
|
237
|
-
#
|
238
|
-
# assert_equal 0, Sidekiq::Extensions::DelayedModel.jobs.size
|
233
|
+
# Sidekiq::Job.clear_all # or .drain_all
|
239
234
|
#
|
240
235
|
# This can be useful to make sure jobs don't linger between tests:
|
241
236
|
#
|
242
237
|
# RSpec.configure do |config|
|
243
238
|
# config.before(:each) do
|
244
|
-
# Sidekiq::
|
239
|
+
# Sidekiq::Job.clear_all
|
245
240
|
# end
|
246
241
|
# end
|
247
242
|
#
|
248
243
|
# or for acceptance testing, i.e. with cucumber:
|
249
244
|
#
|
250
245
|
# AfterStep do
|
251
|
-
# Sidekiq::
|
246
|
+
# Sidekiq::Job.drain_all
|
252
247
|
# end
|
253
248
|
#
|
254
249
|
# When I sign up as "foo@example.com"
|
255
250
|
# Then I should receive a welcome email to "foo@example.com"
|
256
251
|
#
|
257
252
|
module ClassMethods
|
258
|
-
|
259
253
|
# Queue for this worker
|
260
254
|
def queue
|
261
|
-
|
255
|
+
get_sidekiq_options["queue"]
|
262
256
|
end
|
263
257
|
|
264
258
|
# Jobs queued for this worker
|
265
259
|
def jobs
|
266
|
-
Queues.
|
260
|
+
Queues.jobs_by_class[to_s]
|
267
261
|
end
|
268
262
|
|
269
263
|
# Clear all jobs for this worker
|
270
264
|
def clear
|
271
|
-
Queues.clear_for(queue,
|
265
|
+
Queues.clear_for(queue, to_s)
|
272
266
|
end
|
273
267
|
|
274
268
|
# Drain and run all jobs for this worker
|
275
269
|
def drain
|
276
270
|
while jobs.any?
|
277
271
|
next_job = jobs.first
|
278
|
-
Queues.delete_for(next_job["jid"], next_job["queue"],
|
272
|
+
Queues.delete_for(next_job["jid"], next_job["queue"], to_s)
|
279
273
|
process_job(next_job)
|
280
274
|
end
|
281
275
|
end
|
@@ -284,16 +278,16 @@ module Sidekiq
|
|
284
278
|
def perform_one
|
285
279
|
raise(EmptyQueueError, "perform_one called with empty job queue") if jobs.empty?
|
286
280
|
next_job = jobs.first
|
287
|
-
Queues.delete_for(next_job["jid"], queue,
|
281
|
+
Queues.delete_for(next_job["jid"], next_job["queue"], to_s)
|
288
282
|
process_job(next_job)
|
289
283
|
end
|
290
284
|
|
291
285
|
def process_job(job)
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
Sidekiq::Testing.server_middleware.invoke(
|
296
|
-
execute_job(
|
286
|
+
instance = new
|
287
|
+
instance.jid = job["jid"]
|
288
|
+
instance.bid = job["bid"] if instance.respond_to?(:bid=)
|
289
|
+
Sidekiq::Testing.server_middleware.invoke(instance, job, job["queue"]) do
|
290
|
+
execute_job(instance, job["args"])
|
297
291
|
end
|
298
292
|
end
|
299
293
|
|
@@ -307,27 +301,34 @@ module Sidekiq
|
|
307
301
|
Queues.jobs_by_queue.values.flatten
|
308
302
|
end
|
309
303
|
|
310
|
-
# Clear all queued jobs
|
304
|
+
# Clear all queued jobs
|
311
305
|
def clear_all
|
312
306
|
Queues.clear_all
|
313
307
|
end
|
314
308
|
|
315
|
-
# Drain all queued jobs
|
309
|
+
# Drain (execute) all queued jobs
|
316
310
|
def drain_all
|
317
311
|
while jobs.any?
|
318
|
-
|
312
|
+
job_classes = jobs.map { |job| job["class"] }.uniq
|
319
313
|
|
320
|
-
|
321
|
-
|
314
|
+
job_classes.each do |job_class|
|
315
|
+
Object.const_get(job_class).drain
|
322
316
|
end
|
323
317
|
end
|
324
318
|
end
|
325
319
|
end
|
326
320
|
end
|
321
|
+
|
322
|
+
module TestingExtensions
|
323
|
+
def jobs_for(klass)
|
324
|
+
jobs.select do |job|
|
325
|
+
marshalled = job["args"][0]
|
326
|
+
marshalled.index(klass.to_s) && YAML.safe_load(marshalled)[0] == klass
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
327
330
|
end
|
328
331
|
|
329
|
-
if defined?(::Rails) && Rails.respond_to?(:env) && !Rails.env.test?
|
330
|
-
|
331
|
-
puts("⛔️ WARNING: Sidekiq testing API enabled, but this is not the test environment. Your jobs will not go to Redis.")
|
332
|
-
puts("**************************************************")
|
332
|
+
if defined?(::Rails) && Rails.respond_to?(:env) && !Rails.env.test? && !$TESTING # rubocop:disable Style/GlobalVars
|
333
|
+
warn("⛔️ WARNING: Sidekiq testing API enabled, but this is not the test environment. Your jobs will not go to Redis.", uplevel: 1)
|
333
334
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
require "sidekiq/client"
|
5
|
+
|
6
|
+
module Sidekiq
|
7
|
+
class TransactionAwareClient
|
8
|
+
def initialize(pool: nil, config: nil)
|
9
|
+
@redis_client = Client.new(pool: pool, config: config)
|
10
|
+
end
|
11
|
+
|
12
|
+
def batching?
|
13
|
+
Thread.current[:sidekiq_batch]
|
14
|
+
end
|
15
|
+
|
16
|
+
def push(item)
|
17
|
+
# 6160 we can't support both Sidekiq::Batch and transactions.
|
18
|
+
return @redis_client.push(item) if batching?
|
19
|
+
|
20
|
+
# pre-allocate the JID so we can return it immediately and
|
21
|
+
# save it to the database as part of the transaction.
|
22
|
+
item["jid"] ||= SecureRandom.hex(12)
|
23
|
+
AfterCommitEverywhere.after_commit { @redis_client.push(item) }
|
24
|
+
item["jid"]
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# We don't provide transactionality for push_bulk because we don't want
|
29
|
+
# to hold potentially hundreds of thousands of job records in memory due to
|
30
|
+
# a long running enqueue process.
|
31
|
+
def push_bulk(items)
|
32
|
+
@redis_client.push_bulk(items)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
##
|
38
|
+
# Use `Sidekiq.transactional_push!` in your sidekiq.rb initializer
|
39
|
+
module Sidekiq
|
40
|
+
def self.transactional_push!
|
41
|
+
begin
|
42
|
+
require "after_commit_everywhere"
|
43
|
+
rescue LoadError
|
44
|
+
raise %q(You need to add `gem "after_commit_everywhere"` to your Gemfile to use Sidekiq's transactional client)
|
45
|
+
end
|
46
|
+
|
47
|
+
Sidekiq.default_job_options["client_class"] = Sidekiq::TransactionAwareClient
|
48
|
+
Sidekiq::JobUtil::TRANSIENT_ATTRIBUTES << "client_class"
|
49
|
+
true
|
50
|
+
end
|
51
|
+
end
|