inst-jobs 3.0.8 → 3.1.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +4 -2
  3. data/db/migrate/20200825011002_add_strand_order_override.rb +2 -1
  4. data/db/migrate/20210809145804_add_n_strand_index.rb +2 -1
  5. data/db/migrate/20220328152900_add_failed_jobs_indicies.rb +12 -0
  6. data/db/migrate/20220519204546_add_requeued_job_id_to_failed_jobs.rb +7 -0
  7. data/lib/delayed/backend/active_record.rb +77 -7
  8. data/lib/delayed/backend/base.rb +28 -10
  9. data/lib/delayed/batch.rb +1 -1
  10. data/lib/delayed/cli.rb +3 -2
  11. data/lib/delayed/lifecycle.rb +9 -2
  12. data/lib/delayed/log_tailer.rb +2 -2
  13. data/lib/delayed/message_sending.rb +9 -6
  14. data/lib/delayed/performable_method.rb +22 -16
  15. data/lib/delayed/periodic.rb +2 -2
  16. data/lib/delayed/pool.rb +12 -2
  17. data/lib/delayed/server.rb +8 -2
  18. data/lib/delayed/settings.rb +5 -1
  19. data/lib/delayed/version.rb +1 -1
  20. data/lib/delayed/work_queue/parent_process/server.rb +7 -3
  21. data/lib/delayed/worker/health_check.rb +1 -1
  22. data/lib/delayed/worker/process_helper.rb +4 -4
  23. data/lib/delayed/worker.rb +22 -11
  24. metadata +18 -119
  25. data/spec/active_record_job_spec.rb +0 -294
  26. data/spec/delayed/cli_spec.rb +0 -25
  27. data/spec/delayed/daemon_spec.rb +0 -38
  28. data/spec/delayed/message_sending_spec.rb +0 -108
  29. data/spec/delayed/periodic_spec.rb +0 -32
  30. data/spec/delayed/server_spec.rb +0 -103
  31. data/spec/delayed/settings_spec.rb +0 -48
  32. data/spec/delayed/work_queue/in_process_spec.rb +0 -31
  33. data/spec/delayed/work_queue/parent_process/client_spec.rb +0 -87
  34. data/spec/delayed/work_queue/parent_process/server_spec.rb +0 -280
  35. data/spec/delayed/work_queue/parent_process_spec.rb +0 -60
  36. data/spec/delayed/worker/consul_health_check_spec.rb +0 -63
  37. data/spec/delayed/worker/health_check_spec.rb +0 -134
  38. data/spec/delayed/worker_spec.rb +0 -105
  39. data/spec/migrate/20140924140513_add_story_table.rb +0 -9
  40. data/spec/sample_jobs.rb +0 -79
  41. data/spec/shared/delayed_batch.rb +0 -105
  42. data/spec/shared/delayed_method.rb +0 -287
  43. data/spec/shared/performable_method.rb +0 -75
  44. data/spec/shared/shared_backend.rb +0 -1221
  45. data/spec/shared/testing.rb +0 -50
  46. data/spec/shared/worker.rb +0 -413
  47. data/spec/shared_jobs_specs.rb +0 -17
  48. data/spec/spec_helper.rb +0 -136
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Delayed::WorkQueue::ParentProcess::Client do
6
- subject { described_class.new(addrinfo).tap(&:init) }
7
-
8
- let(:addrinfo) { double("Addrinfo") }
9
- let(:connection) { double("Socket") }
10
- let(:job) { Delayed::Job.new(locked_by: "worker_name") }
11
- let(:worker_config) { { queue: "queue_name", min_priority: 1, max_priority: 2 } }
12
- let(:args) { ["worker_name", worker_config] }
13
- let(:job_args) { [["worker_name"], "queue_name", 1, 2] }
14
-
15
- before :all do
16
- FileUtils.mkdir_p(Delayed::Settings.expand_rails_path("tmp"))
17
- Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
18
- end
19
-
20
- it "marshals the given arguments to the server and returns the response" do
21
- expect(addrinfo).to receive(:connect).once.and_return(connection)
22
- expect(connection).to receive(:eof?).and_return(false)
23
- expect(IO).to receive(:select).and_return([[connection], nil, nil])
24
- expect(Marshal).to receive(:dump).with(args, connection).ordered
25
- expect(Marshal).to receive(:load).with(connection).and_return(job).ordered
26
- response = subject.get_and_lock_next_available(*args)
27
- expect(response).to eq(job)
28
- end
29
-
30
- it "returns nil and then reconnects on socket write error" do
31
- expect(addrinfo).to receive(:connect).once.and_return(connection)
32
- expect(Marshal).to receive(:dump).and_raise(SystemCallError.new("failure"))
33
- expect(IO).to receive(:select).and_return([[connection], nil, nil])
34
- expect(connection).to receive(:close)
35
- response = subject.get_and_lock_next_available(*args)
36
- expect(response).to be_nil
37
-
38
- expect(addrinfo).to receive(:connect).once.and_return(connection)
39
- expect(Marshal).to receive(:dump).with(args, connection)
40
- expect(connection).to receive(:eof?).and_return(false)
41
- expect(Marshal).to receive(:load).with(connection).and_return(job)
42
- response = subject.get_and_lock_next_available(*args)
43
- expect(response).to eq(job)
44
- end
45
-
46
- it "returns nil and then reconnects when the socket indicates eof" do
47
- expect(addrinfo).to receive(:connect).once.and_return(connection)
48
- expect(connection).to receive(:eof?).and_return(true)
49
- expect(Marshal).to receive(:dump).with(args, connection).ordered
50
- expect(IO).to receive(:select).and_return([[connection], nil, nil])
51
- expect(connection).to receive(:close)
52
- response = subject.get_and_lock_next_available(*args)
53
- expect(response).to be_nil
54
-
55
- expect(addrinfo).to receive(:connect).once.and_return(connection)
56
- expect(Marshal).to receive(:dump).with(args, connection)
57
- expect(connection).to receive(:eof?).and_return(false)
58
- expect(Marshal).to receive(:load).with(connection).and_return(job)
59
- response = subject.get_and_lock_next_available(*args)
60
- expect(response).to eq(job)
61
- end
62
-
63
- it "errors if the response is not a locked job" do
64
- expect(addrinfo).to receive(:connect).once.and_return(connection)
65
- expect(Marshal).to receive(:dump).with(args, connection)
66
- expect(IO).to receive(:select).and_return([[connection], nil, nil])
67
- expect(Marshal).to receive(:load).with(connection).and_return(:not_a_job)
68
- expect(connection).to receive(:eof?).and_return(false)
69
-
70
- expect do
71
- subject.get_and_lock_next_available(*args)
72
- end.to raise_error(Delayed::WorkQueue::ParentProcess::ProtocolError)
73
- end
74
-
75
- it "errors if the response is a job not locked by this worker" do
76
- expect(addrinfo).to receive(:connect).once.and_return(connection)
77
- expect(Marshal).to receive(:dump).with(args, connection)
78
- job.locked_by = "somebody_else"
79
- expect(IO).to receive(:select).and_return([[connection], nil, nil])
80
- expect(Marshal).to receive(:load).with(connection).and_return(job)
81
- expect(connection).to receive(:eof?).and_return(false)
82
-
83
- expect do
84
- subject.get_and_lock_next_available(*args)
85
- end.to raise_error(Delayed::WorkQueue::ParentProcess::ProtocolError)
86
- end
87
- end
@@ -1,280 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- class JobClass
6
- attr_reader :id
7
-
8
- def initialize
9
- @id = rand
10
- end
11
-
12
- def ==(other)
13
- id == other.id
14
- end
15
- end
16
-
17
- RSpec.describe Delayed::WorkQueue::ParentProcess::Server do
18
- subject { described_class.new(listen_socket) }
19
-
20
- let(:parent) { Delayed::WorkQueue::ParentProcess.new }
21
- let(:listen_socket) { Socket.unix_server_socket(parent.server_address) }
22
- let(:job) { JobClass.new }
23
- let(:worker_config) { { queue: "queue_name", min_priority: 1, max_priority: 2 } }
24
- let(:args) { ["worker_name", worker_config] }
25
- let(:job_args) { [["worker_name"], "queue_name", 1, 2, hash_including(prefetch: 4)] }
26
-
27
- before do
28
- Delayed::Worker.lifecycle.reset!
29
- end
30
-
31
- before :all do
32
- Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
33
- Delayed::Settings.parent_process = {
34
- "server_address" => "/tmp/inst-jobs-test.sock"
35
- }
36
- end
37
-
38
- after :all do
39
- Delayed::Settings.parent_process = {}
40
- end
41
-
42
- after do
43
- File.unlink("/tmp/inst-jobs-test.sock") if File.exist?("/tmp/inst-jobs-test.sock")
44
- Delayed::Worker.lifecycle.reset!
45
- end
46
-
47
- it "accepts new clients" do
48
- Socket.unix(subject.listen_socket.local_address.unix_path)
49
- expect { subject.run_once }.to change(subject, :connected_clients).by(1)
50
- end
51
-
52
- it "queries the queue on client request" do
53
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
54
- subject.run_once
55
-
56
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
57
- Marshal.dump(args, client)
58
- subject.run_once
59
- expect(client).to be_ready
60
- expect(Marshal.load(client)).to eq(job)
61
- end
62
-
63
- it "can pop multiple jobs at once" do
64
- client1 = Socket.unix(subject.listen_socket.local_address.unix_path)
65
- subject.run_once
66
- client2 = Socket.unix(subject.listen_socket.local_address.unix_path)
67
- subject.run_once
68
-
69
- job1 = JobClass.new
70
- job2 = JobClass.new
71
- job_args = [%w[worker_name1 worker_name2], "queue_name", 1, 2, hash_including(prefetch: 3)]
72
- jobs = { "worker_name1" => job1, "worker_name2" => job2 }
73
-
74
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return(jobs)
75
- Marshal.dump(["worker_name1", worker_config], client1)
76
- Marshal.dump(["worker_name2", worker_config], client2)
77
- subject.run_once
78
- expect(Marshal.load(client1)).to eq(job1)
79
- expect(Marshal.load(client2)).to eq(job2)
80
- end
81
-
82
- it "will prefetch and use jobs" do
83
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
84
- subject.run_once
85
-
86
- allow(subject).to receive(:prefetch_owner).and_return("work_queue:X")
87
- job_args = [["worker_name1"], "queue_name", 1, 2,
88
- { prefetch: 4, prefetch_owner: "work_queue:X", forced_latency: 6.0 }]
89
- job2 = Delayed::Job.new(tag: "tag")
90
- job2.create_and_lock!("work_queue:X")
91
- job3 = Delayed::Job.new(tag: "tag")
92
- job3.create_and_lock!("work_queue:X")
93
- jobs = { "worker_name1" => job, "work_queue:X" => [job2, job3] }
94
-
95
- expect(Delayed::Job).to receive(:get_and_lock_next_available).once.with(*job_args).and_return(jobs)
96
- Marshal.dump(["worker_name1", worker_config], client)
97
- subject.run_once
98
- expect(subject).not_to be_all_workers_idle
99
- expect(Marshal.load(client)).to eq(job)
100
-
101
- Marshal.dump(["worker_name1", worker_config], client)
102
- subject.run_once
103
- expect(subject).not_to be_all_workers_idle
104
- expect(Marshal.load(client)).to eq(job2)
105
- end
106
-
107
- context "prefetched job unlocking" do
108
- let(:job_args) do
109
- [["worker_name1"], "queue_name", 1, 2,
110
- { prefetch: 4, prefetch_owner: "prefetch:work_queue:X", forced_latency: 6.0 }]
111
- end
112
- let(:job2) { Delayed::Job.new(tag: "tag").tap { |j| j.create_and_lock!("prefetch:work_queue:X") } }
113
- let(:job3) { Delayed::Job.new(tag: "tag").tap { |j| j.create_and_lock!("prefetch:work_queue:X") } }
114
-
115
- before do
116
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
117
- subject.run_once
118
-
119
- jobs = { "worker_name1" => job, "prefetch:work_queue:X" => [job2, job3] }
120
- allow(subject).to receive(:prefetch_owner).and_return("prefetch:work_queue:X")
121
- allow(Delayed::Job).to receive(:get_and_lock_next_available).once.with(*job_args).and_return(jobs)
122
- Marshal.dump(["worker_name1", worker_config], client)
123
- subject.run_once
124
- end
125
-
126
- it "doesn't unlock anything if nothing is timed out" do
127
- expect(Delayed::Job).not_to receive(:advisory_lock)
128
- expect(Delayed::Job).not_to receive(:unlock)
129
- subject.unlock_timed_out_prefetched_jobs
130
- end
131
-
132
- it "unlocks timed out prefetched jobs" do
133
- allow(Delayed::Settings).to receive(:parent_process).and_return(prefetched_jobs_timeout: -1)
134
- expect(Delayed::Job).to receive(:unlock).with([job2, job3])
135
- subject.unlock_timed_out_prefetched_jobs
136
- expect(subject.instance_variable_get(:@prefetched_jobs).values.sum(&:length)).to eq 0
137
- end
138
-
139
- it "fails gracefully if the lock times out" do
140
- allow(Delayed::Settings).to receive(:parent_process).and_return(prefetched_jobs_timeout: -1)
141
- expect(Delayed::Job).not_to receive(:unlock)
142
- expect(Delayed::Job).to receive(:advisory_lock).and_raise(ActiveRecord::QueryCanceled)
143
- subject.unlock_timed_out_prefetched_jobs
144
- expect(subject.instance_variable_get(:@prefetched_jobs).values.sum(&:length)).to eq 2
145
- end
146
-
147
- it "unlocks all jobs" do
148
- expect(Delayed::Job).to receive(:unlock).with([job2, job3])
149
- subject.unlock_all_prefetched_jobs
150
- expect(subject.instance_variable_get(:@prefetched_jobs).values.sum(&:length)).to eq 0
151
- end
152
- end
153
-
154
- it "doesn't respond immediately if there are no jobs available" do
155
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
156
- subject.run_once
157
-
158
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return({}).ordered
159
- Marshal.dump(args, client)
160
- subject.run_once
161
- expect(client).not_to be_ready
162
-
163
- # next time around, return the result
164
- expect(Delayed::Job).to receive(:get_and_lock_next_available)
165
- .with(*job_args)
166
- .and_return("worker_name" => job)
167
- .ordered
168
- allow(Delayed::Settings).to receive(:sleep_delay).and_return(0)
169
- allow(Delayed::Settings).to receive(:sleep_delay_stagger).and_return(0)
170
- subject.run_once
171
- expect(client).to be_ready
172
- expect(Marshal.load(client)).to eq(job)
173
- end
174
-
175
- it "drops the client on i/o error" do
176
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
177
- subject.run_once
178
-
179
- Marshal.dump(args, client)
180
-
181
- expect(Marshal).to receive(:load).and_raise(IOError.new("socket went away"))
182
- expect { subject.run_once }.to change(subject, :connected_clients).by(-1)
183
- end
184
-
185
- it "drops the client when the client disconnects" do
186
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
187
- subject.run_once
188
-
189
- Marshal.dump(args, client)
190
- # make sure the server knows the client is waiting for a job
191
- subject.run_once
192
-
193
- client.close
194
- expect { subject.run_once }.to change(subject, :connected_clients).by(-1)
195
- expect(subject.instance_variable_get(:@waiting_clients).first.last).to eq []
196
- end
197
-
198
- it "drops the client when a write fails" do
199
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
200
- subject.run_once
201
-
202
- Marshal.dump(args, client)
203
- subject.run_once
204
-
205
- client.close
206
-
207
- server_client_socket = subject.clients.keys.first
208
- # don't let the server see the close and process it there; we want to check a failure later
209
- expect(subject).to receive(:handle_request).with(server_client_socket)
210
-
211
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
212
- # the job gets unlocked
213
- expect(Delayed::Job).to receive(:unlock).with([job])
214
- subject.run_once
215
-
216
- # and the server removes the client from both of its internal state arrays
217
- expect(subject.connected_clients).to eq 0
218
- expect(subject.instance_variable_get(:@waiting_clients).first.last).to eq []
219
- end
220
-
221
- it "tracks when clients are idle" do
222
- expect(subject.all_workers_idle?).to be(true)
223
-
224
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
225
- subject.run_once
226
- expect(subject.all_workers_idle?).to be(true)
227
-
228
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
229
- Marshal.dump(args, client)
230
- subject.run_once
231
- expect(subject.all_workers_idle?).to be(false)
232
-
233
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return({})
234
- Marshal.dump(args, client)
235
- subject.run_once
236
- expect(subject.all_workers_idle?).to be(true)
237
- end
238
-
239
- it "triggers the lifecycle event around the pop" do
240
- called = false
241
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
242
- subject.run_once
243
-
244
- Delayed::Worker.lifecycle.around(:work_queue_pop) do |queue, &cb|
245
- expect(subject.all_workers_idle?).to be(true)
246
- expect(queue).to eq(subject)
247
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
248
- called = true
249
- res = cb.call(queue)
250
- expect(subject.all_workers_idle?).to be(false)
251
- res
252
- end
253
-
254
- Marshal.dump(args, client)
255
- subject.run_once
256
-
257
- expect(Marshal.load(client)).to eq(job)
258
- expect(called).to eq(true)
259
- end
260
-
261
- it "deletes the correct worker when transferring jobs" do
262
- client1 = Socket.unix(subject.listen_socket.local_address.unix_path)
263
- client2 = Socket.unix(subject.listen_socket.local_address.unix_path)
264
- subject.run_once
265
- subject.run_once
266
-
267
- Marshal.dump(args, client1)
268
- Marshal.dump(["worker_name2", worker_config], client2)
269
- subject.run_once
270
- subject.run_once
271
-
272
- waiting_clients = subject.instance_variable_get(:@waiting_clients)
273
- expect(waiting_clients.first.last.length).to eq 2
274
-
275
- expect(Delayed::Job).to receive(:get_and_lock_next_available).and_return("worker_name" => job,
276
- "worker_name2" => job)
277
- subject.run_once
278
- expect(waiting_clients.first.last).to be_empty
279
- end
280
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require "fileutils"
5
-
6
- RSpec.describe Delayed::WorkQueue::ParentProcess do
7
- before :all do
8
- FileUtils.mkdir_p(Delayed::Settings.expand_rails_path("tmp"))
9
- Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
10
- Delayed::Settings.parent_process = {
11
- "server_address" => "/tmp/inst-jobs-test.sock"
12
- }
13
- end
14
-
15
- after :all do
16
- Delayed::Settings.parent_process = {}
17
- end
18
-
19
- after do
20
- File.unlink("/tmp/inst-jobs-test.sock") if File.exist?("/tmp/inst-jobs-test.sock")
21
- Delayed::Worker.lifecycle.reset!
22
- end
23
-
24
- describe "#initalize(config = Settings.parent_process)" do
25
- it "must expand a relative path to be within the Rails root" do
26
- queue = described_class.new("server_address" => "tmp/foo.sock")
27
- expect(queue.server_address).to eq Delayed::Settings.expand_rails_path("tmp/foo.sock")
28
- end
29
-
30
- it "must add a file name when a relative path to a directory is supplied" do
31
- queue = described_class.new("server_address" => "tmp")
32
- expect(queue.server_address).to eq Delayed::Settings.expand_rails_path("tmp/inst-jobs.sock")
33
- end
34
-
35
- it "must capture a full absolute path" do
36
- queue = described_class.new("server_address" => "/tmp/foo.sock")
37
- expect(queue.server_address).to eq "/tmp/foo.sock"
38
- end
39
-
40
- it "must add a file name when an absolute path to a directory is supplied" do
41
- queue = described_class.new("server_address" => "/tmp")
42
- expect(queue.server_address).to eq "/tmp/inst-jobs.sock"
43
- end
44
- end
45
-
46
- it "generates a server listening on a valid unix socket" do
47
- server = subject.server
48
- expect(server).to be_a(Delayed::WorkQueue::ParentProcess::Server)
49
- expect(server.listen_socket.local_address.unix?).to be(true)
50
- expect { server.listen_socket.accept_nonblock }.to raise_error(IO::WaitReadable)
51
- end
52
-
53
- it "generates a client connected to the server unix socket" do
54
- server = subject.server
55
- client = subject.client
56
- expect(client).to be_a(Delayed::WorkQueue::ParentProcess::Client)
57
- expect(client.addrinfo.unix?).to be(true)
58
- expect(client.addrinfo.unix_path).to eq(server.listen_socket.local_address.unix_path)
59
- end
60
- end
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Delayed::Worker::ConsulHealthCheck do
6
- let(:health_check) { described_class.new(worker_name: "foobar") }
7
-
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
- end
13
-
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" })
17
- service_client = check.service_client
18
- expect(service_client.configuration.url.to_s).to eq "http://consul.example.com:8500"
19
- end
20
- end
21
-
22
- describe "#start" do
23
- it "must register this process as a service with consul" do
24
- stub = stub_request(:put, "localhost:8500/v1/agent/service/register")
25
- .with(body: hash_including({ id: "foobar" }))
26
-
27
- health_check.start
28
-
29
- expect(stub).to have_been_requested
30
- end
31
-
32
- it "must supply a args style check" do
33
- stub = stub_request(:put, "localhost:8500/v1/agent/service/register")
34
- .with(body: hash_including({ check: WebMock::API.hash_including({ args: anything }) }))
35
-
36
- health_check.start
37
-
38
- expect(stub).to have_been_requested
39
- end
40
-
41
- it "must include the docker container id when the docker option is set to true" do
42
- stub = stub_request(:put, "localhost:8500/v1/agent/service/register")
43
- .with(body: hash_including({ check: WebMock::API.hash_including({ docker_container_id: anything }) }))
44
-
45
- local_health_check = described_class.new(
46
- worker_name: "foobar",
47
- config: { docker: true }
48
- )
49
- local_health_check.start
50
-
51
- expect(stub).to have_been_requested
52
- end
53
- end
54
-
55
- describe "#stop" do
56
- it "must deregister the service from consul" do
57
- stub = stub_request(:put, "localhost:8500/v1/agent/service/deregister/foobar")
58
-
59
- health_check.stop
60
- expect(stub).to have_been_requested
61
- end
62
- end
63
- end
@@ -1,134 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Delayed::Worker::HealthCheck do
6
- let(:klass) { Class.new(Delayed::Worker::HealthCheck) { self.type_name = :test } }
7
-
8
- before do
9
- klass # Gotta make sure the class has been defined before we try to use it
10
- end
11
-
12
- after do
13
- described_class.subclasses.delete(klass)
14
- end
15
-
16
- it "must maintain a list of its subclasses" do
17
- klass
18
- expect(described_class.subclasses).to include klass
19
- end
20
-
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
- expect(check).to be_a(klass)
25
- end
26
-
27
- it "must raise ArgumentError when the specified type doesn't exist" do
28
- expect do
29
- described_class.build(type: "nope", config: { worker_name: "foobar" })
30
- end.to raise_error ArgumentError
31
- end
32
-
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
- expect(check.config).to eq config
37
- end
38
- end
39
-
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
51
- end
52
- end
53
-
54
- let(:initial_run_at) { 10.minutes.ago }
55
-
56
- before do
57
- klass.live_workers = %w[alive]
58
- Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
59
-
60
- 2.times { Delayed::Job.enqueue(SimpleJob.new, run_at: initial_run_at, max_attempts: 4) }
61
- @alive_job = Delayed::Job.first
62
- @alive_job.update!({
63
- locked_by: "alive",
64
- locked_at: initial_run_at
65
- })
66
- @dead_job = Delayed::Job.last
67
- @dead_job.update!({
68
- locked_by: "dead",
69
- locked_at: initial_run_at
70
- })
71
- Delayed::Settings.worker_health_check_type = :fake
72
- Delayed::Settings.worker_health_check_config = {}
73
- end
74
-
75
- after do
76
- described_class.subclasses.delete(klass)
77
- Delayed::Settings.worker_health_check_type = :none
78
- Delayed::Settings.worker_health_check_config = {}
79
- end
80
-
81
- it "must leave jobs locked by live workers alone" do
82
- described_class.reschedule_abandoned_jobs
83
- @alive_job.reload
84
- expect(@alive_job.run_at.to_i).to eq initial_run_at.to_i
85
- expect(@alive_job.locked_at.to_i).to eq initial_run_at.to_i
86
- expect(@alive_job.locked_by).to eq "alive"
87
- end
88
-
89
- it "must reschedule jobs locked by dead workers" do
90
- described_class.reschedule_abandoned_jobs
91
- @dead_job.reload
92
- expect(@dead_job.run_at).to be > initial_run_at
93
- expect(@dead_job.locked_at).to be_nil
94
- expect(@dead_job.locked_by).to be_nil
95
- end
96
-
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")
99
- # we need to return @dead_job itself, which doesn't match the database
100
- jobs_scope = double
101
- allow(jobs_scope).to receive(:where).and_return(jobs_scope)
102
- allow(jobs_scope).to receive(:not).and_return(jobs_scope)
103
- allow(jobs_scope).to receive(:limit).and_return(jobs_scope)
104
- allow(jobs_scope).to receive(:to_a).and_return([@dead_job], [])
105
- allow(Delayed::Job).to receive(:running_jobs).and_return(jobs_scope)
106
- described_class.reschedule_abandoned_jobs
107
- @dead_job.reload
108
- expect(@dead_job.locked_by).to eq "someone_else"
109
- end
110
-
111
- it "ignores jobs that are prefetched" do
112
- Delayed::Job.where(id: @dead_job).update_all(locked_by: "prefetch:some_node")
113
- allow(Delayed::Job).to receive(:running_jobs).and_return(Delayed::Job.where(id: @dead_job.id))
114
- described_class.reschedule_abandoned_jobs
115
- @dead_job.reload
116
- expect(@dead_job.locked_by).to eq "prefetch:some_node"
117
- end
118
-
119
- it "bails immediately if advisory lock already taken" do
120
- allow(Delayed::Job).to receive(:attempt_advisory_lock).and_return(false)
121
- described_class.reschedule_abandoned_jobs
122
- @dead_job.reload
123
- expect(@dead_job.run_at.to_i).to eq(initial_run_at.to_i)
124
- expect(@dead_job.locked_at).not_to be_nil
125
- expect(@dead_job.locked_by).not_to be_nil
126
- end
127
- end
128
-
129
- describe "#initialize" do
130
- it "must raise ArgumentError when the worker name is not supplied" do
131
- expect { klass.new }.to raise_error ArgumentError
132
- end
133
- end
134
- end