module OpenNebula::WaitExtEvent

Module to wait OpenNebula objects events using ZMQ

Public Instance Methods

wait2(sstr1, sstr2, timeout = 60, cycles = -1) click to toggle source
# File lib/opennebula/wait_ext.rb, line 48
def wait2(sstr1, sstr2, timeout = 60, cycles = -1)
    wfun = OpenNebula::WaitExt::WAIT[self.class]

    # Start with a timeout of 2 seconds, to wait until the first
    # info.
    #
    # The timeout is increased later, to avoid multiple info calls.
    c_timeout = 2
    recvs     = 0
    in_state  = false

    # Subscribe with timeout seconds
    #
    # Subscribe string:
    #
    #   EVENT STATE element_name/state_str//self.ID
    #
    #   - element_name: is the element name to find in the message
    #   - self.ID: returns element ID to find in the message
    ctx = ZMQ::Context.new(1)

    until in_state || (cycles != -1 && recvs >= cycles)
        content = wait_event(ctx,
                             wfun[:event].call(self, sstr1, sstr2),
                             c_timeout)

        if content && !content.empty?
            in_state = wfun[:in_state_e].call(sstr1, sstr2, content)

            break if in_state
        end

        c_timeout *= 10
        c_timeout  = timeout if c_timeout > timeout

        rco = info

        return false if OpenNebula.is_error?(rco)

        in_state = wfun[:in_state].call(self, sstr1, sstr2)

        recvs += 1
    end

    in_state
end
wait_event(ctx, event, timeout) click to toggle source
# File lib/opennebula/wait_ext.rb, line 26
def wait_event(ctx, event, timeout)
    subscriber = ctx.socket(ZMQ::SUB)

    # Create subscriber
    key        = ''
    content    = ''

    subscriber.setsockopt(ZMQ::RCVTIMEO, timeout * 1000)
    subscriber.setsockopt(ZMQ::SUBSCRIBE, event)
    subscriber.connect(@client.one_zmq)

    rc = subscriber.recv_string(key)
    rc = subscriber.recv_string(content) if rc != -1

    return if ZMQ::Util.errno == ZMQ::EAGAIN || rc == -1

    content
ensure
    subscriber.setsockopt(ZMQ::UNSUBSCRIBE, event)
    subscriber.close
end