inst-jobs 2.3.1 → 2.4.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/db/migrate/20101216224513_create_delayed_jobs.rb +9 -7
- data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +8 -13
- data/db/migrate/20110610213249_optimize_delayed_jobs.rb +8 -8
- data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +25 -25
- data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +4 -8
- data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +1 -3
- data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +11 -15
- data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +1 -1
- data/db/migrate/20120608191051_add_jobs_run_at_index.rb +2 -2
- data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +1 -1
- data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +2 -3
- data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +9 -13
- data/db/migrate/20151210162949_improve_max_concurrent.rb +4 -8
- data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +3 -2
- data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +13 -17
- data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +8 -8
- data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +72 -77
- data/db/migrate/20200825011002_add_strand_order_override.rb +93 -97
- data/db/migrate/20210809145804_add_n_strand_index.rb +12 -0
- data/db/migrate/20210812210128_add_singleton_column.rb +200 -0
- data/db/migrate/20210917232626_add_delete_conflicting_singletons_before_unlock_trigger.rb +27 -0
- data/exe/inst_jobs +3 -2
- data/lib/delayed/backend/active_record.rb +204 -150
- data/lib/delayed/backend/base.rb +107 -77
- data/lib/delayed/batch.rb +11 -9
- data/lib/delayed/cli.rb +98 -84
- data/lib/delayed/core_ext/kernel.rb +4 -2
- data/lib/delayed/daemon.rb +70 -74
- data/lib/delayed/job_tracking.rb +26 -25
- data/lib/delayed/lifecycle.rb +27 -24
- data/lib/delayed/log_tailer.rb +17 -17
- data/lib/delayed/logging.rb +13 -16
- data/lib/delayed/message_sending.rb +43 -52
- data/lib/delayed/performable_method.rb +6 -8
- data/lib/delayed/periodic.rb +72 -65
- data/lib/delayed/plugin.rb +2 -4
- data/lib/delayed/pool.rb +198 -192
- data/lib/delayed/server/helpers.rb +6 -6
- data/lib/delayed/server.rb +51 -54
- data/lib/delayed/settings.rb +93 -81
- data/lib/delayed/testing.rb +21 -22
- data/lib/delayed/version.rb +1 -1
- data/lib/delayed/work_queue/in_process.rb +21 -17
- data/lib/delayed/work_queue/parent_process/client.rb +55 -53
- data/lib/delayed/work_queue/parent_process/server.rb +219 -208
- data/lib/delayed/work_queue/parent_process.rb +52 -53
- data/lib/delayed/worker/consul_health_check.rb +21 -19
- data/lib/delayed/worker/health_check.rb +29 -22
- data/lib/delayed/worker/null_health_check.rb +3 -1
- data/lib/delayed/worker/process_helper.rb +8 -9
- data/lib/delayed/worker.rb +271 -261
- data/lib/delayed/yaml_extensions.rb +12 -10
- data/lib/delayed_job.rb +37 -38
- data/lib/inst-jobs.rb +1 -1
- data/spec/active_record_job_spec.rb +129 -136
- data/spec/delayed/cli_spec.rb +7 -7
- data/spec/delayed/daemon_spec.rb +8 -8
- data/spec/delayed/message_sending_spec.rb +16 -9
- data/spec/delayed/periodic_spec.rb +13 -12
- data/spec/delayed/server_spec.rb +38 -38
- data/spec/delayed/settings_spec.rb +26 -25
- data/spec/delayed/work_queue/in_process_spec.rb +7 -7
- data/spec/delayed/work_queue/parent_process/client_spec.rb +16 -12
- data/spec/delayed/work_queue/parent_process/server_spec.rb +43 -40
- data/spec/delayed/work_queue/parent_process_spec.rb +21 -21
- data/spec/delayed/worker/consul_health_check_spec.rb +22 -22
- data/spec/delayed/worker/health_check_spec.rb +60 -52
- data/spec/delayed/worker_spec.rb +28 -25
- data/spec/sample_jobs.rb +45 -15
- data/spec/shared/delayed_batch.rb +74 -67
- data/spec/shared/delayed_method.rb +143 -102
- data/spec/shared/performable_method.rb +39 -38
- data/spec/shared/shared_backend.rb +550 -437
- data/spec/shared/testing.rb +14 -14
- data/spec/shared/worker.rb +155 -147
- data/spec/shared_jobs_specs.rb +13 -13
- data/spec/spec_helper.rb +46 -41
- metadata +79 -53
- data/lib/delayed/backend/redis/bulk_update.lua +0 -50
- data/lib/delayed/backend/redis/destroy_job.lua +0 -2
- data/lib/delayed/backend/redis/enqueue.lua +0 -29
- data/lib/delayed/backend/redis/fail_job.lua +0 -5
- data/lib/delayed/backend/redis/find_available.lua +0 -3
- data/lib/delayed/backend/redis/functions.rb +0 -59
- data/lib/delayed/backend/redis/get_and_lock_next_available.lua +0 -17
- data/lib/delayed/backend/redis/includes/jobs_common.lua +0 -203
- data/lib/delayed/backend/redis/job.rb +0 -528
- data/lib/delayed/backend/redis/set_running.lua +0 -5
- data/lib/delayed/backend/redis/tickle_strand.lua +0 -2
- data/spec/gemfiles/52.gemfile +0 -7
- data/spec/gemfiles/60.gemfile +0 -7
- data/spec/gemfiles/61.gemfile +0 -7
- data/spec/redis_job_spec.rb +0 -148
@@ -1,243 +1,254 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Delayed
|
4
|
-
module WorkQueue
|
5
|
-
class ParentProcess
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
4
|
+
module WorkQueue
|
5
|
+
class ParentProcess
|
6
|
+
class Server
|
7
|
+
attr_reader :clients, :listen_socket
|
8
|
+
|
9
|
+
include Delayed::Logging
|
10
|
+
SIGNALS = %i[INT TERM QUIT].freeze
|
11
|
+
|
12
|
+
def initialize(listen_socket, parent_pid: nil, config: Settings.parent_process)
|
13
|
+
@listen_socket = listen_socket
|
14
|
+
@parent_pid = parent_pid
|
15
|
+
@clients = {}
|
16
|
+
@waiting_clients = {}
|
17
|
+
@prefetched_jobs = {}
|
18
|
+
|
19
|
+
@config = config
|
20
|
+
@client_timeout = config["server_socket_timeout"] || 10.0 # left for backwards compat
|
21
|
+
|
22
|
+
@exit = false
|
23
|
+
@self_pipe = IO.pipe
|
24
|
+
end
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
def connected_clients
|
27
|
+
@clients.size
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def all_workers_idle?
|
31
|
+
@clients.none? { |_, c| c.working }
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
# run the server queue worker
|
35
|
+
# this method does not return, only exits or raises an exception
|
36
|
+
def run
|
37
|
+
logger.debug "Starting work queue process"
|
38
|
+
|
39
|
+
SIGNALS.each do |sig|
|
40
|
+
# We're not doing any aggressive exiting here since we really want
|
41
|
+
# prefetched jobs to be unlocked and we're going to wake up the process
|
42
|
+
# from the IO.select we're using to wait on clients.
|
43
|
+
trap(sig) do
|
44
|
+
@exit = true
|
45
|
+
@self_pipe[1].write_nonblock(".", exception: false)
|
46
|
+
end
|
47
|
+
end
|
38
48
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
49
|
+
last_orphaned_prefetched_jobs_purge = Job.db_time_now - rand(15 * 60)
|
50
|
+
until exit?
|
51
|
+
run_once
|
52
|
+
if last_orphaned_prefetched_jobs_purge + (15 * 60) < Job.db_time_now
|
53
|
+
Job.unlock_orphaned_prefetched_jobs
|
54
|
+
last_orphaned_prefetched_jobs_purge = Job.db_time_now
|
55
|
+
end
|
56
|
+
end
|
57
|
+
rescue => e
|
58
|
+
logger.error "WorkQueue Server died: #{e.inspect}"
|
59
|
+
raise
|
60
|
+
ensure
|
61
|
+
unlock_all_prefetched_jobs
|
62
|
+
end
|
45
63
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
64
|
+
def run_once
|
65
|
+
handles = @clients.keys + [@listen_socket, @self_pipe[0]]
|
66
|
+
# if we're currently idle, then force a "latency" to job fetching - don't
|
67
|
+
# fetch recently queued jobs, allowing busier workers to fetch them first.
|
68
|
+
# if they're not keeping up, the jobs will slip back in time, and suddenly we'll become
|
69
|
+
# active and quickly pick up all the jobs we can. The latency is calculated to ensure that
|
70
|
+
# an active worker is guaranteed to have attempted to fetch new jobs in the meantime
|
71
|
+
forced_latency = Settings.sleep_delay + (Settings.sleep_delay_stagger * 2) if all_workers_idle?
|
72
|
+
timeout = Settings.sleep_delay + (rand * Settings.sleep_delay_stagger)
|
73
|
+
readable, = IO.select(handles, nil, nil, timeout)
|
74
|
+
readable&.each { |s| handle_read(s) }
|
75
|
+
Delayed::Worker.lifecycle.run_callbacks(:check_for_work, self) do
|
76
|
+
check_for_work(forced_latency: forced_latency)
|
77
|
+
end
|
78
|
+
unlock_timed_out_prefetched_jobs
|
52
79
|
end
|
53
|
-
end
|
54
80
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
81
|
+
def handle_read(socket)
|
82
|
+
if socket == @listen_socket
|
83
|
+
handle_accept
|
84
|
+
elsif socket == @self_pipe[0]
|
85
|
+
# We really don't care about the contents of the pipe, we just need to
|
86
|
+
# wake up.
|
87
|
+
@self_pipe[0].read_nonblock(11, exception: false)
|
88
|
+
else
|
89
|
+
handle_request(socket)
|
90
|
+
end
|
91
|
+
end
|
61
92
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
readable, _, _ = IO.select(handles, nil, nil, timeout)
|
72
|
-
if readable
|
73
|
-
readable.each { |s| handle_read(s) }
|
74
|
-
end
|
75
|
-
Delayed::Worker.lifecycle.run_callbacks(:check_for_work, self) do
|
76
|
-
check_for_work(forced_latency: forced_latency)
|
77
|
-
end
|
78
|
-
unlock_timed_out_prefetched_jobs
|
79
|
-
end
|
93
|
+
# Any error on the listen socket other than WaitReadable will bubble up
|
94
|
+
# and terminate the work queue process, to be restarted by the parent daemon.
|
95
|
+
def handle_accept
|
96
|
+
socket, _addr = @listen_socket.accept_nonblock
|
97
|
+
@clients[socket] = ClientState.new(false, socket) if socket
|
98
|
+
rescue IO::WaitReadable
|
99
|
+
logger.error("Server attempted to read listen_socket but failed with IO::WaitReadable")
|
100
|
+
# ignore and just try accepting again next time through the loop
|
101
|
+
end
|
80
102
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
103
|
+
def handle_request(socket)
|
104
|
+
# There is an assumption here that the client will never send a partial
|
105
|
+
# request and then leave the socket open. Doing so would leave us hanging
|
106
|
+
# in Marshal.load forever. This is only a reasonable assumption because we
|
107
|
+
# control the client.
|
108
|
+
client = @clients[socket]
|
109
|
+
if socket.eof?
|
110
|
+
logger.debug("Client #{client.name} closed connection")
|
111
|
+
return drop_socket(socket)
|
112
|
+
end
|
113
|
+
worker_name, worker_config = Marshal.load(socket)
|
114
|
+
client.name = worker_name
|
115
|
+
client.working = false
|
116
|
+
(@waiting_clients[worker_config] ||= []) << client
|
117
|
+
rescue SystemCallError, IOError => e
|
118
|
+
logger.error("Receiving message from client (#{socket}) failed: #{e.inspect}")
|
119
|
+
drop_socket(socket)
|
120
|
+
end
|
92
121
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
122
|
+
def check_for_work(forced_latency: nil)
|
123
|
+
@waiting_clients.each do |(worker_config, workers)|
|
124
|
+
prefetched_jobs = @prefetched_jobs[worker_config] ||= []
|
125
|
+
logger.debug("I have #{prefetched_jobs.length} jobs for #{workers.length} waiting workers")
|
126
|
+
while !prefetched_jobs.empty? && !workers.empty?
|
127
|
+
job = prefetched_jobs.shift
|
128
|
+
client = workers.shift
|
129
|
+
# couldn't re-lock it for some reason
|
130
|
+
logger.debug("Transferring prefetched job to #{client.name}")
|
131
|
+
unless job.transfer_lock!(from: prefetch_owner, to: client.name)
|
132
|
+
workers.unshift(client)
|
133
|
+
next
|
134
|
+
end
|
135
|
+
client.working = true
|
136
|
+
begin
|
137
|
+
logger.debug("Sending prefetched job #{job.id} to #{client.name}")
|
138
|
+
client_timeout { Marshal.dump(job, client.socket) }
|
139
|
+
rescue SystemCallError, IOError, Timeout::Error => e
|
140
|
+
logger.error("Failed to send pre-fetched job to #{client.name}: #{e.inspect}")
|
141
|
+
drop_socket(client.socket)
|
142
|
+
Delayed::Job.unlock([job])
|
143
|
+
end
|
144
|
+
end
|
104
145
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
146
|
+
next if workers.empty?
|
147
|
+
|
148
|
+
logger.debug("Fetching new work for #{workers.length} workers")
|
149
|
+
jobs_to_send = []
|
150
|
+
|
151
|
+
Delayed::Worker.lifecycle.run_callbacks(:work_queue_pop, self, worker_config) do
|
152
|
+
recipients = workers.map(&:name)
|
153
|
+
|
154
|
+
response = Delayed::Job.get_and_lock_next_available(
|
155
|
+
recipients,
|
156
|
+
worker_config[:queue],
|
157
|
+
worker_config[:min_priority],
|
158
|
+
worker_config[:max_priority],
|
159
|
+
prefetch: (Settings.fetch_batch_size * (worker_config[:workers] || 1)) - recipients.length,
|
160
|
+
prefetch_owner: prefetch_owner,
|
161
|
+
forced_latency: forced_latency
|
162
|
+
)
|
163
|
+
logger.debug(
|
164
|
+
"Fetched and locked #{response.values.flatten.size} new jobs for workers (#{response.keys.join(', ')})."
|
165
|
+
)
|
166
|
+
response.each do |(worker_name, locked_jobs)|
|
167
|
+
if worker_name == prefetch_owner
|
168
|
+
# it's actually an array of all the extra jobs
|
169
|
+
logger.debug(
|
170
|
+
"Adding prefetched jobs #{locked_jobs.length} to prefetched array (size: #{prefetched_jobs.count})"
|
171
|
+
)
|
172
|
+
prefetched_jobs.concat(locked_jobs)
|
173
|
+
next
|
174
|
+
end
|
175
|
+
client = workers.find { |worker| worker.name == worker_name }
|
176
|
+
client.working = true
|
177
|
+
jobs_to_send << [client, locked_jobs]
|
178
|
+
end
|
179
|
+
end
|
123
180
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
next
|
181
|
+
jobs_to_send.each do |(recipient, job_to_send)|
|
182
|
+
@waiting_clients[worker_config].delete(client)
|
183
|
+
begin
|
184
|
+
logger.debug("Sending job #{job_to_send.id} to #{recipient.name}")
|
185
|
+
client_timeout { Marshal.dump(job_to_send, recipient.socket) }
|
186
|
+
rescue SystemCallError, IOError, Timeout::Error => e
|
187
|
+
logger.error("Failed to send job to #{recipient.name}: #{e.inspect}")
|
188
|
+
drop_socket(recipient.socket)
|
189
|
+
Delayed::Job.unlock([job_to_send])
|
190
|
+
end
|
191
|
+
end
|
136
192
|
end
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
Delayed::Job.
|
193
|
+
end
|
194
|
+
|
195
|
+
def unlock_timed_out_prefetched_jobs
|
196
|
+
@prefetched_jobs.each do |(worker_config, jobs)|
|
197
|
+
next if jobs.empty?
|
198
|
+
next unless jobs.first.locked_at < Time.now.utc - Settings.parent_process[:prefetched_jobs_timeout]
|
199
|
+
|
200
|
+
Delayed::Job.transaction do
|
201
|
+
Delayed::Job.advisory_lock(Delayed::Job.prefetch_jobs_lock_name)
|
202
|
+
Delayed::Job.unlock(jobs)
|
203
|
+
end
|
204
|
+
@prefetched_jobs[worker_config] = []
|
145
205
|
end
|
146
206
|
end
|
147
207
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
response = Delayed::Job.get_and_lock_next_available(
|
157
|
-
recipients,
|
158
|
-
worker_config[:queue],
|
159
|
-
worker_config[:min_priority],
|
160
|
-
worker_config[:max_priority],
|
161
|
-
prefetch: Settings.fetch_batch_size * (worker_config[:workers] || 1) - recipients.length,
|
162
|
-
prefetch_owner: prefetch_owner,
|
163
|
-
forced_latency: forced_latency)
|
164
|
-
logger.debug("Fetched and locked #{response.values.flatten.size} new jobs for workers (#{response.keys.join(', ')}).")
|
165
|
-
response.each do |(worker_name, job)|
|
166
|
-
if worker_name == prefetch_owner
|
167
|
-
# it's actually an array of all the extra jobs
|
168
|
-
logger.debug("Adding prefetched jobs #{job.length} to prefetched array (size: #{prefetched_jobs.count})")
|
169
|
-
prefetched_jobs.concat(job)
|
170
|
-
next
|
208
|
+
def unlock_all_prefetched_jobs
|
209
|
+
@prefetched_jobs.each do |(_worker_config, jobs)|
|
210
|
+
next if jobs.empty?
|
211
|
+
|
212
|
+
Delayed::Job.transaction do
|
213
|
+
Delayed::Job.advisory_lock(Delayed::Job.prefetch_jobs_lock_name)
|
214
|
+
Delayed::Job.unlock(jobs)
|
171
215
|
end
|
172
|
-
client = workers.find { |worker| worker.name == worker_name }
|
173
|
-
client.working = true
|
174
|
-
jobs_to_send << [client, job]
|
175
216
|
end
|
217
|
+
@prefetched_jobs = {}
|
176
218
|
end
|
177
219
|
|
178
|
-
|
179
|
-
|
220
|
+
def drop_socket(socket)
|
221
|
+
# this socket went away
|
180
222
|
begin
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
223
|
+
socket.close
|
224
|
+
rescue IOError
|
225
|
+
nil
|
226
|
+
end
|
227
|
+
client = @clients[socket]
|
228
|
+
@clients.delete(socket)
|
229
|
+
@waiting_clients.each do |(_config, workers)|
|
230
|
+
workers.delete(client)
|
187
231
|
end
|
188
232
|
end
|
189
|
-
end
|
190
|
-
end
|
191
233
|
|
192
|
-
|
193
|
-
|
194
|
-
next if jobs.empty?
|
195
|
-
if jobs.first.locked_at < Time.now.utc - Settings.parent_process[:prefetched_jobs_timeout]
|
196
|
-
Delayed::Job.unlock(jobs)
|
197
|
-
@prefetched_jobs[worker_config] = []
|
234
|
+
def exit?
|
235
|
+
!!@exit || parent_exited?
|
198
236
|
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
def unlock_all_prefetched_jobs
|
203
|
-
@prefetched_jobs.each do |(_worker_config, jobs)|
|
204
|
-
next if jobs.empty?
|
205
|
-
Delayed::Job.unlock(jobs)
|
206
|
-
end
|
207
|
-
@prefetched_jobs = {}
|
208
|
-
end
|
209
|
-
|
210
|
-
def drop_socket(socket)
|
211
|
-
# this socket went away
|
212
|
-
begin
|
213
|
-
socket.close
|
214
|
-
rescue IOError
|
215
|
-
end
|
216
|
-
client = @clients[socket]
|
217
|
-
@clients.delete(socket)
|
218
|
-
@waiting_clients.each do |(_config, workers)|
|
219
|
-
workers.delete(client)
|
220
|
-
end
|
221
|
-
end
|
222
237
|
|
223
|
-
|
224
|
-
|
225
|
-
|
238
|
+
def prefetch_owner
|
239
|
+
"prefetch:#{Socket.gethostname rescue 'X'}"
|
240
|
+
end
|
226
241
|
|
227
|
-
|
228
|
-
|
229
|
-
|
242
|
+
def parent_exited?
|
243
|
+
@parent_pid && @parent_pid != Process.ppid
|
244
|
+
end
|
230
245
|
|
231
|
-
|
232
|
-
|
233
|
-
|
246
|
+
def client_timeout(&block)
|
247
|
+
Timeout.timeout(@client_timeout, &block)
|
248
|
+
end
|
234
249
|
|
235
|
-
|
236
|
-
|
250
|
+
ClientState = Struct.new(:working, :socket, :name)
|
251
|
+
end
|
237
252
|
end
|
238
|
-
|
239
|
-
ClientState = Struct.new(:working, :socket, :name)
|
240
253
|
end
|
241
254
|
end
|
242
|
-
end
|
243
|
-
end
|
@@ -1,69 +1,68 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "pathname"
|
4
|
+
require "socket"
|
5
|
+
require "timeout"
|
6
6
|
|
7
|
-
require_relative
|
8
|
-
require_relative
|
7
|
+
require_relative "parent_process/client"
|
8
|
+
require_relative "parent_process/server"
|
9
9
|
|
10
10
|
module Delayed
|
11
|
-
module WorkQueue
|
12
|
-
# ParentProcess is a WorkQueue implementation that spawns a separate worker
|
13
|
-
# process for querying the queue. Each Worker child process sends requests to
|
14
|
-
# the ParentProcess via IPC, and receives responses. This centralized queue
|
15
|
-
# querying cuts down on db queries and lock contention, and allows the
|
16
|
-
# possibility for other centralized logic such as notifications when all workers
|
17
|
-
# are idle.
|
18
|
-
#
|
19
|
-
# The IPC implementation uses Unix stream sockets and Ruby's built-in Marshal
|
20
|
-
# functionality. The ParentProcess creates a Unix socket on the filesystem in
|
21
|
-
# the tmp directory, so that if a worker process dies and is restarted it can
|
22
|
-
# reconnect to the socket.
|
23
|
-
#
|
24
|
-
# While Unix and IP sockets are API compatible, we take a lot of shortcuts
|
25
|
-
# because we know it's just a local Unix socket. If we ever wanted to swap this
|
26
|
-
# out for a TCP/IP socket and have the WorkQueue running on another host, we'd
|
27
|
-
# want to be a lot more robust about partial reads/writes and timeouts.
|
28
|
-
class ParentProcess
|
29
|
-
|
30
|
-
|
11
|
+
module WorkQueue
|
12
|
+
# ParentProcess is a WorkQueue implementation that spawns a separate worker
|
13
|
+
# process for querying the queue. Each Worker child process sends requests to
|
14
|
+
# the ParentProcess via IPC, and receives responses. This centralized queue
|
15
|
+
# querying cuts down on db queries and lock contention, and allows the
|
16
|
+
# possibility for other centralized logic such as notifications when all workers
|
17
|
+
# are idle.
|
18
|
+
#
|
19
|
+
# The IPC implementation uses Unix stream sockets and Ruby's built-in Marshal
|
20
|
+
# functionality. The ParentProcess creates a Unix socket on the filesystem in
|
21
|
+
# the tmp directory, so that if a worker process dies and is restarted it can
|
22
|
+
# reconnect to the socket.
|
23
|
+
#
|
24
|
+
# While Unix and IP sockets are API compatible, we take a lot of shortcuts
|
25
|
+
# because we know it's just a local Unix socket. If we ever wanted to swap this
|
26
|
+
# out for a TCP/IP socket and have the WorkQueue running on another host, we'd
|
27
|
+
# want to be a lot more robust about partial reads/writes and timeouts.
|
28
|
+
class ParentProcess
|
29
|
+
class ProtocolError < RuntimeError
|
30
|
+
end
|
31
31
|
|
32
|
-
|
32
|
+
attr_reader :server_address
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
DEFAULT_SOCKET_NAME = "inst-jobs.sock"
|
35
|
+
private_constant :DEFAULT_SOCKET_NAME
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
def initialize(config = Settings.parent_process)
|
38
|
+
@config = config
|
39
|
+
@server_address = generate_socket_path(config["server_address"])
|
40
|
+
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
def server(parent_pid: nil)
|
43
|
+
# The unix_server_socket method takes care of cleaning up any existing
|
44
|
+
# socket for us if the work queue process dies and is restarted.
|
45
|
+
listen_socket = Socket.unix_server_socket(@server_address)
|
46
|
+
Server.new(listen_socket, parent_pid: parent_pid, config: @config)
|
47
|
+
end
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
def client
|
50
|
+
Client.new(Addrinfo.unix(@server_address), config: @config)
|
51
|
+
end
|
52
52
|
|
53
|
-
|
53
|
+
private
|
54
54
|
|
55
|
-
|
56
|
-
|
55
|
+
def generate_socket_path(supplied_path)
|
56
|
+
pathname = Pathname.new(supplied_path)
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
58
|
+
if pathname.absolute? && pathname.directory?
|
59
|
+
pathname.join(DEFAULT_SOCKET_NAME).to_s
|
60
|
+
elsif pathname.absolute?
|
61
|
+
supplied_path
|
62
|
+
else
|
63
|
+
generate_socket_path(Settings.expand_rails_path(supplied_path))
|
64
|
+
end
|
65
|
+
end
|
64
66
|
end
|
65
67
|
end
|
66
68
|
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|