delayed_job_worker_pool 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +22 -10
- data/lib/delayed_job_worker_pool.rb +1 -0
- data/lib/delayed_job_worker_pool/dsl.rb +1 -1
- data/lib/delayed_job_worker_pool/version.rb +1 -1
- data/lib/delayed_job_worker_pool/worker.rb +2 -1
- data/lib/delayed_job_worker_pool/worker_info.rb +11 -0
- data/lib/delayed_job_worker_pool/worker_pool.rb +20 -11
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7e84e64a69a710244b682df783ea1f7f96ce72a
|
4
|
+
data.tar.gz: 75e5bbb9ffe986ded38517b4bb0fb72e7e5049c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 071b9234bfd7e3904bc44eb0957508a31bdd9975d644d7de77d06e3f9b0e8af5ccb0ca185c4bd9e754fde55c52698e4add16ebf873964d31d1af3101cb52d163
|
7
|
+
data.tar.gz: d98c55b40a03b8156d96ebfb870a56e75472101f61806dbab4b52300a6082808e225a4d4a738a689e53d0175d73688a7905e46974c9846080d05358176ca374a
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -45,22 +45,33 @@ sleep_delay ENV['WORKER_SLEEP_DELAY']
|
|
45
45
|
|
46
46
|
preload_app
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
# This runs in the master process after it preloads the app
|
49
|
+
after_preload_app do
|
50
|
+
puts "Master #{Process.pid} preloaded app"
|
51
|
+
|
52
|
+
# Don't hang on to database connections from the master after we've
|
53
|
+
# completed initialization
|
54
|
+
ActiveRecord::Base.connection_pool.disconnect!
|
50
55
|
end
|
51
56
|
|
52
|
-
|
57
|
+
# This runs in the worker processes after it has been forked
|
58
|
+
on_worker_boot do |worker_info|
|
53
59
|
puts "Worker #{Process.pid} started"
|
54
60
|
|
55
61
|
# Reconnect to the database
|
56
62
|
ActiveRecord::Base.establish_connection
|
57
63
|
end
|
58
64
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
65
|
+
# This runs in the master process after a worker starts
|
66
|
+
after_worker_boot do |worker_info|
|
67
|
+
puts "Master #{Process.pid} booted worker #{worker_info.name} with " \
|
68
|
+
"process id #{worker_info.process_id}"
|
69
|
+
end
|
70
|
+
|
71
|
+
# This runs in the master process after a worker shuts down
|
72
|
+
after_worker_shutdown do |worker_info|
|
73
|
+
puts "Master #{Process.pid} detected dead worker #{worker_info.name} " \
|
74
|
+
"with process id #{worker_info.process_id}"
|
64
75
|
end
|
65
76
|
```
|
66
77
|
|
@@ -69,9 +80,10 @@ Here's more information on each setting:
|
|
69
80
|
* `workers` - The number of Delayed Job worker processes to fork. The master process will relaunch workers that fail.
|
70
81
|
* Delayed Job worker settings (`queues`, `min_priority`, `max_priority`, `sleep_delay`, `read_ahead`) - These are passed through to the Delayed Job worker.
|
71
82
|
* `preload_app` - This forces the master process to load Rails before forking worker processes causing the memory consumed by the code to be shared between workers. **If you use this setting make sure you re-establish any necessary connections in the on_worker_boot callback.**
|
72
|
-
* `
|
83
|
+
* `after_preload_app` - A callback that runs in the master process after preloading the app but before forking any workers.
|
73
84
|
* `on_worker_boot` - A callback that runs in the worker process after it has been forked.
|
74
|
-
* `after_worker_boot` - A callback that runs in the master process after
|
85
|
+
* `after_worker_boot` - A callback that runs in the master process after a worker has been forked.
|
86
|
+
* `after_worker_shutdown` - A callback that runs in the master process after a worker has been shutdown.
|
75
87
|
|
76
88
|
All settings are optional and nil values are ignored.
|
77
89
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module DelayedJobWorkerPool
|
2
2
|
class DSL
|
3
3
|
SIMPLE_SETTINGS = [:workers, :queues, :min_priority, :max_priority, :sleep_delay, :read_ahead].freeze
|
4
|
-
CALLBACK_SETTINGS = [:
|
4
|
+
CALLBACK_SETTINGS = [:after_preload_app, :on_worker_boot, :after_worker_boot, :after_worker_shutdown].freeze
|
5
5
|
|
6
6
|
def self.load(path)
|
7
7
|
options = {}
|
@@ -11,17 +11,16 @@ module DelayedJobWorkerPool
|
|
11
11
|
|
12
12
|
install_signal_handlers
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
if preload_app?
|
15
|
+
load_app
|
16
|
+
invoke_callback(:after_preload_app)
|
17
|
+
end
|
17
18
|
|
18
19
|
log_uninheritable_threads
|
19
20
|
|
20
21
|
create_master_alive_pipe
|
21
22
|
num_workers.times { fork_worker }
|
22
23
|
|
23
|
-
invoke_callback(:after_worker_boot)
|
24
|
-
|
25
24
|
monitor_workers
|
26
25
|
|
27
26
|
exit
|
@@ -81,18 +80,20 @@ module DelayedJobWorkerPool
|
|
81
80
|
|
82
81
|
log("Worker #{worker_pid} exited with status #{status.to_i}")
|
83
82
|
worker_pids.delete(worker_pid)
|
83
|
+
invoke_callback(:after_worker_shutdown, worker_info(worker_pid))
|
84
84
|
fork_worker unless shutting_down
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
def invoke_callback(callback_name)
|
89
|
-
options[callback_name].call if options[callback_name]
|
88
|
+
def invoke_callback(callback_name, *args)
|
89
|
+
options[callback_name].call(*args) if options[callback_name]
|
90
90
|
end
|
91
91
|
|
92
92
|
def fork_worker
|
93
93
|
worker_pid = Kernel.fork { run_worker }
|
94
94
|
worker_pids << worker_pid
|
95
95
|
log("Started worker #{worker_pid}")
|
96
|
+
invoke_callback(:after_worker_boot, worker_info(worker_pid))
|
96
97
|
end
|
97
98
|
|
98
99
|
def run_worker
|
@@ -106,14 +107,22 @@ module DelayedJobWorkerPool
|
|
106
107
|
|
107
108
|
load_app unless preload_app?
|
108
109
|
|
109
|
-
invoke_callback(:on_worker_boot)
|
110
|
+
invoke_callback(:on_worker_boot, worker_info(Process.pid))
|
110
111
|
|
111
|
-
DelayedJobWorkerPool::Worker.run(worker_options)
|
112
|
+
DelayedJobWorkerPool::Worker.run(worker_options(Process.pid))
|
112
113
|
rescue => e
|
113
114
|
log("Worker failed with error: #{e.message}\n#{e.backtrace.join("\n")}")
|
114
115
|
exit(1)
|
115
116
|
end
|
116
117
|
|
118
|
+
def worker_info(worker_pid)
|
119
|
+
DelayedJobWorkerPool::WorkerInfo.new(name: worker_name(worker_pid), process_id: worker_pid)
|
120
|
+
end
|
121
|
+
|
122
|
+
def worker_name(worker_pid)
|
123
|
+
"host:#{Socket.gethostname} pid:#{worker_pid}"
|
124
|
+
end
|
125
|
+
|
117
126
|
def num_workers
|
118
127
|
options.fetch(:workers, 1)
|
119
128
|
end
|
@@ -122,8 +131,8 @@ module DelayedJobWorkerPool
|
|
122
131
|
options.fetch(:preload_app, false)
|
123
132
|
end
|
124
133
|
|
125
|
-
def worker_options
|
126
|
-
options.except(:workers, :preload_app, :
|
134
|
+
def worker_options(worker_pid)
|
135
|
+
options.except(:workers, :preload_app, *DelayedJobWorkerPool::DSL::CALLBACK_SETTINGS).merge(name: worker_name(worker_pid))
|
127
136
|
end
|
128
137
|
|
129
138
|
def log(message)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed_job_worker_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Turkel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: delayed_job
|
@@ -124,6 +124,7 @@ extra_rdoc_files: []
|
|
124
124
|
files:
|
125
125
|
- ".gitignore"
|
126
126
|
- ".travis.yml"
|
127
|
+
- CHANGELOG.md
|
127
128
|
- Gemfile
|
128
129
|
- LICENSE.txt
|
129
130
|
- README.md
|
@@ -135,6 +136,7 @@ files:
|
|
135
136
|
- lib/delayed_job_worker_pool/dsl.rb
|
136
137
|
- lib/delayed_job_worker_pool/version.rb
|
137
138
|
- lib/delayed_job_worker_pool/worker.rb
|
139
|
+
- lib/delayed_job_worker_pool/worker_info.rb
|
138
140
|
- lib/delayed_job_worker_pool/worker_pool.rb
|
139
141
|
homepage: https://github.com/salsify/delayed_job_worker_pool
|
140
142
|
licenses:
|