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.



76
77
78
79
80
81
82
83
84
# File 'lib/daemon-spawn.rb', line 76

def initialize(opts={ })
  raise "You must specify a :working_dir" unless opts[:working_dir]
  self.working_dir = opts[:working_dir]

  self.log_file = opts[:log_file] || File.join(self.working_dir, 'logs', self.classname + '.log')
  self.pid_file = opts[:pid_file] || File.join(self.working_dir, 'run', self.classname + '.pid')
  self.sync_log = opts[:sync_log]
  self.singleton = opts[:singleton] || false
end

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



74
75
76
# File 'lib/daemon-spawn.rb', line 74

def log_file
  @log_file
end

#pid_fileObject

Returns the value of attribute pid_file.



74
75
76
# File 'lib/daemon-spawn.rb', line 74

def pid_file
  @pid_file
end

#singletonObject

Returns the value of attribute singleton.



74
75
76
# File 'lib/daemon-spawn.rb', line 74

def singleton
  @singleton
end

#sync_logObject

Returns the value of attribute sync_log.



74
75
76
# File 'lib/daemon-spawn.rb', line 74

def sync_log
  @sync_log
end

#working_dirObject

Returns the value of attribute working_dir.



74
75
76
# File 'lib/daemon-spawn.rb', line 74

def working_dir
  @working_dir
end

Class Method Details

.spawn!(opts = { }, args = ARGV.dup) ⇒ 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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/daemon-spawn.rb', line 130

def self.spawn!(opts={ }, args=ARGV.dup)
  case args.size > 0 && args.shift
  when 'start'
    daemon = self.new(opts)
    DaemonSpawn.start(daemon, args)
  when 'stop'
    daemon = self.new(opts)
    DaemonSpawn.stop(daemon)
  when 'status'
    daemon = self.new(opts)
    if daemon.alive?
      puts "#{daemon.classname} is running (#{daemon.pid})"
    else
      puts "#{daemon.classname} is NOT running"
    end
  when 'restart'
    daemon = self.new(opts)
    DaemonSpawn.stop(daemon)
    DaemonSpawn.start(daemon, args)
  when '-h', '--help', 'help'
    DaemonSpawn.usage
    exit
  else
    DaemonSpawn.usage "Invalid command"
    exit 1
  end
end

Instance Method Details

#alive?Boolean

:nodoc:

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
# File 'lib/daemon-spawn.rb', line 103

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

#classnameObject

:nodoc:



86
87
88
# File 'lib/daemon-spawn.rb', line 86

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

#pidObject

:nodoc:



115
116
117
# File 'lib/daemon-spawn.rb', line 115

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.



93
94
95
# File 'lib/daemon-spawn.rb', line 93

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.



99
100
101
# File 'lib/daemon-spawn.rb', line 99

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