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
50
51
|
# File 'lib/workhorse/daemon/shell_handler.rb', line 3
def self.run(**options, &block)
unless ARGV.count == 1
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)
status = daemon.start
when 'stop'
lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
status = daemon.stop
when 'kill'
lockfile = acquire_lock(lockfile_path, File::LOCK_EX | File::LOCK_NB)
status = daemon.stop(true)
when 'status'
lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
status = daemon.status
when 'watch'
lockfile = acquire_lock(lockfile_path, File::LOCK_EX | File::LOCK_NB)
status = daemon.watch
when 'restart'
lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
status = daemon.restart
when 'restart-logging'
lockfile = acquire_lock(lockfile_path, File::LOCK_EX)
status = daemon.restart_logging
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
|