Module: Cuboid::Rest::Server::InstanceHelpers

Defined in:
lib/cuboid/rest/server/instance_helpers.rb

Constant Summary collapse

@@instances =
{}
@@agents =
{}

Instance Method Summary collapse

Instance Method Details

#agentObject



33
34
35
36
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 33

def agent
    return if !Options.agent.url
    @agent ||= connect_to_agent( Options.agent.url )
end

#agentsObject



29
30
31
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 29

def agents
    @@agents.keys
end

#connect_to_agent(url) ⇒ Object



45
46
47
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 45

def connect_to_agent( url )
    @@agents[url] ||= RPC::Client::Agent.new( url )
end

#connect_to_instance(url, token) ⇒ Object



49
50
51
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 49

def connect_to_instance( url, token )
    RPC::Client::Instance.new( url, token )
end

#connect_to_scheduler(url) ⇒ Object



72
73
74
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 72

def connect_to_scheduler( url )
    RPC::Client::Scheduler.new( url )
end

#exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 91

def exist?( id )
    instances.include? id
end

#get_instanceObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 10

def get_instance
    if agent
        options = {
          owner:   self.class.to_s,
          helpers: {
                owner: {
                    url: env['HTTP_HOST']
                }
            }
        }

        if (info = agent.spawn( options ))
            connect_to_instance( info['url'], info['token'] )
        end
    else
        Processes::Instances.spawn( application: Options.paths.application, daemonize: true )
    end
end

#instance_for(id, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 80

def instance_for( id, &block )
    cleanup = proc do
        instances.delete( id ).close
        session.delete id
    end

    handle_error cleanup do
        block.call @@instances[id]
    end
end

#instancesObject



76
77
78
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 76

def instances
    @@instances
end

#schedulerObject



67
68
69
70
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 67

def scheduler
    return if !Options.scheduler.url
    @scheduler ||= connect_to_scheduler( Options.scheduler.url )
end

#unplug_agent(url) ⇒ Object



38
39
40
41
42
43
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 38

def unplug_agent( url )
    connect_to_agent( url ).node.unplug

    c = @@agents.delete( url )
    c.close if c
end

#update_from_schedulerObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cuboid/rest/server/instance_helpers.rb', line 53

def update_from_scheduler
    return if !scheduler

    scheduler.running.each do |id, info|
        instances[id] ||= connect_to_instance( info['url'], info['token'] )
    end

    (scheduler.failed.keys | scheduler.completed.keys).each do |id|
        session.delete id
        client = instances.delete( id )
        client.close if client
    end
end