Class: Workhorse::Daemon::ShellHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/workhorse/daemon/shell_handler.rb

Class Method Summary collapse

Class Method Details

.run(**options, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/workhorse/daemon/shell_handler.rb', line 3

def self.run(**options, &block)
  unless ARGV.count == 1
    usage
    exit 99
  end

  if Workhorse.lock_shell_commands
    lockfile_path = options.delete(:lockfile) || 'workhorse.lock'
    lockfile = File.open(lockfile_path, 'a')
    lockfile.flock(File::LOCK_EX || File::LOCK_NB)
  else
    lockfile = nil
  end

  daemon = Workhorse::Daemon.new(**options, &block)

  begin
    case ARGV.first
    when 'start'
      exit daemon.start
    when 'stop'
      exit daemon.stop
    when 'kill'
      exit daemon.stop(true)
    when 'status'
      exit daemon.status
    when 'watch'
      exit daemon.watch
    when 'restart'
      exit daemon.restart
    when 'restart-logging'
      exit daemon.restart_logging
    when 'usage'
      usage
      exit 99
    else
      usage
    end

    exit 0
  rescue => e
    warn "#{e.message}\n#{e.backtrace.join("\n")}"
    exit 99
  ensure
    lockfile&.flock(File::LOCK_UN)
  end
end

.usageObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/workhorse/daemon/shell_handler.rb', line 51

def self.usage
  warn <<USAGE
Usage: #{$PROGRAM_NAME} start|stop|status|watch|restart|usage

Options:

  start
Start the daemon

  stop
Stop the daemon

  kill
Kill the daemon

  status
Query the status of the daemon. Exit with status 1 if any worker is
not running.

  watch
Checks the status (running or stopped) and whether it is as
expected. Starts the daemon if it is expected to run but is not.

  restart
Shortcut for consecutive 'stop' and 'start'.

  restart-logging
Re-opens log files, useful e.g. after the log files have been moved or
removed by log rotation.

  usage
Show this message

Exit status:
 0 if OK,
 1 on fatal errors outside of workhorse,
 2 if at least one worker has an unexpected status,
 99 on all other errors.
USAGE
end