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
# 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'
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      daemon.lockfile = lockfile
      status = daemon.start
    when 'stop'
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      daemon.lockfile = lockfile
      status = daemon.stop
    when 'kill'
      begin
        lockfile = acquire_lock(lockfile_path, File::LOCK_EX | File::LOCK_NB)
        daemon.lockfile = lockfile
        status = daemon.stop(true)
      rescue LockNotAvailableError
        status = 1
      end
    when 'status'
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      daemon.lockfile = lockfile
      status = daemon.status
    when 'watch'
      begin
        lockfile = acquire_lock(lockfile_path, File::LOCK_EX | File::LOCK_NB)
        daemon.lockfile = lockfile
        status = daemon.watch
      rescue LockNotAvailableError
        status = 1
      end
    when 'restart'
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      daemon.lockfile = lockfile
      status = daemon.restart
    when 'restart-logging'
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      daemon.lockfile = lockfile
      status = daemon.restart_logging
    when 'soft-restart'
      lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
      daemon.lockfile = lockfile
      status = daemon.soft_restart
    when 'usage'
      usage
      status = 0
    else
      usage
      status = 99
    end
  rescue StandardError => e
    warn "#{e.message}\n#{e.backtrace.join("\n")}"
    status = 99
  ensure
    lockfile&.flock(File::LOCK_UN)
    exit! status
  end
end

.usageObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/workhorse/daemon/shell_handler.rb', line 74

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