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.
-
#singleton ⇒ Object
Returns the value of attribute singleton.
-
#sync_log ⇒ Object
Returns the value of attribute sync_log.
-
#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.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/daemon_spawn.rb', line 70 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 + 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.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def app_name @app_name end |
#index ⇒ Object
Returns the value of attribute index.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def index @index end |
#log_file ⇒ Object
Returns the value of attribute log_file.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def log_file @log_file end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def pid_file @pid_file end |
#singleton ⇒ Object
Returns the value of attribute singleton.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def singleton @singleton end |
#sync_log ⇒ Object
Returns the value of attribute sync_log.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def sync_log @sync_log end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
68 69 70 |
# File 'lib/daemon_spawn.rb', line 68 def working_dir @working_dir end |
Class Method Details
.build(options) ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/daemon_spawn.rb', line 118 def self.build() count = .delete(:processes) || 1 daemons = [] count.times do |index| daemons << new(.merge(:index => index)) end daemons end |
.find(options) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/daemon_spawn.rb', line 127 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
188 189 190 191 192 193 194 |
# File 'lib/daemon_spawn.rb', line 188 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.
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/daemon_spawn.rb', line 145 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
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/daemon_spawn.rb', line 158 def self.start(opts, args) daemons = find(opts) if daemons.empty? daemons = build(opts) daemons.map { |d| DaemonSpawn.start(d, args) } else puts "Daemons already started! PIDS: #{daemons.map {|d| d.pid}.join(', ')}" exit 1 end end |
.status(opts, args) ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'lib/daemon_spawn.rb', line 179 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
169 170 171 172 173 174 175 176 177 |
# File 'lib/daemon_spawn.rb', line 169 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:
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/daemon_spawn.rb', line 102 def alive? #:nodoc: if File.file?(self.pid_file) begin Process.kill(0, self.pid) rescue Errno::ESRCH, ::Exception false end else false end end |
#classname ⇒ Object
:nodoc:
85 86 87 |
# File 'lib/daemon_spawn.rb', line 85 def classname #:nodoc: self.class.to_s.split('::').last end |
#pid ⇒ Object
:nodoc:
114 115 116 |
# File 'lib/daemon_spawn.rb', line 114 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.
92 93 94 |
# File 'lib/daemon_spawn.rb', line 92 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.
98 99 100 |
# File 'lib/daemon_spawn.rb', line 98 def stop raise "You must implement a 'stop' method in your class!" end |