From 64dd4049e2a9d0e9fdc1bf10c289848348807421 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sun, 25 Aug 2024 11:46:22 +1200 Subject: [PATCH] Fix supervisor implementation + add more tests. --- lib/falcon/environment/supervisor.rb | 2 ++ lib/falcon/service/supervisor.rb | 17 +++++++++- test/falcon/service/supervisor.rb | 46 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 test/falcon/service/supervisor.rb diff --git a/lib/falcon/environment/supervisor.rb b/lib/falcon/environment/supervisor.rb index dcf682eb..ac987ec8 100644 --- a/lib/falcon/environment/supervisor.rb +++ b/lib/falcon/environment/supervisor.rb @@ -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. diff --git a/lib/falcon/service/supervisor.rb b/lib/falcon/service/supervisor.rb index bd75a364..4711fc59 100644 --- a/lib/falcon/service/supervisor.rb +++ b/lib/falcon/service/supervisor.rb @@ -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. @@ -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)) @@ -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 diff --git a/test/falcon/service/supervisor.rb b/test/falcon/service/supervisor.rb new file mode 100644 index 00000000..298a0a6e --- /dev/null +++ b/test/falcon/service/supervisor.rb @@ -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