Class: Combi::QueueService
- Inherits:
-
Object
- Object
- Combi::QueueService
- Defined in:
- lib/combi/queue_service.rb
Instance Attribute Summary collapse
-
#rpc_callback ⇒ Object
Returns the value of attribute rpc_callback.
Instance Method Summary collapse
- #acknowledge(delivery_info) ⇒ Object
- #call(kind, message, options = {}) ⇒ Object
- #connect(config, &after_connect) ⇒ Object
- #create_rpc_queue ⇒ Object
- #disconnect ⇒ Object
-
#initialize(config, options) ⇒ QueueService
constructor
A new instance of QueueService.
- #log(message) ⇒ Object
- #next_ready_only(&block) ⇒ Object
- #publish(*args, &block) ⇒ Object
- #queue(name, options = {}, &block) ⇒ Object
- #ready(&block) ⇒ Object
- #reconnect ⇒ Object
- #respond(response, delivery_info) ⇒ Object
- #start ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(config, options) ⇒ QueueService
Returns a new instance of QueueService.
9 10 11 12 13 14 15 |
# File 'lib/combi/queue_service.rb', line 9 def initialize(config, ) @config = config @options = @rpc_queue = nil @ready_defer = EventMachine::DefaultDeferrable.new @ready_callbacks = [] end |
Instance Attribute Details
#rpc_callback ⇒ Object
Returns the value of attribute rpc_callback.
7 8 9 |
# File 'lib/combi/queue_service.rb', line 7 def rpc_callback @rpc_callback end |
Instance Method Details
#acknowledge(delivery_info) ⇒ Object
92 93 94 |
# File 'lib/combi/queue_service.rb', line 92 def acknowledge(delivery_info) @channel.acknowledge(delivery_info.delivery_tag, false) end |
#call(kind, message, options = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/combi/queue_service.rb', line 116 def call(kind, , = {}) log "sending request #{kind} #{.inspect[0..500]} with options #{.inspect}" raise "RPC is not enabled or reply_to is not included" if (@rpc_queue.nil? || @rpc_queue.name.nil?) && [:reply_to].nil? [:expiration] = (([:timeout] || RPC_DEFAULT_TIMEOUT) * 1000).to_i [:routing_key] ||= 'rcalls_queue' [:reply_to] ||= @rpc_queue.name request = { kind: kind, payload: , options: {} } publish(request.to_json, ) end |
#connect(config, &after_connect) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/combi/queue_service.rb', line 49 def connect(config, &after_connect) puts "[INFO] trying to connect to queue" @amqp_conn = AMQP.connect(config) do |connection, open_ok| @channel = AMQP::Channel.new @amqp_conn @channel.auto_recovery = true @exchange = @channel.direct '' after_connect.call connection.on_error do |conn, connection_close| puts "[amqp connection.close] Reply code = #{connection_close.reply_code}, reply text = #{connection_close.reply_text}" if connection_close.reply_code == 320 puts "[amqp connection.close] Setting up a periodic reconnection timer..." reconnect end end connection.on_tcp_connection_loss do |conn, settings| puts "[ERROR] Connection failed, resetting for reconnect" reconnect end end end |
#create_rpc_queue ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/combi/queue_service.rb', line 101 def create_rpc_queue @rpc_queue.unsubscribe unless @rpc_queue.nil? @rpc_queue = queue('', exclusive: true, auto_delete: true) do |rpc_queue| log "\tRPC QUEUE: #{@rpc_queue.name}" rpc_queue.subscribe do |, response| = { 'correlation_id' => .correlation_id, 'response' => response } rpc_callback.call() unless rpc_callback.nil? end @ready_defer.succeed end end |
#disconnect ⇒ Object
78 79 80 |
# File 'lib/combi/queue_service.rb', line 78 def disconnect @amqp_conn.close end |
#log(message) ⇒ Object
30 31 32 33 |
# File 'lib/combi/queue_service.rb', line 30 def log() return unless @debug_mode ||= ENV['DEBUG'] == 'true' puts "#{Time.now.to_f} #{self.class.name} #{}" end |
#next_ready_only(&block) ⇒ Object
22 23 24 |
# File 'lib/combi/queue_service.rb', line 22 def next_ready_only(&block) @ready_defer.callback &block end |
#publish(*args, &block) ⇒ Object
82 83 84 85 86 |
# File 'lib/combi/queue_service.rb', line 82 def publish(*args, &block) @exchange.publish *args do block.call if block_given? end end |
#queue(name, options = {}, &block) ⇒ Object
88 89 90 |
# File 'lib/combi/queue_service.rb', line 88 def queue(name, = {}, &block) @channel.queue(name, , &block) end |
#ready(&block) ⇒ Object
17 18 19 20 |
# File 'lib/combi/queue_service.rb', line 17 def ready(&block) @ready_callbacks << block @ready_defer.callback &block end |
#reconnect ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/combi/queue_service.rb', line 70 def reconnect @ready_defer = EventMachine::DefaultDeferrable.new @ready_callbacks.each do |callback| @ready_defer.callback &callback end start end |
#respond(response, delivery_info) ⇒ Object
96 97 98 99 |
# File 'lib/combi/queue_service.rb', line 96 def respond(response, delivery_info) response = response.call if response.respond_to? :call publish response, routing_key: delivery_info.reply_to, correlation_id: delivery_info.correlation_id end |
#start ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/combi/queue_service.rb', line 35 def start @config[:reconnect_period] ||= 4 reconnection_proc = Proc.new { EM.add_timer(@config[:reconnect_period] * rand) { start } } @config[:on_tcp_connection_failure] = reconnection_proc @config[:on_possible_authentication_failure] = reconnection_proc connect @config do if @options[:rpc] == :enabled create_rpc_queue else @ready_defer.succeed end end end |
#status ⇒ Object
26 27 28 |
# File 'lib/combi/queue_service.rb', line 26 def status @amqp_conn && @amqp_conn.status end |