inst-jobs 3.0.8 → 3.1.11

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 +63 -6
  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 +8 -5
  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 +8 -8
  24. metadata +16 -89
  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,108 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require "debug_inspector"
5
-
6
- RSpec.describe Delayed::MessageSending do
7
- before do
8
- allow(::Rails.env).to receive(:test?).and_return(true)
9
- end
10
-
11
- before(:all) do
12
- # this has to be a "real" constant
13
- class SpecClass # rubocop:disable RSpec/LeakyConstantDeclaration, Lint/ConstantDefinitionInBlock
14
- def call_private(**enqueue_args)
15
- delay(**enqueue_args).private_method
16
- end
17
-
18
- def call_protected(**enqueue_args)
19
- other = self.class.new
20
- other.delay(**enqueue_args).protected_method
21
- end
22
-
23
- def call_public(**_kwargs)
24
- 42
25
- end
26
-
27
- private
28
-
29
- def private_method; end
30
-
31
- protected
32
-
33
- def protected_method; end
34
- end
35
- end
36
-
37
- after(:all) do
38
- Object.send(:remove_const, :SpecClass)
39
- end
40
-
41
- let(:klass) { SpecClass }
42
-
43
- it "allows an object to send a private message to itself" do
44
- job = klass.new.call_private(ignore_transaction: true)
45
- job.invoke_job
46
- end
47
-
48
- it "allows an object to send a private message to itself synchronouosly" do
49
- klass.new.call_private(synchronous: true)
50
- end
51
-
52
- it "warns about directly sending a private message asynchronously" do
53
- expect { klass.new.delay.private_method }.to raise_error(NoMethodError)
54
- end
55
-
56
- it "warns about directly sending a private message synchronusly" do
57
- expect { klass.new.delay(synchronous: true).private_method }.to raise_error(NoMethodError)
58
- end
59
-
60
- it "does not warn about directly sending a private message in production" do
61
- allow(::Rails.env).to receive(:test?).and_return(false)
62
- allow(::Rails.env).to receive(:development?).and_return(false)
63
- klass.new.delay.private_method
64
- end
65
-
66
- it "does not warn about directly sending a private message synchronously in production" do
67
- allow(::Rails.env).to receive(:test?).and_return(false)
68
- allow(::Rails.env).to receive(:development?).and_return(false)
69
- klass.new.delay(synchronous: true).private_method
70
- end
71
-
72
- it "allows an object to send a protected message to itself" do
73
- job = klass.new.call_protected(ignore_transaction: true)
74
- job.invoke_job
75
- end
76
-
77
- it "allows an object to send a protected message to itself synchronouosly" do
78
- klass.new.call_protected(synchronous: true)
79
- end
80
-
81
- it "directly calls a public method on an object with kwargs" do
82
- expect(klass.new.delay(synchronous: true).call_public(kwarg: 10)).to eq 42
83
- end
84
-
85
- it "warns about directly sending a protected message asynchronously" do
86
- expect { klass.new.delay.protected_method }.to raise_error(NoMethodError)
87
- end
88
-
89
- it "warns about directly sending a protected message synchronusly" do
90
- expect { klass.new.delay(synchronous: true).protected_method }.to raise_error(NoMethodError)
91
- end
92
-
93
- it "doesn't explode if you can't dump the sender" do
94
- klass = Class.new do
95
- def delay_something
96
- Kernel.delay.sleep(1)
97
- end
98
-
99
- def encode_with(_encoder)
100
- raise "yaml encoding failed"
101
- end
102
- end
103
-
104
- obj = klass.new
105
- expect { YAML.dump(obj) }.to raise_error("yaml encoding failed")
106
- expect { obj.delay_something }.not_to raise_error
107
- end
108
- end
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Delayed::Periodic do
6
- around do |block|
7
- # make sure we can use ".cron" and
8
- # such safely without leaking global state
9
- prev_sched = described_class.scheduled
10
- prev_ovr = described_class.overrides
11
- described_class.scheduled = {}
12
- described_class.overrides = {}
13
- block.call
14
- ensure
15
- described_class.scheduled = prev_sched
16
- described_class.overrides = prev_ovr
17
- Delayed::Job.delete_all
18
- end
19
-
20
- describe ".cron" do
21
- let(:job_name) { "just a test" }
22
-
23
- it "provides a tag by default for periodic jobs" do
24
- described_class.cron job_name, "*/10 * * * *" do
25
- # no-op
26
- end
27
- instance = described_class.scheduled[job_name]
28
- expect(instance).not_to be_nil
29
- expect(instance.enqueue_args[:singleton]).to eq("periodic: just a test")
30
- end
31
- end
32
- end
@@ -1,103 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
- require "delayed/server"
5
-
6
- RSpec.describe Delayed::Server, sinatra: true do
7
- include Rack::Test::Methods
8
-
9
- @update = false
10
-
11
- def app
12
- described_class.new(update: @update)
13
- end
14
-
15
- def parsed_body
16
- @parsed_body ||= JSON.parse(last_response.body)
17
- end
18
-
19
- before :all do
20
- Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
21
- end
22
-
23
- describe "get '/running'" do
24
- before do
25
- 3.times do |i|
26
- Delayed::Job.create!({
27
- run_at: Time.zone.now,
28
- locked_at: Time.zone.now,
29
- locked_by: "dummy-runner-#{i}:${$$}"
30
- })
31
- end
32
- get "/running"
33
- end
34
-
35
- it "must return a json object with the running job data in an array", aggregate_failures: true do
36
- expect(last_response).to be_ok
37
- expect(parsed_body["data"]).to be_an Array
38
- expect(parsed_body["data"].size).to eq 3
39
- end
40
- end
41
-
42
- describe "get '/jobs'" do
43
- let!(:job1) { Delayed::Job.enqueue(SimpleJob.new, strand: "strand-1") }
44
- let!(:job2) { Delayed::Job.enqueue(SimpleJob.new, strand: "strand-2") }
45
- let!(:job3) { Delayed::Job.enqueue(SimpleJob.new, strand: "strand-3") }
46
-
47
- context "with the flavor param set to id" do
48
- before do
49
- get "/jobs?flavor=id&search_term=#{job2.id}"
50
- end
51
-
52
- it "must only return the job with the id specified in the search_term param" do
53
- jobs = parsed_body["data"]
54
- job_ids = jobs.map { |j| j["id"] }
55
- expect(job_ids).to eq [job2.id]
56
- end
57
-
58
- it "must set recordsFiltered in the response to 1" do
59
- expect(parsed_body["recordsFiltered"]).to eq 1
60
- end
61
- end
62
- end
63
-
64
- describe "post '/bulk_update'" do
65
- let!(:job1) { Delayed::Job.enqueue(SimpleJob.new, strand: "strand-1") }
66
- let!(:job2) { Delayed::Job.enqueue(SimpleJob.new, strand: "strand-2") }
67
- let!(:job3) { Delayed::Job.enqueue(SimpleJob.new, strand: "strand-3") }
68
-
69
- context "with update enabled" do
70
- before do
71
- @update = true
72
- post "/bulk_update", JSON.generate(action: "destroy", ids: [job1.id])
73
- end
74
-
75
- it "must remove job1" do
76
- expect { Delayed::Job.find(job1.id) }.to raise_error(ActiveRecord::RecordNotFound)
77
- expect(Delayed::Job.find(job2.id)).not_to be_nil
78
- expect(Delayed::Job.find(job3.id)).not_to be_nil
79
- end
80
-
81
- it "must return ok" do
82
- expect(last_response.ok?).to be true
83
- end
84
- end
85
-
86
- context "with update disabled" do
87
- before do
88
- @update = false
89
- post "/bulk_update", JSON.generate(action: "destroy", ids: [job1.id])
90
- end
91
-
92
- it "must not remove job1" do
93
- expect(Delayed::Job.find(job1.id)).not_to be_nil
94
- expect(Delayed::Job.find(job2.id)).not_to be_nil
95
- expect(Delayed::Job.find(job3.id)).not_to be_nil
96
- end
97
-
98
- it "must return forbidden" do
99
- expect(last_response.forbidden?).to be true
100
- end
101
- end
102
- end
103
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Delayed::Settings do
6
- let(:configfile) do
7
- <<~YAML
8
- default:
9
- workers:
10
- - queue: myqueue
11
- workers: 2
12
- - queue: secondqueue
13
- max_priority: 7
14
- max_attempts: 1
15
- YAML
16
- end
17
-
18
- describe ".worker_config" do
19
- it "merges each worker config with the top-level config" do
20
- expect(File).to receive(:read).with("fname").and_return(configfile)
21
- config = described_class.worker_config("fname")
22
- expect(config[:workers]).to eq([
23
- { "queue" => "myqueue", "workers" => 2, "max_attempts" => 1 },
24
- { "queue" => "secondqueue", "max_priority" => 7, "max_attempts" => 1 }
25
- ])
26
- end
27
- end
28
-
29
- describe ".apply_worker_config!" do
30
- it "applies global settings from the given config" do
31
- expect(described_class).to receive(:last_ditch_logfile=).with(true)
32
- described_class.apply_worker_config!("last_ditch_logfile" => true)
33
- end
34
-
35
- it "merges in parent_process overrides to default config" do
36
- described_class.apply_worker_config!("parent_process" => { "foo" => "bar" })
37
-
38
- expect(described_class.parent_process).to include("foo" => "bar")
39
- end
40
- end
41
-
42
- describe ".parent_process_client_timeout=" do
43
- it "must update the value in the parent_process settings hash" do
44
- described_class.parent_process_client_timeout = 42
45
- expect(described_class.parent_process["server_socket_timeout"]).to eq 42
46
- end
47
- end
48
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- RSpec.describe Delayed::WorkQueue::InProcess do
6
- before :all do
7
- Delayed.select_backend(Delayed::Backend::ActiveRecord::Job)
8
- end
9
-
10
- after do
11
- Delayed::Worker.lifecycle.reset!
12
- end
13
-
14
- let(:worker_config) { { queue: "test", min_priority: 1, max_priority: 2 } }
15
- let(:args) { ["worker_name", worker_config] }
16
-
17
- it "triggers the lifecycle event around the pop" do
18
- called = false
19
- Delayed::Worker.lifecycle.around(:work_queue_pop) do |queue, &cb|
20
- expect(queue).to eq(subject)
21
- expect(Delayed::Job).to receive(:get_and_lock_next_available)
22
- .with("worker_name", "test", 1, 2)
23
- .and_return(:job)
24
- called = true
25
- cb.call(queue)
26
- end
27
- job = subject.get_and_lock_next_available(*args)
28
- expect(job).to eq(:job)
29
- expect(called).to eq(true)
30
- end
31
- end
@@ -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