Class: DaemonSpawn::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/daemon_spawn.rb

Direct Known Subclasses

Scout::StreamerDaemon

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/scout/daemon_spawn.rb', line 91

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_nameObject

Returns the value of attribute app_name.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def app_name
  @app_name
end

#indexObject

Returns the value of attribute index.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def index
  @index
end

#log_fileObject

Returns the value of attribute log_file.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def log_file
  @log_file
end

#pid_fileObject

Returns the value of attribute pid_file.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def pid_file
  @pid_file
end

#signalObject

Returns the value of attribute signal.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def signal
  @signal
end

#singletonObject

Returns the value of attribute singleton.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def singleton
  @singleton
end

#sync_logObject

Returns the value of attribute sync_log.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def sync_log
  @sync_log
end

#timeoutObject

Returns the value of attribute timeout.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def timeout
  @timeout
end

#working_dirObject

Returns the value of attribute working_dir.



89
90
91
# File 'lib/scout/daemon_spawn.rb', line 89

def working_dir
  @working_dir
end

Class Method Details

.build(options) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/scout/daemon_spawn.rb', line 137

def self.build(options)
  count = options.delete(:processes) || 1
  daemons = []
  count.times do |index|
    daemons << new(options.merge(:index => index))
  end
  daemons
end

.find(options) ⇒ Object



146
147
148
149
150
151
# File 'lib/scout/daemon_spawn.rb', line 146

def self.find(options)
  pid_file = new(options).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(options.merge(:pid_file => f)) }
end

.restart(opts, args) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/scout/daemon_spawn.rb', line 206

def self.restart(opts, args)
  daemons = build(opts)
  daemons.map do |daemon|
    DaemonSpawn.stop(daemon)
    sleep 0.1
    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.



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/scout/daemon_spawn.rb', line 164

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



177
178
179
180
181
182
183
184
185
# File 'lib/scout/daemon_spawn.rb', line 177

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



197
198
199
200
201
202
203
204
# File 'lib/scout/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/scout/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:

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/scout/daemon_spawn.rb', line 125

def alive? #:nodoc:
  if File.file?(pid_file)
    DaemonSpawn.alive? pid
  else
    false
  end
end

#classnameObject

:nodoc:



108
109
110
# File 'lib/scout/daemon_spawn.rb', line 108

def classname #:nodoc:
  self.class.to_s.split('::').last
end

#pidObject

:nodoc:



133
134
135
# File 'lib/scout/daemon_spawn.rb', line 133

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.



115
116
117
# File 'lib/scout/daemon_spawn.rb', line 115

def start(args)
  raise "You must implement a 'start' method in your class!"
end

#stopObject

Provide your implementation. These are provided as a reminder only and will raise an error if invoked.



121
122
123
# File 'lib/scout/daemon_spawn.rb', line 121

def stop
  raise "You must implement a 'stop' method in your class!"
end