Class: DaemonSpawn::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



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

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.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def app_name
  @app_name
end

#indexObject

Returns the value of attribute index.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def index
  @index
end

#log_fileObject

Returns the value of attribute log_file.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def log_file
  @log_file
end

#pid_fileObject

Returns the value of attribute pid_file.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def pid_file
  @pid_file
end

#signalObject

Returns the value of attribute signal.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def signal
  @signal
end

#singletonObject

Returns the value of attribute singleton.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def singleton
  @singleton
end

#sync_logObject

Returns the value of attribute sync_log.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def sync_log
  @sync_log
end

#timeoutObject

Returns the value of attribute timeout.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def timeout
  @timeout
end

#working_dirObject

Returns the value of attribute working_dir.



87
88
89
# File 'lib/daemon_spawn.rb', line 87

def working_dir
  @working_dir
end

Class Method Details

.build(options) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/daemon_spawn.rb', line 135

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



144
145
146
147
148
149
# File 'lib/daemon_spawn.rb', line 144

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



204
205
206
207
208
209
210
# File 'lib/daemon_spawn.rb', line 204

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.



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

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



175
176
177
178
179
180
181
182
183
# File 'lib/daemon_spawn.rb', line 175

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



195
196
197
198
199
200
201
202
# File 'lib/daemon_spawn.rb', line 195

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



185
186
187
188
189
190
191
192
193
# File 'lib/daemon_spawn.rb', line 185

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)


123
124
125
126
127
128
129
# File 'lib/daemon_spawn.rb', line 123

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

#classnameObject

:nodoc:



106
107
108
# File 'lib/daemon_spawn.rb', line 106

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

#pidObject

:nodoc:



131
132
133
# File 'lib/daemon_spawn.rb', line 131

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.



113
114
115
# File 'lib/daemon_spawn.rb', line 113

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.



119
120
121
# File 'lib/daemon_spawn.rb', line 119

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