Class: Workhorse::Daemon::ShellHandler

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

Defined Under Namespace

Classes: LockNotAvailableError

Class Method Summary collapse

Class Method Details

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



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
50
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
91
92
93
94
95
# File 'lib/workhorse/daemon/shell_handler.rb', line 5

def self.run(**options, &block)
  unless ARGV.one?
    usage
    exit 99
  end

  lockfile_path = options.delete(:lockfile) || 'workhorse.lock'
  daemon = Workhorse::Daemon.new(**options, &block)

  lockfile = nil

  begin
    case ARGV.first
    when 'start'
      Workhorse.debug_log('ShellHandler: start command invoked')
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      Workhorse.debug_log('ShellHandler: lock acquired for start')
      daemon.lockfile = lockfile
      status = daemon.start
    when 'stop'
      Workhorse.debug_log('ShellHandler: stop command invoked')
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      Workhorse.debug_log('ShellHandler: lock acquired for stop')
      daemon.lockfile = lockfile
      status = daemon.stop
    when 'kill'
      Workhorse.debug_log('ShellHandler: kill command invoked')
      begin
        lockfile = acquire_lock(lockfile_path, File::LOCK_EX | File::LOCK_NB)
        Workhorse.debug_log('ShellHandler: lock acquired for kill')
        daemon.lockfile = lockfile
        status = daemon.stop(true)
      rescue LockNotAvailableError
        Workhorse.debug_log('ShellHandler: lock not available for kill')
        status = 1
      end
    when 'status'
      Workhorse.debug_log('ShellHandler: status command invoked')
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      Workhorse.debug_log('ShellHandler: lock acquired for status')
      daemon.lockfile = lockfile
      status = daemon.status
    when 'watch'
      Workhorse.debug_log('ShellHandler: watch command invoked')
      begin
        lockfile = acquire_lock(lockfile_path, File::LOCK_EX | File::LOCK_NB)
        Workhorse.debug_log('ShellHandler: lock acquired for watch')
        daemon.lockfile = lockfile
        status = daemon.watch
      rescue LockNotAvailableError
        Workhorse.debug_log('ShellHandler: lock not available for watch')
        status = 1
      end
    when 'restart'
      Workhorse.debug_log('ShellHandler: restart command invoked')
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      Workhorse.debug_log('ShellHandler: lock acquired for restart')
      daemon.lockfile = lockfile
      status = daemon.restart
    when 'restart-logging'
      Workhorse.debug_log('ShellHandler: restart-logging command invoked')
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      Workhorse.debug_log('ShellHandler: lock acquired for restart-logging')
      daemon.lockfile = lockfile
      status = daemon.restart_logging
    when 'soft-restart'
      Workhorse.debug_log('ShellHandler: soft-restart command invoked')
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      Workhorse.debug_log('ShellHandler: lock acquired for soft-restart')
      daemon.lockfile = lockfile
      status = daemon.soft_restart
    when 'usage'
      usage
      status = 0
    else
      usage
      status = 99
    end
  rescue StandardError => e
    Workhorse.debug_log("ShellHandler: #{ARGV.first} failed with #{e.class}: #{e.message}")
    warn "#{e.message}\n#{e.backtrace.join("\n")}"
    status = 99
  ensure
    if lockfile
      Workhorse.debug_log("ShellHandler: releasing lock for #{ARGV.first}")
      lockfile.flock(File::LOCK_UN)
    end
    Workhorse.debug_log("ShellHandler: exiting with status #{status}")
    exit! status
  end
end

.usageObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/workhorse/daemon/shell_handler.rb', line 97

def self.usage
  warn <<~USAGE
    Usage: #{$PROGRAM_NAME} start|stop|status|watch|restart|soft-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.

      soft-restart
        Signals workers to restart gracefully. Idle workers restart
        immediately; busy workers finish their current job first. Returns
        immediately (fire-and-forget).
        NOTE: Requires 'watch' (typically via cron) to start fresh workers.
        Without 'watch', this behaves like a graceful stop with no automatic
        recovery.

      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