/
supervisor.rb
75 lines (61 loc) · 1.89 KB
/
supervisor.rb
1
2
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.
require 'samovar'
require 'async'
require 'json'
require 'async/io/stream'
require 'async/io/unix_endpoint'
module Falcon
module Command
# Implements the `falcon supervisor` command.
#
# Talks to an instance of the supervisor to issue commands and print results.
class Supervisor < Samovar::Command
self.description = "Control and query a specific supervisor."
# The command line options.
# @attribute [Samovar::Options]
options do
option "--path <path>", "The control IPC path.", default: "supervisor.ipc"
end
# Implements the `falcon supervisor restart` command.
class Restart < Samovar::Command
self.description = "Restart the process group."
# Send the restart message to the supervisor.
def call(stream)
stream.puts({please: 'restart'}.to_json, separator: "\0")
end
end
# Implements the `falcon supervisor metrics` command.
class Metrics < Samovar::Command
self.description = "Show metrics about the falcon processes."
# Send the metrics message to the supervisor and print the results.
def call(stream)
stream.puts({please: 'metrics'}.to_json, separator: "\0")
response = JSON.parse(stream.gets("\0"), symbolize_names: true)
pp response
end
end
# The nested command to execute.
# @name nested
# @attribute [Command]
nested :command, {
'restart' => Restart,
'metrics' => Metrics,
}, default: 'metrics'
# The endpoint the supervisor is bound to.
def endpoint
Async::IO::Endpoint.unix(@options[:path])
end
# Connect to the supervisor and execute the requested command.
def call
Async do
endpoint.connect do |socket|
stream = Async::IO::Stream.new(socket)
@command.call(stream)
end
end
end
end
end
end