Module: Fallen

Defined in:
lib/fallen.rb

Defined Under Namespace

Modules: CLI

Instance Method Summary collapse

Instance Method Details

#chdir!(path) ⇒ Object

Changes the working directory

All paths will be relative to the working directory unless they're specified as absolute paths.

Parameters:

  • path (String)

    of the new workng directory



23
24
25
# File 'lib/fallen.rb', line 23

def chdir! path
  Dir.chdir File.absolute_path(path)
end

#daemonize!Object

Note:

Unless STDIN, STDOUT or STDERR are redirected to a file, these will be redirected to /dev/null

Detachs this fallen angel from current process and runs it in background



13
14
15
# File 'lib/fallen.rb', line 13

def daemonize!
  Process.daemon true, (@stdin || @stdout || @stderr)
end

#pid_file(path) ⇒ Object

Path where the PID file will be created



28
29
30
# File 'lib/fallen.rb', line 28

def pid_file path
  @pid_file = File.absolute_path(path)
end

#run!Object

Runs the fallen angel

This will set up INT & TERM signal handlers to stop execution properly. When this signal handlers are called it will also call the stop callback method.



90
91
92
93
94
95
96
# File 'lib/fallen.rb', line 90

def run!
  save_pid_file
  @running = true
  trap(:INT) { @running = false; stop }
  trap(:TERM) { @running = false; stop }
  run
end

#running?Boolean

Returns true if the fallen angel is running

Returns:

  • (Boolean)


60
61
62
# File 'lib/fallen.rb', line 60

def running?
  @running
end

#start!Object

Brings the fallen angel to life

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 Fallen will exit with an error code.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fallen.rb', line 70

def start!
  if @pid_file && File.exists?(@pid_file)
    pid = File.read(@pid_file).strip
    begin
      Process.kill 0, pid.to_i
      STDERR.puts "Daemon is already running with PID #{pid}"
      exit 2
    rescue Errno::ESRCH
      run!
    end
  else
    run!
  end
end

#stderr(path) ⇒ Object

Reopens STDERR for writing to path

This path is relative to the working directory unless an absolute path is given.



54
55
56
57
# File 'lib/fallen.rb', line 54

def stderr path
  @stderr = File.absolute_path(path)
  STDERR.reopen @stderr, "a"
end

#stdin(path) ⇒ Object

Reopens STDIN for reading from path

This path is relative to the working directory unless an absolute path is given.



36
37
38
39
# File 'lib/fallen.rb', line 36

def stdin path
  @stdin = File.absolute_path(path)
  STDIN.reopen @stdin
end

#stdout(path) ⇒ Object

Reopens STDOUT for writing to path

This path is relative to the working directory unless an absolute path is given.



45
46
47
48
# File 'lib/fallen.rb', line 45

def stdout path
  @stdout = File.absolute_path(path)
  STDOUT.reopen @stdout, "a"
end

#stopObject

Callback method to be run when fallen angel stops



118
# File 'lib/fallen.rb', line 118

def stop; end

#stop!Object

Stops fallen angel execution

This method only works when a PID file is given, otherwise it will exit with an error.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fallen.rb', line 102

def stop!
  if @pid_file && File.exists?(@pid_file)
    pid = File.read(@pid_file).strip
    begin
      Process.kill :INT, pid.to_i
    rescue Errno::ESRCH
      STDERR.puts "No daemon is running with PID #{pid}"
      exit 3
    end
  else
    STDERR.puts "Couldn't find a PID file"
    exit 1
  end
end