Module: OpenNebula::WaitExtEvent

Defined in:
lib/opennebula/wait_ext.rb

Overview

Module to wait OpenNebula objects events using ZMQ

Instance Method Summary collapse

Instance Method Details

#wait2(sstr1, sstr2, timeout = 60, cycles = -1)) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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