inst-jobs 3.1.2 → 3.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/delayed/lifecycle.rb +7 -1
- data/lib/delayed/version.rb +1 -1
- metadata +3 -51
- data/spec/active_record_job_spec.rb +0 -326
- data/spec/delayed/cli_spec.rb +0 -25
- data/spec/delayed/daemon_spec.rb +0 -38
- data/spec/delayed/message_sending_spec.rb +0 -108
- data/spec/delayed/periodic_spec.rb +0 -32
- data/spec/delayed/server_spec.rb +0 -103
- data/spec/delayed/settings_spec.rb +0 -48
- data/spec/delayed/work_queue/in_process_spec.rb +0 -31
- data/spec/delayed/work_queue/parent_process/client_spec.rb +0 -87
- data/spec/delayed/work_queue/parent_process/server_spec.rb +0 -280
- data/spec/delayed/work_queue/parent_process_spec.rb +0 -60
- data/spec/delayed/worker/consul_health_check_spec.rb +0 -63
- data/spec/delayed/worker/health_check_spec.rb +0 -134
- data/spec/delayed/worker_spec.rb +0 -106
- data/spec/migrate/20140924140513_add_story_table.rb +0 -9
- data/spec/sample_jobs.rb +0 -79
- data/spec/shared/delayed_batch.rb +0 -105
- data/spec/shared/delayed_method.rb +0 -287
- data/spec/shared/performable_method.rb +0 -75
- data/spec/shared/shared_backend.rb +0 -1238
- data/spec/shared/testing.rb +0 -50
- data/spec/shared/worker.rb +0 -413
- data/spec/shared_jobs_specs.rb +0 -17
- data/spec/spec_helper.rb +0 -138
data/spec/delayed/server_spec.rb
DELETED
@@ -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 be(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 be(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
|