Module: Maestro::MQHelper

Defined in:
lib/maestro_common/helpers/mq_helper.rb

Defined Under Namespace

Classes: MQError

Constant Summary collapse

@@INFINITE_RETRIES =
2**32-1
@@stomp =
nil
@@subs =
{}
@@tries =
0
@@sleep =
20
@@uri =
nil

Class Method Summary collapse

Class Method Details

.connectObject



38
39
40
41
42
43
44
45
46
47
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/maestro_common/helpers/mq_helper.rb', line 38

def MQHelper.connect
  if @@stomp.nil?
    Maestro.log.info "Request for stomp connection - creating"

    # retry info - the retryable below doesn't support 'unlimited' but if we give it a really big number...
    if @@tries == 0
      @@tries = Maestro.stomp_config['max_retries']

      if @@tries.nil?
        @@tries = @@INFINITE_RETRIES
        Maestro.log.info "Connect retry attempt count not specified, using ~INFINITE (really big number)"
      else
        Maestro.log.info "Connect retry attempt count not specified, using config file setting #{@@tries}"
      end
    end

    @@sleep = Maestro.stomp_config['retry_seconds'] || 20

    # New form is config.url = some_url
    # Old form is config.user, config.passcode, config.host, config.port,
    # config.ssl
    # Check for new config first, it overrides
    url = Maestro.stomp_config['url']

    if url && !url.empty?
      # New form
      is_failover = url.start_with?('failover:')
      options = {}

      if url.include?('+ssl')
        options[:ssl] = { :ca_file => Maestro.stomp_config['cert'] || nil, :verify_mode => OpenSSL::SSL::VERIFY_NONE }
      end

      @@uri = url

      if is_failover
        Maestro.log.info "Using OnStomp Failover Client"
        client = OnStomp::Failover::Client.new(url, options)
      else
        client = OnStomp::Client.new(url, options)
      end
    else
      # Old form
      Maestro.log.debug "Old-form stomp config being used"
      user = Maestro.stomp_config['user']
      passcode = Maestro.stomp_config['passcode']
      host = Maestro.stomp_config['host']
      port = Maestro.stomp_config['port'].to_s
      ssl = Maestro.stomp_config['ssl'] ? "+ssl" : ""
      cert = Maestro.stomp_config['cert'] || nil
  
      # construct the connection URI
      user_and_password = [user,passcode].reject{ |e| e.nil? || e.empty? }.join(":")
      host_and_port     = [host,port].reject{|e| e.nil? || e.empty?}.join(":")
      protocol          = ['stomp', ssl, '://'].reject{|e| e.nil? || e.empty?}.join
      @@uri             = protocol + [host_and_port, user_and_password].reject{|e| e.nil? || e.empty?}.reverse.join("@")
  
      if (!ssl.nil? && ssl == "+ssl")
        client = OnStomp::Client.new(@@uri, :ssl => {:ca_file => cert, :verify_mode => OpenSSL::SSL::VERIFY_NONE})
      else
        client = OnStomp::Client.new(@@uri)
      end
    end

    @@stomp = client
    reconnect
    raise "Failed To Connect To Maestro" unless client.connected?
  end
  
  @@stomp
end

.connectionObject



26
27
28
29
30
31
# File 'lib/maestro_common/helpers/mq_helper.rb', line 26

def MQHelper.connection
  if @@stomp.nil?
    Maestro.log.warn "Request for stomp connection, but not set up yet!"
  end
  @@stomp
end

.connection=(stomp) ⇒ Object

only for testing



34
35
36
# File 'lib/maestro_common/helpers/mq_helper.rb', line 34

def MQHelper.connection=(stomp)
  @@stomp = stomp
end

.disconnectObject



136
137
138
139
140
# File 'lib/maestro_common/helpers/mq_helper.rb', line 136

def MQHelper.disconnect
  if !@@stomp.nil?
    @@stomp.disconnect
  end
end

.reconnectObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/maestro_common/helpers/mq_helper.rb', line 110

def MQHelper.reconnect
  if !@@stomp
    raise Maestro::MQHelper::MQError, "Cannot reconnect, no existing MQ instance.  Call 'connect' to create initial connection, then use 'reconnect' if connection fails"
  end

  if !@@stomp.connected?
    Maestro::Utils.retryable(:tries => @@tries, :sleep => @@sleep) do
      Maestro.log.info "Connecting To stomp At #{@@uri}"
      @@stomp.connect
    end

    if @@stomp.connected?
      oldsubs = @@subs.dup
      @@subs.clear
      oldsubs.each do |dest, info|
        Maestro.log.info "Resubscribing to #{dest}"
        do_subscribe(dest, info['headers'], &info['callback'])
      end
    else
      Maestro.log.warn "Unable to reconnect to subscriptions because no MQ Connection"
    end
  end

  @@stomp.connected?
end

.resetObject



17
18
19
20
21
22
23
24
# File 'lib/maestro_common/helpers/mq_helper.rb', line 17

def MQHelper.reset
  Maestro.log.info "Resetting MQHelper"
  @@stomp = nil
  @@subs = {}
  @@tries = 0
  @@sleep = 20
  @@uri = nil
end

.subscribe(dest, headers = {}, &cb) ⇒ Object



142
143
144
145
# File 'lib/maestro_common/helpers/mq_helper.rb', line 142

def MQHelper.subscribe(dest, headers={}, &cb)
  Maestro.log.info "MQHelper: Subscribing to #{dest}"
  do_subscribe(dest, headers, &cb)
end

.unsubscribe(dest, &cb) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/maestro_common/helpers/mq_helper.rb', line 147

def MQHelper.unsubscribe(dest, &cb)
  if @@subs.has_key?(dest)
    Maestro.log.info "MQHelper: Unsubscribing from #{dest}"
    @@stomp.unsubscribe(@@subs[dest]['frame'])
    @@subs.delete(dest)
  end
end