lib/activehook/server/manager.rb in activehook-server-0.1.3 vs lib/activehook/server/manager.rb in activehook-server-0.1.4
- old
+ new
@@ -8,10 +8,11 @@
attr_reader :forks
def initialize(options = {})
options.each { |key, value| send("#{key}=", value) }
@master = Process.pid
+ @forks = []
at_exit { shutdown }
end
# Instantiates new Worker objects, setting them with our options. We
# follow up by booting each of our Workers. Our Manager is then put to
@@ -25,20 +26,21 @@
end
# Shutsdown our Worker processes.
#
def shutdown
- @forks.each { |w| Process.kill('SIGINT', w[:pid].to_i) }
+ unless @forks.empty?
+ @forks.each { |w| Process.kill('SIGINT', w[:pid].to_i) }
+ end
Process.kill('SIGINT', @master)
end
private
# Create the specified number of workers and starts them
#
def create_workers
- @forks = []
@workers.times do |id|
pid = fork { Worker.new(@options.merge(id: id)).start }
@forks << { id: id, pid: pid }
end
end
@@ -52,12 +54,31 @@
# Validates our data before starting our Workers. Also instantiates our
# connection pool by pinging Redis.
#
def validate!
- raise Errors::Server, 'Cound not connect to Redis.' unless ActiveHook::Server.redis.with { |c| c.ping && c.quit }
- raise Errors::Server, 'Workers must be an Integer.' unless @workers.is_a?(Integer)
- raise Errors::Server, 'Options must be a Hash.' unless @options.is_a?(Hash)
+ validate_redis
+ validate_workers
+ validate_options
+ end
+
+ def validate_redis
+ ActiveHook::Server.redis.with { |c| c.ping && c.quit }
+ rescue
+ msg = 'Cound not connect to Redis.'
+ ActiveHook::Server.log.err(msg)
+ raise Errors::Manager, msg
+ end
+
+ def validate_workers
+ return if @workers.is_a?(Integer)
+ msg = 'Workers must be an Integer.'
+ raise Errors::Manager, msg
+ end
+
+ def validate_options
+ return if @options.is_a?(Hash)
+ raise Errors::Manager, 'Options must be a Hash.'
end
end
end
end