Class: Cuboid::MCP::CoreTools::SpawnInstance

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/cuboid/mcp/core_tools.rb

Class Method Summary collapse

Class Method Details

.call(options: {}, start: true, live: true, server_context: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cuboid/mcp/core_tools.rb', line 137

def self.call( options: {}, start: true, live: true, server_context: nil, ** )
    CoreTools.instrumented_call do
        # Goes through the shared spawner so a configured Agent
        # provisions the instance over the grid; falls back to
        # local `Processes::Instances.spawn` when no agent is set.
        instance = ::Cuboid::Server::InstanceHelpers.spawn

        live_attached = false
        if live
            options = CoreTools.inject_live_plugin(
                options,
                instance_id:    instance.token,
                server_context: server_context
            )
            live_attached = options != nil && options['plugins'] && options['plugins']['live']
        end

        if start
            begin
                instance.run( options )
            rescue => e
                # Roll back the spawn — leaking a half-initialised
                # process is worse than leaking the error. Drop
                # any live registration we made above.
                (instance.shutdown rescue nil)
                ::Cuboid::MCP::Live.unregister( instance.token ) if live_attached
                raise e
            end
        end

        CoreTools.instances[instance.token] = instance

        result = { instance_id: instance.token, url: instance.url }
        if live_attached
            result[:live] = {
                notification_method: ::Cuboid::MCP::Live.notification_method
            }
        end
        result
    end
end