Class: EmbeddingUtil::ServerManager

Inherits:
Object
  • Object
show all
Defined in:
lib/embedding_util/server_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: EmbeddingUtil.configuration) ⇒ ServerManager

Returns a new instance of ServerManager.



16
17
18
# File 'lib/embedding_util/server_manager.rb', line 16

def initialize(config: EmbeddingUtil.configuration)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/embedding_util/server_manager.rb', line 14

def config
  @config
end

Class Method Details

.supported?(config = EmbeddingUtil.configuration) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/embedding_util/server_manager.rb', line 20

def self.supported?(config = EmbeddingUtil.configuration)
  RuntimeCommand.available?(config.runtime)
end

Instance Method Details

#ensure_server(capability, profile: config.resolved_profile) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/embedding_util/server_manager.rb', line 24

def ensure_server(capability, profile: config.resolved_profile)
  server_model = ServerModel.for(capability, profile)
  log_path = server_log_path(server_model)

  with_lock(server_model) do
    state = read_state(server_model)
    log_path = start_background(server_model) unless healthy_state?(state) || running_server_state?(state)
  end

  wait_for_healthy(server_model, log_path: log_path)
end

#serve(model:, runtime: config.runtime, shutdown_idle: config.shutdown_idle, host: config.host, port: nil) ⇒ Object



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
# File 'lib/embedding_util/server_manager.rb', line 36

def serve(model:, runtime: config.runtime, shutdown_idle: config.shutdown_idle, host: config.host, port: nil)
  server_model = model.is_a?(ServerModel) ? model : ServerModel.parse(model)
  resolved_runtime = RuntimeCommand.resolve(runtime)
  selected_port = selected_port_for(server_model, host: host, port: port)
  command = RuntimeCommand.new(runtime: resolved_runtime, server_model: server_model, host: host, port: selected_port)
  last_output_at = Time.now

  FileUtils.mkdir_p(config.state_dir)
  puts "starting #{server_model.name} with #{command.label} on http://#{host}:#{selected_port}"
  puts "shutdown idle: #{shutdown_idle}s" if shutdown_idle&.positive?

  Open3.popen2e(*command.argv) do |_stdin, output, wait_thread|
    write_state(server_model, pid: wait_thread.pid, url: "http://#{host}:#{selected_port}", runtime: command.label, port: selected_port)
    watchdog = start_watchdog(wait_thread.pid, shutdown_idle) { last_output_at }

    output.each_line do |line|
      last_output_at = Time.now
      print line
    end

    watchdog&.kill
    delete_state(server_model)
    wait_thread.value.exitstatus
  end
end