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

#clientObject



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

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

    # If not, get one via cloud_auth
    @cloud_auth.client
end

#each(&block) ⇒ Object



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

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



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/opennebula/flow/service_pool.rb', line 111

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_client = 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:



132
133
134
135
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
# File 'lib/opennebula/flow/service_pool.rb', line 132

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

    if external_client.nil?
        aux_client = client
    else
        aux_client = external_client
    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)
        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



77
78
79
80
81
82
83
84
# File 'lib/opennebula/flow/service_pool.rb', line 77

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

    @one_pool = osp

    rc
end

#info_allObject



86
87
88
89
90
91
92
93
# File 'lib/opennebula/flow/service_pool.rb', line 86

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

    @one_pool = osp

    rc
end

#to_jsonObject

rubocop:disable Lint/ToJSON



96
97
98
99
# File 'lib/opennebula/flow/service_pool.rb', line 96

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