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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79d4a1eeecbbf23ff19a1811faf6a8d35d9616c5
4
- data.tar.gz: 99b6e576ac2ef89486b12614f171dd2db7d4588c
3
+ metadata.gz: f7e84e64a69a710244b682df783ea1f7f96ce72a
4
+ data.tar.gz: 75e5bbb9ffe986ded38517b4bb0fb72e7e5049c0
5
5
  SHA512:
6
- metadata.gz: 303a786f4bbe3c5e96a23e6cd63582f321289b4b8e63489071b9502059d503cf12f2667f8891c11a2955947bf2a426831840bdd28377a7e57dec5afac4179205
7
- data.tar.gz: 824d5d7e04193c7a4ed6fbddec598f378dc6f5f2ad57e39bc32700f3eb0a000e1881778aaf0fedf371607012812b10859adafd7b0606723154066ce1fc73a7c0
6
+ metadata.gz: 071b9234bfd7e3904bc44eb0957508a31bdd9975d644d7de77d06e3f9b0e8af5ccb0ca185c4bd9e754fde55c52698e4add16ebf873964d31d1af3101cb52d163
7
+ data.tar.gz: d98c55b40a03b8156d96ebfb870a56e75472101f61806dbab4b52300a6082808e225a4d4a738a689e53d0175d73688a7905e46974c9846080d05358176ca374a
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ### 0.2.0 (unreleased)
4
+ *
data/README.md CHANGED
@@ -45,22 +45,33 @@ sleep_delay ENV['WORKER_SLEEP_DELAY']
45
45
 
46
46
  preload_app
47
47
 
48
- before_worker_boot do
49
- puts 'Master about to start forking children'
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
- before on_worker_boot do
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
- after_worker_boot do
60
- puts 'Master booted children'
61
-
62
- # Don't hang on to database connections from the master after we've completed initialization
63
- ActiveRecord::Base.connection_pool.disconnect!
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
- * `before_worker_boot` - A callback that runs in the master process before forking any workers.
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 the initial set of workers have been forked.
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,5 +1,6 @@
1
1
  require 'delayed_job_worker_pool/application'
2
2
  require 'delayed_job_worker_pool/dsl'
3
3
  require 'delayed_job_worker_pool/worker'
4
+ require 'delayed_job_worker_pool/worker_info'
4
5
  require 'delayed_job_worker_pool/worker_pool'
5
6
  require 'delayed_job_worker_pool/version'
@@ -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 = [:before_worker_boot, :on_worker_boot, :after_worker_boot].freeze
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 = {}
@@ -1,3 +1,3 @@
1
1
  module DelayedJobWorkerPool
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -1,9 +1,10 @@
1
1
  module DelayedJobWorkerPool
2
2
  module Worker
3
3
  extend self
4
-
4
+
5
5
  def run(options = {})
6
6
  dj_worker = Delayed::Worker.new(options)
7
+ dj_worker.name = options[:name] if options.include?(:name)
7
8
  dj_worker.start
8
9
  end
9
10
  end
@@ -0,0 +1,11 @@
1
+ module DelayedJobWorkerPool
2
+ class WorkerInfo
3
+ attr_reader :process_id, :name
4
+
5
+ def initialize(attributes)
6
+ @process_id = attributes.fetch(:process_id)
7
+ @name = attributes.fetch(:name)
8
+ end
9
+
10
+ end
11
+ end
@@ -11,17 +11,16 @@ module DelayedJobWorkerPool
11
11
 
12
12
  install_signal_handlers
13
13
 
14
- load_app if preload_app?
15
-
16
- invoke_callback(:before_worker_boot)
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, :before_worker_boot, :on_worker_boot, :after_worker_boot)
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.1.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-17 00:00:00.000000000 Z
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: