Skip to content

Commit

Permalink
Integrate async-container-supervisor.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 27, 2025
1 parent be54958 commit b2a4056
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 231 deletions.
4 changes: 1 addition & 3 deletions bake/falcon/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
# Copyright, 2020-2025, by Samuel Williams.

def restart
require_relative "../../lib/falcon/command/supervisor"

Falcon::Command::Supervisor["restart"].call
context.lookup("async:container:supervisor:restart").call
end
21 changes: 21 additions & 0 deletions examples/benchmark/small.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const options = {
stages: [
// Warmup: Gradually ramp up:
{duration: '10s', target: 64},

// Main test: Sustained load:
{duration: '1m', target: 64},
],
};

import http from 'k6/http';
import { check, sleep } from 'k6';

export default function () {
const res = http.get('http://localhost:9292/small');

check(res, {
'is status 200': (r) => r.status === 200,
'response time < 200ms': (r) => r.timings.duration < 200,
});
}
8 changes: 8 additions & 0 deletions examples/dungeons/Modelfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM llama2
# sets the temperature to 1 [higher is more creative, lower is more coherent]
PARAMETER temperature 1
# sets the context window size to 4096, this controls how many tokens the LLM can use as context to generate the next token
PARAMETER num_ctx 4096

# sets a custom system message to specify the behavior of the chat assistant
SYSTEM You find yourself at the entrance to a dungeon. What do you do next?
37 changes: 37 additions & 0 deletions examples/dungeons/characters/cat.model
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FROM llama2:13b

PARAMETER num_ctx 16384

SYSTEM """
You are an entity in a role playing game. You will receive input from the game world and must respond with actions. Input from the game world will include nearby events and dialogue. You can perform one or more actions include speaking, moving and using things in the world. You must ONLY respond with actions in the specified format otherwise the game will respond with ERROR to inform you that the input was not understood, and you should try again using one of the defined actions.

Inputs will be formatted in the following way:

TIME [time]
The game world has advanced to the specified time.
SEE [entity] [distance] [action]
You see something.
HEAR [entity] [distance] [dialogue]
You hear something.
ARRIVE [entity]
You arrive somewhere.
ERROR [message]
Your action was not understood (perhaps try again).

Actions must be formatted in the following way:

MOVE TOWARDS [entity]
You will begin moving towards the named entity.
SPEAK "dialogue"
You will speak the specified dialogue.
USE [entity]
You will use the named entity.
LOOK
You will look around and the game will inform you of nearby entities.
PASS
Do nothing.

Before you try to interact with things, ensure you LOOK to see what is available. Then, MOVE TOWARDS the entity. Finally the game world will inform you when you ARRIVE at the entity. You may perform more than one action at a time if you list them on separate lines.

The entity you are simulating is a cat.
"""
89 changes: 89 additions & 0 deletions examples/hello-event/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2021-2023, by Samuel Williams.

$LOAD_PATH << File.expand_path("../../lib", __dir__)
$LOAD_PATH << File.expand_path("../../ext", __dir__)

require "io/event"

require "socket"
require "fiber"

class Scheduler
def initialize(selector = nil)
@fiber = Fiber.current
@selector = selector || IO::Event::Selector.new(@fiber)
@pending = []
@waiting = {}

unless @selector.respond_to?(:io_close)
instance_eval{undef io_close}
end

@mutex = Mutex.new
end

def block(blocker, timeout)
raise NotImplementedError
end

def unblock(blocker, fiber)
raise NotImplementedError
end

def io_wait(io, events, timeout)
fiber = Fiber.current
@waiting[fiber] = io
@selector.io_wait(fiber, io, events)
ensure
@waiting.delete(fiber)
end

def io_close(io)
@selector.io_close(io)
end

def kernel_sleep(duration)
@selector.defer
end

def close
while @selector.ready? || @waiting.any?
begin
@selector.select(nil)
rescue Errno::EINTR
# Ignore.
end
end
rescue Interrupt
# Exit.
end

def fiber(&block)
fiber = Fiber.new(&block)

@selector.resume(fiber)

return fiber
end
end

class DirectScheduler < Scheduler
def io_read(io, buffer, length)
fiber = Fiber.current
@waiting[fiber] = io
result = @selector.io_read(fiber, io, buffer, length)
ensure
@waiting.delete(fiber)
end

def io_write(io, buffer, length)
fiber = Fiber.current
@waiting[fiber] = io
@selector.io_write(fiber, io, buffer, length)
ensure
@waiting.delete(fiber)
end
end
33 changes: 33 additions & 0 deletions examples/hello-event/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2021-2023, by Samuel Williams.

require_relative "scheduler"
require "io/nonblock"

#scheduler = DirectScheduler.new
scheduler = Scheduler.new
Fiber.set_scheduler(scheduler)

port = Integer(ARGV.pop || 3020)

RESPONSE = "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World"

Fiber.schedule do
server = TCPServer.new("localhost", port)
server.listen(Socket::SOMAXCONN)

loop do
peer, address = server.accept

Fiber.schedule do
while request_line = peer.readpartial(1024) rescue nil
peer.write(RESPONSE)
end
ensure
peer.close
end
end
end
2 changes: 1 addition & 1 deletion examples/hello/falcon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

# append preload "preload.rb"


include Async::Container::Supervisor::Supervised
end

service "supervisor" do
Expand Down
2 changes: 1 addition & 1 deletion falcon.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Gem::Specification.new do |spec|

spec.add_dependency "async"
spec.add_dependency "async-container", "~> 0.20"
spec.add_dependency "async-container-supervisor", "~> 0.3.0"
spec.add_dependency "async-http", "~> 0.75"
spec.add_dependency "async-http-cache", "~> 0.4"
spec.add_dependency "async-service", "~> 0.10"
spec.add_dependency "bundler"
spec.add_dependency "localhost", "~> 1.1"
spec.add_dependency "openssl", "~> 3.0"
spec.add_dependency "process-metrics", "~> 0.2"
spec.add_dependency "protocol-http", "~> 0.31"
spec.add_dependency "protocol-rack", "~> 0.7"
spec.add_dependency "samovar", "~> 2.3"
Expand Down
1 change: 1 addition & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# gem "async-http", path: "../async-http-native-io"
# gem "openssl", git: "https://github.com/ruby/openssl.git"
# gem "async-container", path: "../async-container"
gem "async-container-supervisor", path: "../async-container-supervisor"
# gem "async-websocket", path: "../async-websocket"
# gem "async-http", path: "../async-http"
# gem "async-http-cache", path: "../async-http-cache"
Expand Down
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
26 changes: 4 additions & 22 deletions lib/falcon/environment/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,24 @@
# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "../service/supervisor"
require_relative "../environment"

require "io/endpoint/unix_endpoint"
require "async/container/supervisor"

module Falcon
module Environment
# Provides an environment for hosting a supervisor which can monitor multiple applications.
module Supervisor
# The service class to use for the supervisor.
# @returns [Class]
def service_class
::Falcon::Service::Supervisor
end

# The name of the supervisor
# @returns [String]
def name
"supervisor"
end
include Async::Container::Supervisor::Environment

# The IPC path to use for communication with the supervisor.
# @returns [String]
def ipc_path
::File.expand_path("supervisor.ipc", root)
end

# The endpoint the supervisor will bind to.
# @returns [::IO::Endpoint::Generic]
def endpoint
::IO::Endpoint.unix(ipc_path)
end

# Options to use when creating the container.
def container_options
{restart: true, count: 1, health_check_timeout: 30}
def monitors
[Async::Container::Supervisor::MemoryMonitor.new(interval: 10)]
end
end

Expand Down
Loading

0 comments on commit b2a4056

Please sign in to comment.