Class: Insidious
- Inherits:
-
Object
- Object
- Insidious
- Defined in:
- lib/insidious.rb
Instance Attribute Summary collapse
-
#pid_file ⇒ Object
readonly
Returns the value of attribute pid_file.
Instance Method Summary collapse
-
#daemon? ⇒ Boolean
Returns true if insidious is running as a daemon which is the default.
-
#initialize(options = {}) ⇒ Insidious
constructor
Intiailise Insidious, note the correct spelling of initialise.
-
#pid ⇒ Object
Get the pid from the pid_file TODO: should this be ‘cached’?.
-
#pid=(pid) ⇒ Object
Save the PID to the PID file specified in @pid_file.
-
#restart!(&block) ⇒ Object
Restarts the daemon, just a convenience method really.
-
#running? ⇒ Boolean
Returns true if the daemon is running.
-
#start!(&block) ⇒ Object
Starts the daemon.
-
#stop! ⇒ Object
Stops the daemon execution.
Constructor Details
#initialize(options = {}) ⇒ Insidious
Intiailise Insidious, note the correct spelling of initialise.
7 8 9 10 |
# File 'lib/insidious.rb', line 7 def initialize( = {}) @daemonize = [:daemonize].nil? ? true : [:daemonize] @pid_file = [:pid_file].nil? ? nil : File.absolute_path([:pid_file]) end |
Instance Attribute Details
#pid_file ⇒ Object (readonly)
Returns the value of attribute pid_file.
4 5 6 |
# File 'lib/insidious.rb', line 4 def pid_file @pid_file end |
Instance Method Details
#daemon? ⇒ Boolean
Returns true if insidious is running as a daemon which is the default
75 76 77 |
# File 'lib/insidious.rb', line 75 def daemon? @daemonize end |
#pid ⇒ Object
Get the pid from the pid_file TODO: should this be ‘cached’?
81 82 83 |
# File 'lib/insidious.rb', line 81 def pid File.read(@pid_file).strip.to_i end |
#pid=(pid) ⇒ Object
Save the PID to the PID file specified in @pid_file
86 87 88 89 90 |
# File 'lib/insidious.rb', line 86 def pid=(pid) File.open(@pid_file, 'w') do |file| file.write(pid) end if @pid_file end |
#restart!(&block) ⇒ Object
Restarts the daemon, just a convenience method really
52 53 54 55 |
# File 'lib/insidious.rb', line 52 def restart!(&block) stop! if running? start!(&block) end |
#running? ⇒ Boolean
Returns true if the daemon is running
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/insidious.rb', line 58 def running? # First check if we have a pid file and if it exists return false if @pid_file.nil? || !File.exists?(@pid_file) # Then make sure we have a pid return false if pid.nil? # If we can get the process id then we assume it is running begin Process.getpgid(pid) true rescue Errno::ESRCH false end end |
#start!(&block) ⇒ Object
Starts the daemon
If a PID file was provided it will try to store the current PID. If this files exists it will try to check if the stored PID is already running, in which case insidious will exit with an error code.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/insidious.rb', line 18 def start!(&block) if running? fail InsidiousError.new("Process is already running with PID #{pid}") exit 2 else if @pid_file.nil? && daemon? fail InsidiousError.new('No PID file is set but daemonize is set to true') exit 1 end run_daemon!(&block) end end |
#stop! ⇒ Object
Stops the daemon execution
This method only works when a PID file is given, otherwise it will exit with an error.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/insidious.rb', line 36 def stop! if @pid_file && File.exists?(@pid_file) begin Process.kill(:INT, pid) File.delete(@pid_file) rescue Errno::ESRCH fail InsidiousError.new("No process is running with PID #{pid}") exit 3 end else fail InsidiousError.new("Couldn't find the PID file: '#{@pid_file}'") exit 1 end end |