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
- .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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/daemon_spawn.rb', line 88 def initialize(opts = {}) raise 'You must specify a :working_dir' unless opts[:working_dir] 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 + '.pid') self.log_file = opts[:log_file] || File.join(working_dir, 'logs', app_name + '.log') self.signal = opts[:signal] || 'TERM' self.timeout = opts[:timeout] 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.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def app_name @app_name end |
#index ⇒ Object
Returns the value of attribute index.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def index @index end |
#log_file ⇒ Object
Returns the value of attribute log_file.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def log_file @log_file end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def pid_file @pid_file end |
#signal ⇒ Object
Returns the value of attribute signal.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def signal @signal end |
#singleton ⇒ Object
Returns the value of attribute singleton.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def singleton @singleton end |
#sync_log ⇒ Object
Returns the value of attribute sync_log.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def sync_log @sync_log end |
#timeout ⇒ Object
Returns the value of attribute timeout.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def timeout @timeout end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
86 87 88 |
# File 'lib/daemon_spawn.rb', line 86 def working_dir @working_dir end |
Class Method Details
.build(options) ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/daemon_spawn.rb', line 134 def self.build() count = .delete(:processes) || 1 daemons = [] count.times do |index| daemons << new(.merge(:index => index)) end daemons end |
.find(options) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/daemon_spawn.rb', line 143 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
203 204 205 206 207 208 209 |
# File 'lib/daemon_spawn.rb', line 203 def self.restart(opts, args) daemons = build(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.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/daemon_spawn.rb', line 161 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
174 175 176 177 178 179 180 181 182 |
# File 'lib/daemon_spawn.rb', line 174 def self.start(opts, args) living_daemons = find(opts).select { |d| d.alive? } if living_daemons.any? puts "Daemons already started! 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
194 195 196 197 198 199 200 201 |
# File 'lib/daemon_spawn.rb', line 194 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
184 185 186 187 188 189 190 191 192 |
# File 'lib/daemon_spawn.rb', line 184 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:
122 123 124 125 126 127 128 |
# File 'lib/daemon_spawn.rb', line 122 def alive? #:nodoc: if File.file?(pid_file) DaemonSpawn.alive? pid else false end end |
#classname ⇒ Object
:nodoc:
105 106 107 |
# File 'lib/daemon_spawn.rb', line 105 def classname #:nodoc: self.class.to_s.split('::').last end |
#pid ⇒ Object
:nodoc:
130 131 132 |
# File 'lib/daemon_spawn.rb', line 130 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.
112 113 114 |
# File 'lib/daemon_spawn.rb', line 112 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.
118 119 120 |
# File 'lib/daemon_spawn.rb', line 118 def stop raise "You must implement a 'stop' method in your class!" end |