Skip to content

Commit

Permalink
Use bake for supervisor tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jun 20, 2024
1 parent 558cfb1 commit 48e6484
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 81 deletions.
40 changes: 37 additions & 3 deletions bake/falcon/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,42 @@
# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.

def restart
require_relative '../../lib/falcon/command/supervisor'
def initialize(context)
super

require 'io/endpoint/unix_endpoint'
require 'io/stream'
require 'json'

Falcon::Command::Supervisor["restart"].call
@path = "supervisor.ipc"
end

# Restart the process group that the supervisor belongs to.
def restart
connect do |stream|
stream.puts({please: 'restart'}.to_json, separator: "\0")
return JSON.parse(stream.gets("\0"), symbolize_names: true)
end
end

# Ask the supervisor for metrics relating to the process group that the supervisor belongs to.
# @returns [Hash] The metrics, organised by process ID.
def metrics
connect do |stream|
stream.puts({please: 'metrics'}.to_json, separator: "\0")
return JSON.parse(stream.gets("\0"), symbolize_names: true)
end
end

private

# The endpoint the supervisor is bound to.
def endpoint
::IO::Endpoint.unix(@path)
end

def connect
endpoint.connect do |stream|
yield IO::Stream(stream)
end
end
73 changes: 0 additions & 73 deletions lib/falcon/command/supervisor.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/falcon/command/top.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
require_relative 'virtual'
require_relative 'proxy'
require_relative 'redirect'
require_relative 'supervisor'

require_relative '../version'

Expand Down Expand Up @@ -38,7 +37,6 @@ class Top < Samovar::Command
'virtual' => Virtual,
'proxy' => Proxy,
'redirect' => Redirect,
'supervisor' => Supervisor,
}, default: 'serve'

# Whether verbose logging is enabled.
Expand Down
11 changes: 8 additions & 3 deletions lib/falcon/service/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'process/metrics'
require 'json'
require 'io/stream'

require 'async/service/generic'

Expand Down Expand Up @@ -33,14 +34,18 @@ def do_restart(message)
# Stop accepting connections.
# Wait for existing connnections to drain.
# Terminate this process group.
signal = message[:signal] || :INT
signal = message[:signal] || :HUP

Console.info(self, "Restarting process group", signal: signal, ppid: Process.ppid)
Process.kill(signal, Process.ppid)

return {ppid: Process.ppid, signal: signal}
end

# Capture process metrics relating to the process group that the supervisor belongs to.
def do_metrics(message)
Process::Metrics::General.capture(pid: Process.ppid, ppid: Process.ppid)
Console.info(self, "Capturing process metrics", ppid: Process.ppid)
return Process::Metrics::General.capture(pid: Process.ppid, ppid: Process.ppid)
end

# Handle an incoming request.
Expand Down Expand Up @@ -69,7 +74,7 @@ def setup(container)
container.run(name: self.name, restart: true, count: 1) do |instance|
Async do
@bound_endpoint.accept do |peer|
stream = ::IO::Stream.new(peer)
stream = ::IO::Stream(peer)

while message = stream.gets("\0")
response = handle(JSON.parse(message, symbolize_names: true))
Expand Down

0 comments on commit 48e6484

Please sign in to comment.