inst-jobs 2.4.10 → 3.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrate/20210929204903_update_conflicting_singleton_function_to_use_index.rb +27 -0
  3. data/db/migrate/20211101190934_update_after_delete_trigger_for_singleton_index.rb +137 -0
  4. data/db/migrate/20211207094200_update_after_delete_trigger_for_singleton_transition_cases.rb +171 -0
  5. data/db/migrate/20211220112800_fix_singleton_race_condition_insert.rb +59 -0
  6. data/db/migrate/20211220113000_fix_singleton_race_condition_delete.rb +207 -0
  7. data/db/migrate/20220127091200_fix_singleton_unique_constraint.rb +31 -0
  8. data/db/migrate/20220128084800_update_insert_trigger_for_singleton_unique_constraint_change.rb +60 -0
  9. data/db/migrate/20220128084900_update_delete_trigger_for_singleton_unique_constraint_change.rb +209 -0
  10. data/db/migrate/20220203063200_remove_old_singleton_index.rb +31 -0
  11. data/db/migrate/20220328152900_add_failed_jobs_indicies.rb +12 -0
  12. data/db/migrate/20220519204546_add_requeued_job_id_to_failed_jobs.rb +7 -0
  13. data/exe/inst_jobs +1 -1
  14. data/lib/delayed/backend/active_record.rb +62 -15
  15. data/lib/delayed/backend/base.rb +20 -5
  16. data/lib/delayed/batch.rb +1 -1
  17. data/lib/delayed/lifecycle.rb +8 -1
  18. data/lib/delayed/message_sending.rb +1 -1
  19. data/lib/delayed/periodic.rb +1 -1
  20. data/lib/delayed/pool.rb +12 -2
  21. data/lib/delayed/rails_reloader_plugin.rb +30 -0
  22. data/lib/delayed/server.rb +8 -2
  23. data/lib/delayed/settings.rb +3 -1
  24. data/lib/delayed/version.rb +1 -1
  25. data/lib/delayed/work_queue/parent_process/server.rb +43 -12
  26. data/lib/delayed/worker/health_check.rb +1 -1
  27. data/lib/delayed/worker/process_helper.rb +3 -3
  28. data/lib/delayed/worker.rb +6 -24
  29. metadata +65 -83
  30. data/spec/active_record_job_spec.rb +0 -294
  31. data/spec/delayed/cli_spec.rb +0 -25
  32. data/spec/delayed/daemon_spec.rb +0 -38
  33. data/spec/delayed/message_sending_spec.rb +0 -108
  34. data/spec/delayed/periodic_spec.rb +0 -32
  35. data/spec/delayed/server_spec.rb +0 -103
  36. data/spec/delayed/settings_spec.rb +0 -48
  37. data/spec/delayed/work_queue/in_process_spec.rb +0 -31
  38. data/spec/delayed/work_queue/parent_process/client_spec.rb +0 -87
  39. data/spec/delayed/work_queue/parent_process/server_spec.rb +0 -233
  40. data/spec/delayed/work_queue/parent_process_spec.rb +0 -60
  41. data/spec/delayed/worker/consul_health_check_spec.rb +0 -63
  42. data/spec/delayed/worker/health_check_spec.rb +0 -134
  43. data/spec/delayed/worker_spec.rb +0 -100
  44. data/spec/migrate/20140924140513_add_story_table.rb +0 -9
  45. data/spec/sample_jobs.rb +0 -79
  46. data/spec/shared/delayed_batch.rb +0 -105
  47. data/spec/shared/delayed_method.rb +0 -287
  48. data/spec/shared/performable_method.rb +0 -75
  49. data/spec/shared/shared_backend.rb +0 -989
  50. data/spec/shared/testing.rb +0 -50
  51. data/spec/shared/worker.rb +0 -413
  52. data/spec/shared_jobs_specs.rb +0 -17
  53. data/spec/spec_helper.rb +0 -134
@@ -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,233 +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
- it "doesn't respond immediately if there are no jobs available" do
108
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
109
- subject.run_once
110
-
111
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return({}).ordered
112
- Marshal.dump(args, client)
113
- subject.run_once
114
- expect(client).not_to be_ready
115
-
116
- # next time around, return the result
117
- expect(Delayed::Job).to receive(:get_and_lock_next_available)
118
- .with(*job_args)
119
- .and_return("worker_name" => job)
120
- .ordered
121
- allow(Delayed::Settings).to receive(:sleep_delay).and_return(0)
122
- allow(Delayed::Settings).to receive(:sleep_delay_stagger).and_return(0)
123
- subject.run_once
124
- expect(client).to be_ready
125
- expect(Marshal.load(client)).to eq(job)
126
- end
127
-
128
- it "drops the client on i/o error" do
129
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
130
- subject.run_once
131
-
132
- Marshal.dump(args, client)
133
-
134
- expect(Marshal).to receive(:load).and_raise(IOError.new("socket went away"))
135
- expect { subject.run_once }.to change(subject, :connected_clients).by(-1)
136
- end
137
-
138
- it "drops the client when the client disconnects" do
139
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
140
- subject.run_once
141
-
142
- Marshal.dump(args, client)
143
- # make sure the server knows the client is waiting for a job
144
- subject.run_once
145
-
146
- client.close
147
- expect { subject.run_once }.to change(subject, :connected_clients).by(-1)
148
- expect(subject.instance_variable_get(:@waiting_clients).first.last).to eq []
149
- end
150
-
151
- it "drops the client when a write fails" do
152
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
153
- subject.run_once
154
-
155
- Marshal.dump(args, client)
156
- subject.run_once
157
-
158
- client.close
159
-
160
- server_client_socket = subject.clients.keys.first
161
- # don't let the server see the close and process it there; we want to check a failure later
162
- expect(subject).to receive(:handle_request).with(server_client_socket)
163
-
164
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
165
- # the job gets unlocked
166
- expect(Delayed::Job).to receive(:unlock).with([job])
167
- subject.run_once
168
-
169
- # and the server removes the client from both of its internal state arrays
170
- expect(subject.connected_clients).to eq 0
171
- expect(subject.instance_variable_get(:@waiting_clients).first.last).to eq []
172
- end
173
-
174
- it "tracks when clients are idle" do
175
- expect(subject.all_workers_idle?).to be(true)
176
-
177
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
178
- subject.run_once
179
- expect(subject.all_workers_idle?).to be(true)
180
-
181
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
182
- Marshal.dump(args, client)
183
- subject.run_once
184
- expect(subject.all_workers_idle?).to be(false)
185
-
186
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return({})
187
- Marshal.dump(args, client)
188
- subject.run_once
189
- expect(subject.all_workers_idle?).to be(true)
190
- end
191
-
192
- it "triggers the lifecycle event around the pop" do
193
- called = false
194
- client = Socket.unix(subject.listen_socket.local_address.unix_path)
195
- subject.run_once
196
-
197
- Delayed::Worker.lifecycle.around(:work_queue_pop) do |queue, &cb|
198
- expect(subject.all_workers_idle?).to be(true)
199
- expect(queue).to eq(subject)
200
- expect(Delayed::Job).to receive(:get_and_lock_next_available).with(*job_args).and_return("worker_name" => job)
201
- called = true
202
- res = cb.call(queue)
203
- expect(subject.all_workers_idle?).to be(false)
204
- res
205
- end
206
-
207
- Marshal.dump(args, client)
208
- subject.run_once
209
-
210
- expect(Marshal.load(client)).to eq(job)
211
- expect(called).to eq(true)
212
- end
213
-
214
- it "deletes the correct worker when transferring jobs" do
215
- client1 = Socket.unix(subject.listen_socket.local_address.unix_path)
216
- client2 = Socket.unix(subject.listen_socket.local_address.unix_path)
217
- subject.run_once
218
- subject.run_once
219
-
220
- Marshal.dump(args, client1)
221
- Marshal.dump(["worker_name2", worker_config], client2)
222
- subject.run_once
223
- subject.run_once
224
-
225
- waiting_clients = subject.instance_variable_get(:@waiting_clients)
226
- expect(waiting_clients.first.last.length).to eq 2
227
-
228
- expect(Delayed::Job).to receive(:get_and_lock_next_available).and_return("worker_name" => job,
229
- "worker_name2" => job)
230
- subject.run_once
231
- expect(waiting_clients.first.last).to be_empty
232
- end
233
- 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
@@ -1,100 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../spec_helper"
4
-
5
- describe Delayed::Worker do
6
- subject { described_class.new(worker_config.dup) }
7
-
8
- let(:worker_config) do
9
- {
10
- queue: "test", min_priority: 1, max_priority: 2, stuff: "stuff"
11
- }.freeze
12
- end
13
- let(:job_attrs) do
14
- {
15
- id: 42, name: "testjob", full_name: "testfullname", :last_error= => nil,
16
- attempts: 1, reschedule: nil, :expired? => false,
17
- payload_object: {}, priority: 25
18
- }.freeze
19
- end
20
-
21
- after { described_class.lifecycle.reset! }
22
-
23
- describe "#perform" do
24
- it "fires off an error callback when a job raises an exception" do
25
- fired = false
26
- described_class.lifecycle.before(:error) { |_worker, _exception| fired = true }
27
- job = double(job_attrs)
28
- output_count = subject.perform(job)
29
- expect(fired).to be_truthy
30
- expect(output_count).to eq(1)
31
- end
32
-
33
- it "uses the retry callback for a retriable exception" do
34
- error_fired = retry_fired = false
35
- described_class.lifecycle.before(:error) { |_worker, _exception| error_fired = true }
36
- described_class.lifecycle.before(:retry) { |_worker, _exception| retry_fired = true }
37
- job = Delayed::Job.new(payload_object: {}, priority: 25, strand: "test_jobs", max_attempts: 3)
38
- expect(job).to receive(:invoke_job) do
39
- raise Delayed::RetriableError, "that's all this job does"
40
- end
41
- output_count = subject.perform(job)
42
- expect(error_fired).to be_falsey
43
- expect(retry_fired).to be_truthy
44
- expect(output_count).to eq(1)
45
- end
46
-
47
- it "reloads" do
48
- fake_application = double("Rails.application",
49
- config: double("Rails.application.config",
50
- cache_classes: false,
51
- reload_classes_only_on_change: false),
52
- reloader: double)
53
-
54
- allow(Rails).to receive(:application).and_return(fake_application)
55
- if Rails::VERSION::MAJOR >= 5
56
- expect(Rails.application.reloader).to receive(:reload!).once
57
- else
58
- expect(ActionDispatch::Reloader).to receive(:prepare!).once
59
- expect(ActionDispatch::Reloader).to receive(:cleanup!).once
60
- end
61
- job = double(job_attrs)
62
- subject.perform(job)
63
- end
64
- end
65
-
66
- describe "#log_job" do
67
- around do |block|
68
- prev_logger = Delayed::Settings.job_detailed_log_format
69
- block.call
70
- Delayed::Settings.job_detailed_log_format = prev_logger
71
- end
72
-
73
- it "has a reasonable default format" do
74
- payload = double(perform: nil)
75
- job = Delayed::Job.new(payload_object: payload, priority: 25, strand: "test_jobs")
76
- short_log_format = subject.log_job(job, :short)
77
- expect(short_log_format).to eq("RSpec::Mocks::Double")
78
- long_format = subject.log_job(job, :long)
79
- expect(long_format).to eq("RSpec::Mocks::Double {\"priority\":25,\"attempts\":0,\"created_at\":null,\"tag\":\"RSpec::Mocks::Double#perform\",\"max_attempts\":null,\"strand\":\"test_jobs\",\"source\":null}") # rubocop:disable Layout/LineLength
80
- end
81
-
82
- it "logging format can be changed with settings" do
83
- Delayed::Settings.job_detailed_log_format = ->(job) { "override format #{job.strand}" }
84
- payload = double(perform: nil)
85
- job = Delayed::Job.new(payload_object: payload, priority: 25, strand: "test_jobs")
86
- short_log_format = subject.log_job(job, :short)
87
- expect(short_log_format).to eq("RSpec::Mocks::Double")
88
- long_format = subject.log_job(job, :long)
89
- expect(long_format).to eq("RSpec::Mocks::Double override format test_jobs")
90
- end
91
- end
92
-
93
- describe "#run" do
94
- it "passes extra config options through to the WorkQueue" do
95
- expect(subject.work_queue).to receive(:get_and_lock_next_available)
96
- .with(subject.name, worker_config).and_return(nil)
97
- subject.run
98
- end
99
- end
100
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class AddStoryTable < ActiveRecord::Migration[4.2]
4
- def change
5
- create_table :stories do |table|
6
- table.string :text
7
- end
8
- end
9
- end