Class: DaemonSpawn::Base
- Inherits:
-
Object
- Object
- DaemonSpawn::Base
- Defined in:
- lib/daemon-spawn.rb
Instance Attribute Summary collapse
-
#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
-
.spawn!(opts = { }, args = ARGV) ⇒ Object
Invoke this method to process command-line args and dispatch appropriately.
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.
75 76 77 78 79 80 81 82 83 |
# File 'lib/daemon-spawn.rb', line 75 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_file ⇒ Object
Returns the value of attribute log_file.
73 74 75 |
# File 'lib/daemon-spawn.rb', line 73 def log_file @log_file end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
73 74 75 |
# File 'lib/daemon-spawn.rb', line 73 def pid_file @pid_file end |
#singleton ⇒ Object
Returns the value of attribute singleton.
73 74 75 |
# File 'lib/daemon-spawn.rb', line 73 def singleton @singleton end |
#sync_log ⇒ Object
Returns the value of attribute sync_log.
73 74 75 |
# File 'lib/daemon-spawn.rb', line 73 def sync_log @sync_log end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
73 74 75 |
# File 'lib/daemon-spawn.rb', line 73 def working_dir @working_dir end |
Class Method Details
.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.
129 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 |
# File 'lib/daemon-spawn.rb', line 129 def self.spawn!(opts={ }, args=ARGV) 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:
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 |