Class: DaemonSpawn::Base
- Inherits:
-
Object
- Object
- DaemonSpawn::Base
- Defined in:
- lib/daemon_spawn.rb
Instance Attribute Summary collapse
-
#app_name ⇒ Object
Returns the value of attribute app_name.
-
#index ⇒ Object
Returns the value of attribute index.
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#pid_file ⇒ Object
Returns the value of attribute pid_file.
-
#signal ⇒ Object
Returns the value of attribute signal.
-
#singleton ⇒ Object
Returns the value of attribute singleton.
-
#sync_log ⇒ Object
Returns the value of attribute sync_log.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#working_dir ⇒ Object
Returns the value of attribute working_dir.
Class Method Summary collapse
- .build(options) ⇒ Object
- .find(options) ⇒ Object
- .restart(opts, args) ⇒ Object
-
.spawn!(opts = {}, args = ARGV) ⇒ Object
Invoke this method to process command-line args and dispatch appropriately.
-
.start(opts, args) ⇒ Object
Start daemons(s) unless at least one is running.
- .status(opts, args) ⇒ Object
- .stop(opts, args) ⇒ Object
Instance Method Summary collapse
-
#alive? ⇒ Boolean
:nodoc:.
-
#classname ⇒ Object
:nodoc:.
-
#initialize(opts = {}) ⇒ Base
constructor
A new instance of Base.
-
#pid ⇒ Object
:nodoc:.
-
#start(args) ⇒ Object
Provide your implementation.
-
#stop ⇒ Object
Provide your implementation.
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/daemon_spawn.rb', line 90 def initialize(opts = {}) raise 'You must specify a :working_dir' unless opts[:working_dir] self.signal = opts[:signal] || "TERM" self.timeout = opts[:timeout] || 15 self.working_dir = opts[:working_dir] self.app_name = opts[:application] || classname self.pid_file = opts[:pid_file] || File.join(working_dir, 'tmp', 'pids', app_name + extension) self.log_file = opts[:log_file] || File.join(working_dir, 'logs', app_name + '.log') self.index = opts[:index] || 0 if self.index > 0 self.pid_file += ".#{self.index}" self.log_file += ".#{self.index}" end self.sync_log = opts[:sync_log] self.singleton = opts[:singleton] || false end |
Instance Attribute Details
#app_name ⇒ Object
Returns the value of attribute app_name.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def app_name @app_name end |
#index ⇒ Object
Returns the value of attribute index.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def index @index end |
#log_file ⇒ Object
Returns the value of attribute log_file.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def log_file @log_file end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def pid_file @pid_file end |
#signal ⇒ Object
Returns the value of attribute signal.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def signal @signal end |
#singleton ⇒ Object
Returns the value of attribute singleton.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def singleton @singleton end |
#sync_log ⇒ Object
Returns the value of attribute sync_log.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def sync_log @sync_log end |
#timeout ⇒ Object
Returns the value of attribute timeout.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def timeout @timeout end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
88 89 90 |
# File 'lib/daemon_spawn.rb', line 88 def working_dir @working_dir end |
Class Method Details
.build(options) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/daemon_spawn.rb', line 136 def self.build() count = .delete(:processes) || 1 daemons = [] count.times do |index| daemons << new(.merge(:index => index)) end daemons end |
.find(options) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/daemon_spawn.rb', line 145 def self.find() pid_file = new().pid_file basename = File.basename(pid_file).split('.').first pid_files = Dir.glob(File.join(File.dirname(pid_file), "#{basename}.*pid*")) pid_files.map { |f| new(.merge(:pid_file => f)) } end |
.restart(opts, args) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/daemon_spawn.rb', line 206 def self.restart(opts, args) daemons = find(opts) daemons.map do |daemon| DaemonSpawn.stop(daemon) DaemonSpawn.start(daemon, args) end end |
.spawn!(opts = {}, args = ARGV) ⇒ Object
Invoke this method to process command-line args and dispatch appropriately. Valid options include the following symbols:
-
:working_dir
– the working directory (required) -
:log_file
– path to the log file -
:pid_file
– path to the pid file -
:sync_log
– indicate whether or not to sync log IO -
:singleton
– If set to true, only one instance is
allowed to start args must begin with ‘start’, ‘stop’, ‘status’, or ‘restart’. The first token will be removed and any remaining arguments passed to the daemon’s start method.
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/daemon_spawn.rb', line 163 def self.spawn!(opts = {}, args = ARGV) case args.any? and command = args.shift when 'start', 'stop', 'status', 'restart' send(command, opts, args) when '-h', '--help', 'help' DaemonSpawn.usage exit else DaemonSpawn.usage "Invalid command" exit 1 end end |
.start(opts, args) ⇒ Object
Start daemons(s) unless at least one is running
177 178 179 180 181 182 183 184 185 |
# File 'lib/daemon_spawn.rb', line 177 def self.start(opts, args) living_daemons = find(opts).select { |d| d.alive? } if living_daemons.any? puts "Daemons are still alive! PIDS: #{living_daemons.map {|d| d.pid}.join(', ')}" exit 1 else build(opts).map { |d| DaemonSpawn.start(d, args) } end end |
.status(opts, args) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/daemon_spawn.rb', line 197 def self.status(opts, args) daemons = find(opts) if daemons.empty? puts 'No PIDs found' else daemons.each { |d| DaemonSpawn.status(d) } end end |
.stop(opts, args) ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/daemon_spawn.rb', line 187 def self.stop(opts, args) daemons = find(opts) if daemons.empty? puts "No PID files found. Is the daemon started?" exit 1 else daemons.each { |d| DaemonSpawn.stop(d) } end end |
Instance Method Details
#alive? ⇒ Boolean
:nodoc:
124 125 126 127 128 129 130 |
# File 'lib/daemon_spawn.rb', line 124 def alive? #:nodoc: if File.file?(pid_file) self.class.alive? pid else false end end |
#classname ⇒ Object
:nodoc:
107 108 109 |
# File 'lib/daemon_spawn.rb', line 107 def classname #:nodoc: self.class.to_s.split('::').last end |
#pid ⇒ Object
:nodoc:
132 133 134 |
# File 'lib/daemon_spawn.rb', line 132 def pid #:nodoc: IO.read(self.pid_file).to_i rescue nil end |
#start(args) ⇒ Object
Provide your implementation. These are provided as a reminder only and will raise an error if invoked. When started, this method will be invoked with the remaining command-line arguments.
114 115 116 |
# File 'lib/daemon_spawn.rb', line 114 def start(args) raise "You must implement a 'start' method in your class!" end |
#stop ⇒ Object
Provide your implementation. These are provided as a reminder only and will raise an error if invoked.
120 121 122 |
# File 'lib/daemon_spawn.rb', line 120 def stop raise "You must implement a 'stop' method in your class!" end |