Skip to content
Kward Search API index

Class: Kward::Transport::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/transport/manager.rb

Overview

Starts and supervises registered transport adapters.

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(registry:, gateway: nil, plugin_chat_gateway: nil, config_root: nil, config_provider: nil, policy: nil, logger: nil) ⇒ Manager

Returns a new instance of Manager.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kward/transport/manager.rb', line 11

def initialize(registry:, gateway: nil, plugin_chat_gateway: nil, config_root: nil, config_provider: nil, policy: nil, logger: nil)
  @registry = registry
  @gateway = gateway
  @plugin_chat_gateway = plugin_chat_gateway
  @config_root = config_root
  @config_provider = config_provider
  @policy = policy
  @logger = logger || Logger.new($stderr)
  @entries = {}
  @mutex = Mutex.new
end

Instance Method Details

#listObject



23
24
25
# File 'lib/kward/transport/manager.rb', line 23

def list
  @registry.transports.map { |type| descriptor(type) }
end

#reload(registry) ⇒ Object



101
102
103
104
105
# File 'lib/kward/transport/manager.rb', line 101

def reload(registry)
  shutdown
  @mutex.synchronize { @registry = registry }
  list
end

#restart(name_or_id, config: nil) ⇒ Object



96
97
98
99
# File 'lib/kward/transport/manager.rb', line 96

def restart(name_or_id, config: nil)
  stop(name_or_id)
  start(name_or_id, config: config)
end

#shutdownObject



107
108
109
110
111
# File 'lib/kward/transport/manager.rb', line 107

def shutdown
  entries = @mutex.synchronize { @entries.values.dup }
  entries.each { |entry| stop(entry.type.id) if entry.state == "running" || entry.state == "starting" }
  nil
end

#start(name_or_id, config: nil, workspace_root: nil) ⇒ Object



33
34
35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kward/transport/manager.rb', line 33

def start(name_or_id, config: nil, workspace_root: nil)
  type = resolve_type(name_or_id)
  @mutex.synchronize do
    raise "Transport #{type.id} is already running" if running_entry?(type.id)
  end

  config ||= @config_provider&.call(type.id) || {}
  config = config.merge("workspace" => workspace_root.to_s) if workspace_root
  gateway = @gateway.respond_to?(:call) ? @gateway.call(type.id) : @gateway
  plugin_chat_gateway = @plugin_chat_gateway.respond_to?(:call) ? @plugin_chat_gateway.call(type.id) : @plugin_chat_gateway
  host = Host.new(
    transport_id: type.id,
    gateway: gateway,
    plugin_chat_gateway: plugin_chat_gateway,
    config: config,
    storage: @config_root && Store.new(type.id, root: @config_root),
    policy: @policy,
    logger: @logger,
    execution_profile: type.execution_profile
  )
  adapter = type.handler.call(host, host.config)
  unless adapter.respond_to?(:start) && adapter.respond_to?(:stop)
    raise "Transport #{type.id} must implement start and stop"
  end

  entry = Entry.new(type: type, host: host, adapter: adapter, state: "starting")
  @mutex.synchronize { @entries[type.id] = entry }
  begin
    adapter.start
    @mutex.synchronize do
      entry.state = "running"
      entry.started_at = Time.now.utc
      entry.error = nil
    end
    adapter
  rescue StandardError => e
    @mutex.synchronize do
      entry.state = "failed"
      entry.error = e.message
      entry.stopped_at = Time.now.utc
    end
    log_error(type, e)
    raise
  end
end

#status(name_or_id = nil) ⇒ Object



27
28
29
30
31
# File 'lib/kward/transport/manager.rb', line 27

def status(name_or_id = nil)
  return status_for(resolve_type(name_or_id)) if name_or_id

  list.map { |type| status_for(type) }
end

#stop(name_or_id) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kward/transport/manager.rb', line 79

def stop(name_or_id)
  type = resolve_type(name_or_id)
  entry = @mutex.synchronize { @entries[type.id] }
  return false unless entry

  begin
    entry.adapter.stop
  ensure
    entry.host.shutdown
    @mutex.synchronize do
      entry.state = "stopped"
      entry.stopped_at = Time.now.utc
    end
  end
  true
end