inst-jobs 2.3.2 → 2.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/db/migrate/20101216224513_create_delayed_jobs.rb +9 -7
- data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +8 -13
- data/db/migrate/20110610213249_optimize_delayed_jobs.rb +8 -8
- data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +25 -25
- data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +4 -8
- data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +1 -3
- data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +11 -15
- data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +1 -1
- data/db/migrate/20120608191051_add_jobs_run_at_index.rb +2 -2
- data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +1 -1
- data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +2 -3
- data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +9 -13
- data/db/migrate/20151210162949_improve_max_concurrent.rb +4 -8
- data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +3 -2
- data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +13 -17
- data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +8 -8
- data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +72 -77
- data/db/migrate/20200825011002_add_strand_order_override.rb +93 -97
- data/db/migrate/20210809145804_add_n_strand_index.rb +12 -0
- data/db/migrate/20210812210128_add_singleton_column.rb +203 -0
- data/exe/inst_jobs +3 -2
- data/lib/delayed/backend/active_record.rb +182 -148
- data/lib/delayed/backend/base.rb +79 -74
- data/lib/delayed/batch.rb +11 -9
- data/lib/delayed/cli.rb +98 -84
- data/lib/delayed/core_ext/kernel.rb +4 -2
- data/lib/delayed/daemon.rb +70 -74
- data/lib/delayed/job_tracking.rb +26 -25
- data/lib/delayed/lifecycle.rb +27 -24
- data/lib/delayed/log_tailer.rb +17 -17
- data/lib/delayed/logging.rb +13 -16
- data/lib/delayed/message_sending.rb +42 -51
- data/lib/delayed/performable_method.rb +5 -7
- data/lib/delayed/periodic.rb +66 -65
- data/lib/delayed/plugin.rb +2 -4
- data/lib/delayed/pool.rb +198 -193
- data/lib/delayed/server/helpers.rb +6 -6
- data/lib/delayed/server.rb +51 -54
- data/lib/delayed/settings.rb +93 -81
- data/lib/delayed/testing.rb +21 -22
- data/lib/delayed/version.rb +1 -1
- data/lib/delayed/work_queue/in_process.rb +21 -18
- data/lib/delayed/work_queue/parent_process/client.rb +54 -55
- data/lib/delayed/work_queue/parent_process/server.rb +215 -209
- data/lib/delayed/work_queue/parent_process.rb +52 -53
- data/lib/delayed/worker/consul_health_check.rb +21 -19
- data/lib/delayed/worker/health_check.rb +21 -12
- data/lib/delayed/worker/null_health_check.rb +3 -1
- data/lib/delayed/worker/process_helper.rb +8 -9
- data/lib/delayed/worker.rb +271 -265
- data/lib/delayed/yaml_extensions.rb +12 -10
- data/lib/delayed_job.rb +37 -38
- data/lib/inst-jobs.rb +1 -1
- data/spec/active_record_job_spec.rb +128 -135
- data/spec/delayed/cli_spec.rb +7 -7
- data/spec/delayed/daemon_spec.rb +8 -8
- data/spec/delayed/message_sending_spec.rb +16 -9
- data/spec/delayed/periodic_spec.rb +13 -12
- data/spec/delayed/server_spec.rb +38 -38
- data/spec/delayed/settings_spec.rb +26 -25
- data/spec/delayed/work_queue/in_process_spec.rb +7 -7
- data/spec/delayed/work_queue/parent_process/client_spec.rb +15 -11
- data/spec/delayed/work_queue/parent_process/server_spec.rb +43 -40
- data/spec/delayed/work_queue/parent_process_spec.rb +21 -21
- data/spec/delayed/worker/consul_health_check_spec.rb +22 -22
- data/spec/delayed/worker/health_check_spec.rb +51 -49
- data/spec/delayed/worker_spec.rb +28 -25
- data/spec/gemfiles/52.gemfile +5 -3
- data/spec/gemfiles/52.gemfile.lock +240 -0
- data/spec/gemfiles/60.gemfile +5 -3
- data/spec/gemfiles/60.gemfile.lock +1 -1
- data/spec/gemfiles/61.gemfile +5 -3
- data/spec/sample_jobs.rb +45 -15
- data/spec/shared/delayed_batch.rb +74 -67
- data/spec/shared/delayed_method.rb +143 -102
- data/spec/shared/performable_method.rb +39 -38
- data/spec/shared/shared_backend.rb +534 -441
- data/spec/shared/testing.rb +14 -14
- data/spec/shared/worker.rb +155 -147
- data/spec/shared_jobs_specs.rb +13 -13
- data/spec/spec_helper.rb +43 -40
- metadata +75 -56
- data/lib/delayed/backend/redis/bulk_update.lua +0 -50
- data/lib/delayed/backend/redis/destroy_job.lua +0 -2
- data/lib/delayed/backend/redis/enqueue.lua +0 -29
- data/lib/delayed/backend/redis/fail_job.lua +0 -5
- data/lib/delayed/backend/redis/find_available.lua +0 -3
- data/lib/delayed/backend/redis/functions.rb +0 -59
- data/lib/delayed/backend/redis/get_and_lock_next_available.lua +0 -17
- data/lib/delayed/backend/redis/includes/jobs_common.lua +0 -203
- data/lib/delayed/backend/redis/job.rb +0 -528
- data/lib/delayed/backend/redis/set_running.lua +0 -5
- data/lib/delayed/backend/redis/tickle_strand.lua +0 -2
- data/spec/redis_job_spec.rb +0 -148
@@ -1,20 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
attr_reader :id
|
5
|
+
class JobClass
|
6
|
+
attr_reader :id
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
def initialize
|
9
|
+
@id = rand
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
end
|
12
|
+
def ==(other)
|
13
|
+
id == other.id
|
16
14
|
end
|
15
|
+
end
|
17
16
|
|
17
|
+
RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
18
18
|
let(:parent) { Delayed::WorkQueue::ParentProcess.new }
|
19
19
|
let(:subject) { described_class.new(listen_socket) }
|
20
20
|
let(:listen_socket) { Socket.unix_server_socket(parent.server_address) }
|
@@ -26,7 +26,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
26
26
|
before :all do
|
27
27
|
Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
|
28
28
|
Delayed::Settings.parent_process = {
|
29
|
-
|
29
|
+
"server_address" => "/tmp/inst-jobs-test.sock"
|
30
30
|
}
|
31
31
|
end
|
32
32
|
|
@@ -34,27 +34,27 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
34
34
|
Delayed::Settings.parent_process = {}
|
35
35
|
end
|
36
36
|
|
37
|
-
after
|
38
|
-
File.unlink(
|
37
|
+
after do
|
38
|
+
File.unlink("/tmp/inst-jobs-test.sock") if File.exist?("/tmp/inst-jobs-test.sock")
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
42
|
-
|
41
|
+
it "accepts new clients" do
|
42
|
+
Socket.unix(subject.listen_socket.local_address.unix_path)
|
43
43
|
expect { subject.run_once }.to change(subject, :connected_clients).by(1)
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
46
|
+
it "queries the queue on client request" do
|
47
47
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
48
48
|
subject.run_once
|
49
49
|
|
50
|
-
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return(
|
50
|
+
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
|
51
51
|
Marshal.dump(args, client)
|
52
52
|
subject.run_once
|
53
53
|
expect(client).to be_ready
|
54
54
|
expect(Marshal.load(client)).to eq(job)
|
55
55
|
end
|
56
56
|
|
57
|
-
it
|
57
|
+
it "can pop multiple jobs at once" do
|
58
58
|
client1 = Socket.unix(subject.listen_socket.local_address.unix_path)
|
59
59
|
subject.run_once
|
60
60
|
client2 = Socket.unix(subject.listen_socket.local_address.unix_path)
|
@@ -62,8 +62,8 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
62
62
|
|
63
63
|
job1 = JobClass.new
|
64
64
|
job2 = JobClass.new
|
65
|
-
job_args = [[
|
66
|
-
jobs = {
|
65
|
+
job_args = [%w[worker_name1 worker_name2], "queue_name", 1, 2, hash_including(prefetch: 3)]
|
66
|
+
jobs = { "worker_name1" => job1, "worker_name2" => job2 }
|
67
67
|
|
68
68
|
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return(jobs)
|
69
69
|
Marshal.dump(["worker_name1", worker_config], client1)
|
@@ -73,27 +73,28 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
73
73
|
expect(Marshal.load(client2)).to eq(job2)
|
74
74
|
end
|
75
75
|
|
76
|
-
it
|
76
|
+
it "will prefetch and use jobs" do
|
77
77
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
78
78
|
subject.run_once
|
79
79
|
|
80
|
-
allow(subject).to receive(:prefetch_owner).and_return(
|
81
|
-
job_args = [["worker_name1"], "queue_name", 1, 2,
|
82
|
-
|
83
|
-
job2.
|
84
|
-
|
85
|
-
job3.
|
86
|
-
|
80
|
+
allow(subject).to receive(:prefetch_owner).and_return("work_queue:X")
|
81
|
+
job_args = [["worker_name1"], "queue_name", 1, 2,
|
82
|
+
{ prefetch: 4, prefetch_owner: "work_queue:X", forced_latency: 6.0 }]
|
83
|
+
job2 = Delayed::Job.new(tag: "tag")
|
84
|
+
job2.create_and_lock!("work_queue:X")
|
85
|
+
job3 = Delayed::Job.new(tag: "tag")
|
86
|
+
job3.create_and_lock!("work_queue:X")
|
87
|
+
jobs = { "worker_name1" => job, "work_queue:X" => [job2, job3] }
|
87
88
|
|
88
89
|
expect(Delayed::Job).to receive(:get_and_lock_next_available).once.with(*job_args).and_return(jobs)
|
89
90
|
Marshal.dump(["worker_name1", worker_config], client)
|
90
91
|
subject.run_once
|
91
|
-
expect(subject).
|
92
|
+
expect(subject).not_to be_all_workers_idle
|
92
93
|
expect(Marshal.load(client)).to eq(job)
|
93
94
|
|
94
95
|
Marshal.dump(["worker_name1", worker_config], client)
|
95
96
|
subject.run_once
|
96
|
-
expect(subject).
|
97
|
+
expect(subject).not_to be_all_workers_idle
|
97
98
|
expect(Marshal.load(client)).to eq(job2)
|
98
99
|
end
|
99
100
|
|
@@ -107,7 +108,10 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
107
108
|
expect(client).not_to be_ready
|
108
109
|
|
109
110
|
# next time around, return the result
|
110
|
-
expect(Delayed::Job).to receive(:get_and_lock_next_available)
|
111
|
+
expect(Delayed::Job).to receive(:get_and_lock_next_available)
|
112
|
+
.with(*job_args)
|
113
|
+
.and_return("worker_name" => job)
|
114
|
+
.ordered
|
111
115
|
allow(Delayed::Settings).to receive(:sleep_delay).and_return(0)
|
112
116
|
allow(Delayed::Settings).to receive(:sleep_delay_stagger).and_return(0)
|
113
117
|
subject.run_once
|
@@ -115,7 +119,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
115
119
|
expect(Marshal.load(client)).to eq(job)
|
116
120
|
end
|
117
121
|
|
118
|
-
it
|
122
|
+
it "drops the client on i/o error" do
|
119
123
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
120
124
|
subject.run_once
|
121
125
|
|
@@ -125,7 +129,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
125
129
|
expect { subject.run_once }.to change(subject, :connected_clients).by(-1)
|
126
130
|
end
|
127
131
|
|
128
|
-
it
|
132
|
+
it "drops the client when the client disconnects" do
|
129
133
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
130
134
|
subject.run_once
|
131
135
|
|
@@ -138,7 +142,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
138
142
|
expect(subject.instance_variable_get(:@waiting_clients).first.last).to eq []
|
139
143
|
end
|
140
144
|
|
141
|
-
it
|
145
|
+
it "drops the client when a write fails" do
|
142
146
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
143
147
|
subject.run_once
|
144
148
|
|
@@ -151,7 +155,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
151
155
|
# don't let the server see the close and process it there; we want to check a failure later
|
152
156
|
expect(subject).to receive(:handle_request).with(server_client_socket)
|
153
157
|
|
154
|
-
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return(
|
158
|
+
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
|
155
159
|
# the job gets unlocked
|
156
160
|
expect(Delayed::Job).to receive(:unlock).with([job])
|
157
161
|
subject.run_once
|
@@ -161,14 +165,14 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
161
165
|
expect(subject.instance_variable_get(:@waiting_clients).first.last).to eq []
|
162
166
|
end
|
163
167
|
|
164
|
-
it
|
168
|
+
it "tracks when clients are idle" do
|
165
169
|
expect(subject.all_workers_idle?).to be(true)
|
166
170
|
|
167
171
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
168
172
|
subject.run_once
|
169
173
|
expect(subject.all_workers_idle?).to be(true)
|
170
174
|
|
171
|
-
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return(
|
175
|
+
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
|
172
176
|
Marshal.dump(args, client)
|
173
177
|
subject.run_once
|
174
178
|
expect(subject.all_workers_idle?).to be(false)
|
@@ -179,7 +183,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
179
183
|
expect(subject.all_workers_idle?).to be(true)
|
180
184
|
end
|
181
185
|
|
182
|
-
it
|
186
|
+
it "triggers the lifecycle event around the pop" do
|
183
187
|
called = false
|
184
188
|
client = Socket.unix(subject.listen_socket.local_address.unix_path)
|
185
189
|
subject.run_once
|
@@ -187,7 +191,7 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
187
191
|
Delayed::Worker.lifecycle.around(:work_queue_pop) do |queue, &cb|
|
188
192
|
expect(subject.all_workers_idle?).to be(true)
|
189
193
|
expect(queue).to eq(subject)
|
190
|
-
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return(
|
194
|
+
expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
|
191
195
|
called = true
|
192
196
|
res = cb.call(queue)
|
193
197
|
expect(subject.all_workers_idle?).to be(false)
|
@@ -201,4 +205,3 @@ RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
|
|
201
205
|
expect(called).to eq(true)
|
202
206
|
end
|
203
207
|
end
|
204
|
-
|
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "spec_helper"
|
4
|
+
require "fileutils"
|
5
5
|
|
6
6
|
RSpec.describe Delayed::WorkQueue::ParentProcess do
|
7
7
|
before :all do
|
8
|
-
FileUtils.mkdir_p(Delayed::Settings.expand_rails_path(
|
8
|
+
FileUtils.mkdir_p(Delayed::Settings.expand_rails_path("tmp"))
|
9
9
|
Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
|
10
10
|
Delayed::Settings.parent_process = {
|
11
|
-
|
11
|
+
"server_address" => "/tmp/inst-jobs-test.sock"
|
12
12
|
}
|
13
13
|
end
|
14
14
|
|
@@ -16,43 +16,43 @@ RSpec.describe Delayed::WorkQueue::ParentProcess do
|
|
16
16
|
Delayed::Settings.parent_process = {}
|
17
17
|
end
|
18
18
|
|
19
|
-
after
|
20
|
-
File.unlink(
|
19
|
+
after do
|
20
|
+
File.unlink("/tmp/inst-jobs-test.sock") if File.exist?("/tmp/inst-jobs-test.sock")
|
21
21
|
Delayed::Worker.lifecycle.reset!
|
22
22
|
end
|
23
23
|
|
24
24
|
let(:subject) { described_class.new }
|
25
25
|
|
26
|
-
describe
|
27
|
-
it
|
28
|
-
queue = described_class.new(
|
29
|
-
expect(queue.server_address).to eq Delayed::Settings.expand_rails_path(
|
26
|
+
describe "#initalize(config = Settings.parent_process)" do
|
27
|
+
it "must expand a relative path to be within the Rails root" do
|
28
|
+
queue = described_class.new("server_address" => "tmp/foo.sock")
|
29
|
+
expect(queue.server_address).to eq Delayed::Settings.expand_rails_path("tmp/foo.sock")
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
33
|
-
queue = described_class.new(
|
34
|
-
expect(queue.server_address).to eq Delayed::Settings.expand_rails_path(
|
32
|
+
it "must add a file name when a relative path to a directory is supplied" do
|
33
|
+
queue = described_class.new("server_address" => "tmp")
|
34
|
+
expect(queue.server_address).to eq Delayed::Settings.expand_rails_path("tmp/inst-jobs.sock")
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
38
|
-
queue = described_class.new(
|
39
|
-
expect(queue.server_address).to eq
|
37
|
+
it "must capture a full absolute path" do
|
38
|
+
queue = described_class.new("server_address" => "/tmp/foo.sock")
|
39
|
+
expect(queue.server_address).to eq "/tmp/foo.sock"
|
40
40
|
end
|
41
41
|
|
42
|
-
it
|
43
|
-
queue = described_class.new(
|
44
|
-
expect(queue.server_address).to eq
|
42
|
+
it "must add a file name when an absolute path to a directory is supplied" do
|
43
|
+
queue = described_class.new("server_address" => "/tmp")
|
44
|
+
expect(queue.server_address).to eq "/tmp/inst-jobs.sock"
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
it
|
48
|
+
it "generates a server listening on a valid unix socket" do
|
49
49
|
server = subject.server
|
50
50
|
expect(server).to be_a(Delayed::WorkQueue::ParentProcess::Server)
|
51
51
|
expect(server.listen_socket.local_address.unix?).to be(true)
|
52
52
|
expect { server.listen_socket.accept_nonblock }.to raise_error(IO::WaitReadable)
|
53
53
|
end
|
54
54
|
|
55
|
-
it
|
55
|
+
it "generates a client connected to the server unix socket" do
|
56
56
|
server = subject.server
|
57
57
|
client = subject.client
|
58
58
|
expect(client).to be_a(Delayed::WorkQueue::ParentProcess::Client)
|
@@ -1,50 +1,50 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe Delayed::Worker::ConsulHealthCheck do
|
6
|
-
let(:health_check) {
|
6
|
+
let(:health_check) { described_class.new(worker_name: "foobar") }
|
7
7
|
|
8
|
-
describe
|
9
|
-
it
|
10
|
-
check =
|
11
|
-
expect(check.service_client.configuration.url.to_s).to eq
|
8
|
+
describe "#initialize" do
|
9
|
+
it "must use a default service client when the config is mostly empty" do
|
10
|
+
check = described_class.new(worker_name: "foobar")
|
11
|
+
expect(check.service_client.configuration.url.to_s).to eq "http://localhost:8500"
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
15
|
-
check =
|
14
|
+
it "must create a new service API client when the config has relevant keys set" do
|
15
|
+
check = described_class.new(worker_name: "foobar",
|
16
|
+
config: { url: "http://consul.example.com:8500" })
|
16
17
|
service_client = check.service_client
|
17
|
-
expect(service_client.configuration.url.to_s).to eq
|
18
|
+
expect(service_client.configuration.url.to_s).to eq "http://consul.example.com:8500"
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
21
|
-
describe
|
22
|
-
it
|
22
|
+
describe "#start" do
|
23
|
+
it "must register this process as a service with consul" do
|
23
24
|
stub = stub_request(:put, "localhost:8500/v1/agent/service/register")
|
24
|
-
|
25
|
+
.with(body: hash_including({ id: "foobar" }))
|
25
26
|
|
26
27
|
health_check.start
|
27
28
|
|
28
29
|
expect(stub).to have_been_requested
|
29
30
|
end
|
30
31
|
|
31
|
-
|
32
|
-
it 'must supply a args style check' do
|
32
|
+
it "must supply a args style check" do
|
33
33
|
stub = stub_request(:put, "localhost:8500/v1/agent/service/register")
|
34
|
-
|
34
|
+
.with(body: hash_including({ check: WebMock::API.hash_including({ args: anything }) }))
|
35
35
|
|
36
36
|
health_check.start
|
37
37
|
|
38
38
|
expect(stub).to have_been_requested
|
39
39
|
end
|
40
40
|
|
41
|
-
it
|
41
|
+
it "must include the docker container id when the docker option is set to true" do
|
42
42
|
stub = stub_request(:put, "localhost:8500/v1/agent/service/register")
|
43
|
-
|
43
|
+
.with(body: hash_including({ check: WebMock::API.hash_including({ docker_container_id: anything }) }))
|
44
44
|
|
45
|
-
local_health_check =
|
46
|
-
worker_name:
|
47
|
-
config: {docker: true}
|
45
|
+
local_health_check = described_class.new(
|
46
|
+
worker_name: "foobar",
|
47
|
+
config: { docker: true }
|
48
48
|
)
|
49
49
|
local_health_check.start
|
50
50
|
|
@@ -52,8 +52,8 @@ RSpec.describe Delayed::Worker::ConsulHealthCheck do
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
describe
|
56
|
-
it
|
55
|
+
describe "#stop" do
|
56
|
+
it "must deregister the service from consul" do
|
57
57
|
stub = stub_request(:put, "localhost:8500/v1/agent/service/deregister/foobar")
|
58
58
|
|
59
59
|
health_check.stop
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe Delayed::Worker::HealthCheck do
|
6
6
|
let(:klass) { Class.new(Delayed::Worker::HealthCheck) { self.type_name = :test } }
|
@@ -10,116 +10,118 @@ RSpec.describe Delayed::Worker::HealthCheck do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
after do
|
13
|
-
|
13
|
+
described_class.subclasses.delete(klass)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "must maintain a list of its subclasses" do
|
17
17
|
klass
|
18
|
-
expect(
|
18
|
+
expect(described_class.subclasses).to include klass
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
it
|
23
|
-
check =
|
21
|
+
describe ".build(type:, config: {})" do
|
22
|
+
it "must select the concrete class to use by the type_name in the subclass" do
|
23
|
+
check = described_class.build(type: "test", worker_name: "foobar")
|
24
24
|
expect(check).to be_a(klass)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "must raise ArgumentError when the specified type doesn't exist" do
|
28
|
-
expect
|
29
|
-
|
30
|
-
|
28
|
+
expect do
|
29
|
+
described_class.build(type: "nope", config: { worker_name: "foobar" })
|
30
|
+
end.to raise_error ArgumentError
|
31
31
|
end
|
32
32
|
|
33
|
-
it
|
34
|
-
config = {foo:
|
35
|
-
check =
|
33
|
+
it "must initiaize the specified class using the supplied config" do
|
34
|
+
config = { foo: "bar" }.with_indifferent_access
|
35
|
+
check = described_class.build(type: "test", worker_name: "foobar", config: config)
|
36
36
|
expect(check.config).to eq config
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
describe
|
41
|
-
let(:klass)
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
describe ".reschedule_abandoned_jobs" do
|
41
|
+
let(:klass) do
|
42
|
+
Class.new(Delayed::Worker::HealthCheck) do
|
43
|
+
self.type_name = :fake
|
44
|
+
class << self
|
45
|
+
attr_accessor :live_workers
|
46
|
+
end
|
47
|
+
|
48
|
+
def live_workers
|
49
|
+
self.class.live_workers
|
50
|
+
end
|
45
51
|
end
|
46
|
-
|
47
|
-
def live_workers
|
48
|
-
self.class.live_workers
|
49
|
-
end
|
50
|
-
} }
|
52
|
+
end
|
51
53
|
|
52
54
|
let(:initial_run_at) { Time.zone.now }
|
53
55
|
|
54
56
|
before do
|
55
|
-
klass.live_workers = %w
|
57
|
+
klass.live_workers = %w[alive]
|
56
58
|
Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
|
57
59
|
|
58
60
|
2.times { Delayed::Job.enqueue(SimpleJob.new, run_at: initial_run_at, max_attempts: 4) }
|
59
61
|
@alive_job = Delayed::Job.first
|
60
62
|
@alive_job.update!({
|
61
|
-
|
62
|
-
|
63
|
-
|
63
|
+
locked_by: "alive",
|
64
|
+
locked_at: initial_run_at
|
65
|
+
})
|
64
66
|
@dead_job = Delayed::Job.last
|
65
67
|
@dead_job.update!({
|
66
|
-
|
67
|
-
|
68
|
-
|
68
|
+
locked_by: "dead",
|
69
|
+
locked_at: initial_run_at
|
70
|
+
})
|
69
71
|
Delayed::Settings.worker_health_check_type = :fake
|
70
72
|
Delayed::Settings.worker_health_check_config = {}
|
71
73
|
end
|
72
74
|
|
73
75
|
after do
|
74
|
-
|
76
|
+
described_class.subclasses.delete(klass)
|
75
77
|
Delayed::Settings.worker_health_check_type = :none
|
76
78
|
Delayed::Settings.worker_health_check_config = {}
|
77
79
|
end
|
78
80
|
|
79
|
-
it
|
80
|
-
|
81
|
+
it "must leave jobs locked by live workers alone" do
|
82
|
+
described_class.reschedule_abandoned_jobs
|
81
83
|
@alive_job.reload
|
82
84
|
expect(@alive_job.run_at.to_i).to eq initial_run_at.to_i
|
83
85
|
expect(@alive_job.locked_at.to_i).to eq initial_run_at.to_i
|
84
|
-
expect(@alive_job.locked_by).to eq
|
86
|
+
expect(@alive_job.locked_by).to eq "alive"
|
85
87
|
end
|
86
88
|
|
87
|
-
it
|
88
|
-
|
89
|
+
it "must reschedule jobs locked by dead workers" do
|
90
|
+
described_class.reschedule_abandoned_jobs
|
89
91
|
@dead_job.reload
|
90
92
|
expect(@dead_job.run_at).to be > initial_run_at
|
91
93
|
expect(@dead_job.locked_at).to be_nil
|
92
94
|
expect(@dead_job.locked_by).to be_nil
|
93
95
|
end
|
94
96
|
|
95
|
-
it
|
96
|
-
Delayed::Job.where(id: @dead_job).update_all(locked_by:
|
97
|
+
it "ignores jobs that are re-locked after fetching from db" do
|
98
|
+
Delayed::Job.where(id: @dead_job).update_all(locked_by: "someone_else")
|
97
99
|
allow(Delayed::Job).to receive(:running_jobs).and_return([@dead_job])
|
98
|
-
|
100
|
+
described_class.reschedule_abandoned_jobs
|
99
101
|
@dead_job.reload
|
100
|
-
expect(@dead_job.locked_by).to eq
|
102
|
+
expect(@dead_job.locked_by).to eq "someone_else"
|
101
103
|
end
|
102
104
|
|
103
|
-
it
|
104
|
-
Delayed::Job.where(id: @dead_job).update_all(locked_by:
|
105
|
+
it "ignores jobs that are prefetched" do
|
106
|
+
Delayed::Job.where(id: @dead_job).update_all(locked_by: "prefetch:some_node")
|
105
107
|
allow(Delayed::Job).to receive(:running_jobs).and_return([@dead_job])
|
106
|
-
|
108
|
+
described_class.reschedule_abandoned_jobs
|
107
109
|
@dead_job.reload
|
108
|
-
expect(@dead_job.locked_by).to eq
|
110
|
+
expect(@dead_job.locked_by).to eq "prefetch:some_node"
|
109
111
|
end
|
110
112
|
|
111
113
|
it "bails immediately if advisory lock already taken" do
|
112
|
-
allow(
|
113
|
-
|
114
|
+
allow(described_class).to receive(:attempt_advisory_lock).and_return(false)
|
115
|
+
described_class.reschedule_abandoned_jobs
|
114
116
|
@dead_job.reload
|
115
117
|
expect(@dead_job.run_at.to_i).to eq(initial_run_at.to_i)
|
116
|
-
expect(@dead_job.locked_at).
|
117
|
-
expect(@dead_job.locked_by).
|
118
|
+
expect(@dead_job.locked_at).not_to be_nil
|
119
|
+
expect(@dead_job.locked_by).not_to be_nil
|
118
120
|
end
|
119
121
|
end
|
120
122
|
|
121
|
-
describe
|
122
|
-
it
|
123
|
+
describe "#initialize" do
|
124
|
+
it "must raise ArgumentError when the worker name is not supplied" do
|
123
125
|
expect { klass.new }.to raise_error ArgumentError
|
124
126
|
end
|
125
127
|
end
|