Class: OpenNebula::ServicePool

Inherits:
Pool show all
Defined in:
lib/opennebula/flow/service_pool.rb

Overview

ServicePool class

Constant Summary collapse

@@mutex =

rubocop:disable Style/ClassVars

Mutex.new
@@mutex_hash =
{}

Constants inherited from Pool

Pool::INFO_ALL, Pool::INFO_GROUP, Pool::INFO_MINE, Pool::INFO_PRIMARY_GROUP, Pool::PAGINATED_POOLS

Instance Attribute Summary

Attributes inherited from Pool

#element_name, #pool_name

Instance Method Summary collapse

Methods inherited from Pool

#each_page_delete, #each_with_xpath, #get_hash, #get_page, #info_paginated, #is_paginated?, #loop_page, #to_str

Methods inherited from XMLPool

#each_element

Methods inherited from XMLElement

#[], #add_element, #attr, build_xml, #delete_element, #each_xpath, #element_xml, #has_elements?, #initialize_xml, #name, #retrieve_elements, #retrieve_xmlelements, #set_content, #template_like_str, #template_str, #template_xml, #text, #to_hash, #to_xml, #xml_nil?

Constructor Details

#initialize(cloud_auth, client) ⇒ DocumentPool

Class constructor

Parameters:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/opennebula/flow/service_pool.rb', line 51

def initialize(cloud_auth, client)
    # TODO, what if cloud_auth is nil?
    @cloud_auth = cloud_auth
    @client     = client
    @one_pool   = nil

    if @client
        rc = @client.call('user.info', -1)

        unless OpenNebula.is_error?(rc)
            info     = Nokogiri::XML(rc)
            @user_id = Integer(info.xpath('/USER/ID').text)
        end
    end

    super('DOCUMENT_POOL', 'DOCUMENT', @client)
end

Instance Method Details

#client(user_name = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/opennebula/flow/service_pool.rb', line 69

def client(user_name = nil)
    # If there's a client defined use it
    return @client unless @client.nil?

    # If not, get one via cloud_auth
    if user_name.nil?
        @cloud_auth.client
    else
        @cloud_auth.client(user_name)
    end
end

#each(&block) ⇒ Object



105
106
107
108
109
# File 'lib/opennebula/flow/service_pool.rb', line 105

def each(&block)
    return if @one_pool.nil?

    @one_pool.each(&block)
end

#each_page(size) ⇒ Object

Iterates over pool pages

size

nil => default page size > 0 => page size

state state of objects



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/opennebula/flow/service_pool.rb', line 115

def each_page(size)
    loop_page(size, Service::DOCUMENT_TYPE, false) do |element, page|
        page.each("//#{element}") do |obj|
            service = Service.new_with_id(obj['ID'], @client)
            service.info

            yield(service)
        end

        size
    end
end

#get(service_id, external_user = nil) {|this| ... } ⇒ Service, OpenNebula::Error

Retrieves a Service element from OpenNebula. The Service::info() method is called

Parameters:

  • service_id (Integer)

    Numerical Id of the service to retrieve

Yield Parameters:

  • this (Service)

    block will have the service’s mutex locked. The mutex will be unlocked after the block execution.

Returns:



136
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/opennebula/flow/service_pool.rb', line 136

def get(service_id, external_user = nil, &block)
    service_id = service_id.to_i if service_id
    aux_client = nil

    # WARNING!!!
    # No validation will be performed for external_user, the credentials
    # for this user must be validated previously.
    if external_user.nil?
        aux_client = client
    else
        aux_client = client(external_user)
    end

    service = Service.new_with_id(service_id, aux_client)

    if block_given?
        obj_mutex = nil
        entry     = nil

        @@mutex.synchronize do
            # entry is an array of [Mutex, waiting]
            # waiting is the number of threads waiting on this mutex
            entry = @@mutex_hash[service_id]

            if entry.nil?
                entry = [Mutex.new, 0]
                @@mutex_hash[service_id] = entry
            end

            obj_mutex = entry[0]
            entry[1]  = entry[1] + 1

            if @@mutex_hash.size > 10000
                @@mutex_hash.delete_if do |_s_id, entry_loop|
                    entry_loop[1] == 0
                end
            end
        end

        rc = obj_mutex.synchronize do
            rc = service.info

            if OpenNebula.is_error?(rc)
                return rc
            end

            block.call(service, client)
        end

        @@mutex.synchronize do
            entry[1] = entry[1] - 1
        end

        if OpenNebula.is_error?(rc)
            return rc
        end
    else
        service.info
    end

    service
end

#infoObject



81
82
83
84
85
86
87
88
# File 'lib/opennebula/flow/service_pool.rb', line 81

def info
    osp = OpenNebulaServicePool.new(client)
    rc  = osp.info

    @one_pool = osp

    rc
end

#info_allObject



90
91
92
93
94
95
96
97
# File 'lib/opennebula/flow/service_pool.rb', line 90

def info_all
    osp = OpenNebulaServicePool.new(client)
    rc  = osp.info_all

    @one_pool = osp

    rc
end

#to_jsonObject

rubocop:disable Lint/ToJSON



100
101
102
103
# File 'lib/opennebula/flow/service_pool.rb', line 100

def to_json
    # rubocop:enable Lint/ToJSON
    @one_pool.to_json
end