Class: Opener::Daemons::Controller
- Inherits:
-
Object
- Object
- Opener::Daemons::Controller
- Defined in:
- lib/opener/daemons/controller.rb
Overview
CLI controller for a component.
Instance Attribute Summary collapse
-
#exec_path ⇒ String
readonly
The path to the script to daemonize.
-
#name ⇒ String
readonly
The name of the daemon.
- #parser ⇒ Opener::Daemons::OptionParser readonly
Instance Method Summary collapse
- #configure_slop ⇒ Slop
-
#initialize(options = {}) ⇒ Controller
constructor
A new instance of Controller.
-
#run(argv = ARGV) ⇒ Object
Runs the CLI.
-
#setup_env(options) ⇒ Hash
Returns a Hash containing the various environment variables to set for the daemon (on top of the current environment variables).
-
#start_background(options, argv = []) ⇒ Object
Starts the daemon in the background.
-
#start_foreground(options, argv = []) ⇒ Object
Runs the daemon in the foreground.
-
#stop(options) ⇒ Object
Stops the daemon.
Constructor Details
#initialize(options = {}) ⇒ Controller
Returns a new instance of Controller.
26 27 28 29 30 |
# File 'lib/opener/daemons/controller.rb', line 26 def initialize( = {}) @name = .fetch(:name) @exec_path = .fetch(:exec_path) @parser = configure_slop end |
Instance Attribute Details
#exec_path ⇒ String (readonly)
The path to the script to daemonize.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/opener/daemons/controller.rb', line 17 class Controller attr_reader :name, :exec_path, :parser ## # @param [Hash] options # # @option options [String] :name # @option options [String] :exec_path # def initialize( = {}) @name = .fetch(:name) @exec_path = .fetch(:exec_path) @parser = configure_slop end ## # Runs the CLI # # @param [Array] argv CLI arguments to parse. # def run(argv = ARGV) parser.parse(argv) end ## # @return [Slop] # def configure_slop parser = OptionParser.new(name) parser.parser.run do |opts, args| command = args.shift new_args = args.reject { |arg| arg == '--' } case command when 'start' start_background(opts, new_args) when 'stop' stop(opts) when 'restart' stop(opts) start_background(opts, new_args) else start_foreground(opts, new_args) end end return parser end ## # Runs the daemon in the foreground. # # @param [Slop] options # @param [Array] argv # def start_foreground(, argv = []) exec(setup_env(), exec_path, *argv) end ## # Starts the daemon in the background. # # @param [Slop] options # @param [Array] argv # def start_background(, argv = []) pidfile = Pidfile.new([:pidfile]) pid = Process.spawn( setup_env(), exec_path, *argv, out: '/dev/null', err: '/dev/null', in: '/dev/null' ) pidfile.write(pid) begin # Wait until the process has _actually_ started. Timeout.timeout([:wait]) { sleep(0.5) until pidfile.alive? } puts "Process with Pidfile #{pidfile.read} started" rescue Timeout::Error pidfile.unlink abort "Failed to start the process after #{[:wait]} seconds" end end ## # Stops the daemon. # # @param [Slop] options # def stop() pidfile = Pidfile.new([:pidfile]) if pidfile.alive? id = pidfile.read pidfile.terminate pidfile.unlink puts "Process with Pidfile #{id.inspect} terminated" else abort 'Process already terminated or you are not allowed to terminate it' end end ## # Returns a Hash containing the various environment variables to set for # the daemon (on top of the current environment variables). # # @param [Slop] options # @return [Hash] # def setup_env() newrelic_config = File.( '../../../../config/newrelic.yml', __FILE__ ) env = ENV.to_hash.merge( 'INPUT_QUEUE' => [:input].to_s, 'DAEMON_THREADS' => [:threads].to_s, 'OUTPUT_BUCKET' => [:bucket].to_s, 'NRCONFIG' => newrelic_config, 'APP_ROOT' => File.('../../../../', __FILE__), 'APP_NAME' => name ) if !env['RAILS_ENV'] and env['RACK_ENV'] env['RAILS_ENV'] = env['RACK_ENV'] end unless [:'disable-syslog'] env['ENABLE_SYSLOG'] = 'true' end return env end end |
#name ⇒ String (readonly)
The name of the daemon.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/opener/daemons/controller.rb', line 17 class Controller attr_reader :name, :exec_path, :parser ## # @param [Hash] options # # @option options [String] :name # @option options [String] :exec_path # def initialize( = {}) @name = .fetch(:name) @exec_path = .fetch(:exec_path) @parser = configure_slop end ## # Runs the CLI # # @param [Array] argv CLI arguments to parse. # def run(argv = ARGV) parser.parse(argv) end ## # @return [Slop] # def configure_slop parser = OptionParser.new(name) parser.parser.run do |opts, args| command = args.shift new_args = args.reject { |arg| arg == '--' } case command when 'start' start_background(opts, new_args) when 'stop' stop(opts) when 'restart' stop(opts) start_background(opts, new_args) else start_foreground(opts, new_args) end end return parser end ## # Runs the daemon in the foreground. # # @param [Slop] options # @param [Array] argv # def start_foreground(, argv = []) exec(setup_env(), exec_path, *argv) end ## # Starts the daemon in the background. # # @param [Slop] options # @param [Array] argv # def start_background(, argv = []) pidfile = Pidfile.new([:pidfile]) pid = Process.spawn( setup_env(), exec_path, *argv, out: '/dev/null', err: '/dev/null', in: '/dev/null' ) pidfile.write(pid) begin # Wait until the process has _actually_ started. Timeout.timeout([:wait]) { sleep(0.5) until pidfile.alive? } puts "Process with Pidfile #{pidfile.read} started" rescue Timeout::Error pidfile.unlink abort "Failed to start the process after #{[:wait]} seconds" end end ## # Stops the daemon. # # @param [Slop] options # def stop() pidfile = Pidfile.new([:pidfile]) if pidfile.alive? id = pidfile.read pidfile.terminate pidfile.unlink puts "Process with Pidfile #{id.inspect} terminated" else abort 'Process already terminated or you are not allowed to terminate it' end end ## # Returns a Hash containing the various environment variables to set for # the daemon (on top of the current environment variables). # # @param [Slop] options # @return [Hash] # def setup_env() newrelic_config = File.( '../../../../config/newrelic.yml', __FILE__ ) env = ENV.to_hash.merge( 'INPUT_QUEUE' => [:input].to_s, 'DAEMON_THREADS' => [:threads].to_s, 'OUTPUT_BUCKET' => [:bucket].to_s, 'NRCONFIG' => newrelic_config, 'APP_ROOT' => File.('../../../../', __FILE__), 'APP_NAME' => name ) if !env['RAILS_ENV'] and env['RACK_ENV'] env['RAILS_ENV'] = env['RACK_ENV'] end unless [:'disable-syslog'] env['ENABLE_SYSLOG'] = 'true' end return env end end |
#parser ⇒ Opener::Daemons::OptionParser (readonly)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/opener/daemons/controller.rb', line 17 class Controller attr_reader :name, :exec_path, :parser ## # @param [Hash] options # # @option options [String] :name # @option options [String] :exec_path # def initialize( = {}) @name = .fetch(:name) @exec_path = .fetch(:exec_path) @parser = configure_slop end ## # Runs the CLI # # @param [Array] argv CLI arguments to parse. # def run(argv = ARGV) parser.parse(argv) end ## # @return [Slop] # def configure_slop parser = OptionParser.new(name) parser.parser.run do |opts, args| command = args.shift new_args = args.reject { |arg| arg == '--' } case command when 'start' start_background(opts, new_args) when 'stop' stop(opts) when 'restart' stop(opts) start_background(opts, new_args) else start_foreground(opts, new_args) end end return parser end ## # Runs the daemon in the foreground. # # @param [Slop] options # @param [Array] argv # def start_foreground(, argv = []) exec(setup_env(), exec_path, *argv) end ## # Starts the daemon in the background. # # @param [Slop] options # @param [Array] argv # def start_background(, argv = []) pidfile = Pidfile.new([:pidfile]) pid = Process.spawn( setup_env(), exec_path, *argv, out: '/dev/null', err: '/dev/null', in: '/dev/null' ) pidfile.write(pid) begin # Wait until the process has _actually_ started. Timeout.timeout([:wait]) { sleep(0.5) until pidfile.alive? } puts "Process with Pidfile #{pidfile.read} started" rescue Timeout::Error pidfile.unlink abort "Failed to start the process after #{[:wait]} seconds" end end ## # Stops the daemon. # # @param [Slop] options # def stop() pidfile = Pidfile.new([:pidfile]) if pidfile.alive? id = pidfile.read pidfile.terminate pidfile.unlink puts "Process with Pidfile #{id.inspect} terminated" else abort 'Process already terminated or you are not allowed to terminate it' end end ## # Returns a Hash containing the various environment variables to set for # the daemon (on top of the current environment variables). # # @param [Slop] options # @return [Hash] # def setup_env() newrelic_config = File.( '../../../../config/newrelic.yml', __FILE__ ) env = ENV.to_hash.merge( 'INPUT_QUEUE' => [:input].to_s, 'DAEMON_THREADS' => [:threads].to_s, 'OUTPUT_BUCKET' => [:bucket].to_s, 'NRCONFIG' => newrelic_config, 'APP_ROOT' => File.('../../../../', __FILE__), 'APP_NAME' => name ) if !env['RAILS_ENV'] and env['RACK_ENV'] env['RAILS_ENV'] = env['RACK_ENV'] end unless [:'disable-syslog'] env['ENABLE_SYSLOG'] = 'true' end return env end end |
Instance Method Details
#configure_slop ⇒ Slop
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/opener/daemons/controller.rb', line 44 def configure_slop parser = OptionParser.new(name) parser.parser.run do |opts, args| command = args.shift new_args = args.reject { |arg| arg == '--' } case command when 'start' start_background(opts, new_args) when 'stop' stop(opts) when 'restart' stop(opts) start_background(opts, new_args) else start_foreground(opts, new_args) end end return parser end |
#run(argv = ARGV) ⇒ Object
Runs the CLI
37 38 39 |
# File 'lib/opener/daemons/controller.rb', line 37 def run(argv = ARGV) parser.parse(argv) end |
#setup_env(options) ⇒ Hash
Returns a Hash containing the various environment variables to set for the daemon (on top of the current environment variables).
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/opener/daemons/controller.rb', line 135 def setup_env() newrelic_config = File.( '../../../../config/newrelic.yml', __FILE__ ) env = ENV.to_hash.merge( 'INPUT_QUEUE' => [:input].to_s, 'DAEMON_THREADS' => [:threads].to_s, 'OUTPUT_BUCKET' => [:bucket].to_s, 'NRCONFIG' => newrelic_config, 'APP_ROOT' => File.('../../../../', __FILE__), 'APP_NAME' => name ) if !env['RAILS_ENV'] and env['RACK_ENV'] env['RAILS_ENV'] = env['RACK_ENV'] end unless [:'disable-syslog'] env['ENABLE_SYSLOG'] = 'true' end return env end |
#start_background(options, argv = []) ⇒ Object
Starts the daemon in the background.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/opener/daemons/controller.rb', line 83 def start_background(, argv = []) pidfile = Pidfile.new([:pidfile]) pid = Process.spawn( setup_env(), exec_path, *argv, out: '/dev/null', err: '/dev/null', in: '/dev/null' ) pidfile.write(pid) begin # Wait until the process has _actually_ started. Timeout.timeout([:wait]) { sleep(0.5) until pidfile.alive? } puts "Process with Pidfile #{pidfile.read} started" rescue Timeout::Error pidfile.unlink abort "Failed to start the process after #{[:wait]} seconds" end end |
#start_foreground(options, argv = []) ⇒ Object
Runs the daemon in the foreground.
73 74 75 |
# File 'lib/opener/daemons/controller.rb', line 73 def start_foreground(, argv = []) exec(setup_env(), exec_path, *argv) end |
#stop(options) ⇒ Object
Stops the daemon.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/opener/daemons/controller.rb', line 113 def stop() pidfile = Pidfile.new([:pidfile]) if pidfile.alive? id = pidfile.read pidfile.terminate pidfile.unlink puts "Process with Pidfile #{id.inspect} terminated" else abort 'Process already terminated or you are not allowed to terminate it' end end |