Skip to content

Commit

Permalink
Fix supervisor implementation + add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 24, 2024
1 parent 1f5c352 commit 64dd404
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/falcon/environment/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
require_relative '../service/supervisor'
require_relative '../environment'

require 'io/endpoint/unix_endpoint'

module Falcon
module Environment
# Provides an environment for hosting a supervisor which can monitor multiple applications.
Expand Down
17 changes: 16 additions & 1 deletion lib/falcon/service/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

require 'async/service/generic'

require 'io/endpoint/bound_endpoint'
require 'io/stream'

module Falcon
module Service
# Implements a host supervisor which can restart the host services and provide various metrics about the running processes.
Expand Down Expand Up @@ -69,7 +72,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 All @@ -91,6 +94,18 @@ def stop

super
end

def invoke(command)
@bound_endpoint.local_address_endpoint.connect do |peer|
stream = ::IO::Stream(peer)

stream.puts(command.to_json, separator: "\0")

response = JSON.parse(stream.gets("\0"), symbolize_names: true)

return response
end
end
end
end
end
46 changes: 46 additions & 0 deletions test/falcon/service/supervisor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require 'falcon/service/supervisor'
require 'falcon/configuration'
require 'falcon/environment/supervisor'

describe Falcon::Service::Supervisor do
let(:environment) do
Async::Service::Environment.new(Falcon::Environment::Supervisor).with(
root: File.expand_path('.supervisor', __dir__),
)
end

let(:supervisor) do
subject.new(environment)
end

it "can create a supervisor" do
expect(supervisor).to be_a subject
end

it "can start and stop server" do
container = Async::Container.new

supervisor.start
supervisor.setup(container)
container.wait_until_ready

expect(container.group.running).to have_attributes(size: be == 1)

response = supervisor.invoke({please: 'metrics'})

expect(response).to be_a(Hash)

# The supervisor should report itself:
expect(response.values).to have_value(have_keys(
command: be == "supervisor"
))
ensure
supervisor.stop
container.stop
end
end

0 comments on commit 64dd404

Please sign in to comment.