Class: AlchemyFlux::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemy-flux.rb

Overview

An Alchemy Flux Service

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Service) initialize(name, options = {}, &block)

Create a AlchemyFlux service instance

name the name of the service being created options



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/alchemy-flux.rb', line 42

def initialize(name, options = {}, &block)
  @name = name
  @options = {
      ampq_uri: 'amqp://localhost',
      prefetch: 20,
      timeout: 1000,
      threadpool_size: 500,
      resource_paths: []
  }.merge(options)

  @service_fn = block || Proc.new { |message| "" }

  @uuid = "#{@name}.#{AlchemyFlux::Service.generateUUID()}"
  @transactions = {}
  @processing_messages = 0

  @response_queue_name = @uuid
  @service_queue_name = @name
  @state = :stopped
end

Instance Attribute Details

- (Object) processing_messages (readonly)

The incoming number of messages being processed



30
31
32
# File 'lib/alchemy-flux.rb', line 30

def processing_messages
  @processing_messages
end

- (Object) state (readonly)

The current state of the Service, either stopped or started



24
25
26
# File 'lib/alchemy-flux.rb', line 24

def state
  @state
end

- (Object) transactions (readonly)

The outgoing message transactions



27
28
29
# File 'lib/alchemy-flux.rb', line 27

def transactions
  @transactions
end

Class Method Details

+ (Object) generateUUID

Generate a UUID string



33
34
35
# File 'lib/alchemy-flux.rb', line 33

def self.generateUUID
  UUIDTools::UUID.random_create.to_i.to_s(16).ljust(32,'0')
end

+ (Object) start(ampq_uri = 'amqp://localhost', threadpool_size = 500)

Start the EventMachine and AMQP connections for all Services

The application has two or more threads

  1. The Controller Thread (e.g. the rspec thread)

  2. The EM Thread

  3. The EM defer Threads

When we start a Service we do it in a Thread so that it will not block the calling Thread

When the FIRST Service is started EM.run initialises in that Thread When the second Service is initialises the block is executed in the new thread, but all the callbacks will be executed in the EM thread



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/alchemy-flux.rb', line 89

def self.start(ampq_uri = 'amqp://localhost', threadpool_size=500)
  return if EM.reactor_running?
  start_blocker = Queue.new
  Thread.new do
    Thread.current["name"] = "EM Thread" if EM.reactor_thread?
    Thread.current.abort_on_exception = true
    EM.threadpool_size = threadpool_size
    AMQP.start(ampq_uri) do |connection|
      @@connection = connection
      @@connection.on_error do |conn, connection_close|
        message = "Channel exception: [#{connection_close.reply_code}] #{connection_close.reply_text}"
        puts message
        raise message
      end
      start_blocker << :unblock
    end
  end
  start_blocker.pop
end

+ (Object) stop

Stop EventMachine and the



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/alchemy-flux.rb', line 110

def self.stop
  return if !EM.reactor_running?
  stop_blocker = Queue.new

  #last tick
  AMQP.stop do
    EM.stop_event_loop
    stop_blocker << :unblock
  end
  stop_blocker.pop
  sleep(0.05) # to ensure it finished
end

Instance Method Details

- (Object) inspect

overriding inspect



64
65
66
# File 'lib/alchemy-flux.rb', line 64

def inspect
  to_s
end

- (Object) send_message(exchange, routing_key, message, options)

send a message to an exchange with routing key

exchange

A AMQP exchange

routing_key

The routing key to use

message

The message to be sent

options

The message options



297
298
299
300
301
302
303
# File 'lib/alchemy-flux.rb', line 297

def send_message(exchange, routing_key, message, options)
  message_options = options.merge({:routing_key => routing_key})
  message = MessagePack.pack(message)
  EventMachine.next_tick do
    exchange.publish message, message_options
  end
end

- (Object) send_message_to_resource(message)

send a message to a resource

message

the message to be sent to the path in the message

This method can optionally take a block which will be executed asynchronously and yielded the response



326
327
328
329
330
331
332
333
334
335
# File 'lib/alchemy-flux.rb', line 326

def send_message_to_resource(message)
  routing_key = path_to_routing_key(message['path'])
  if block_given?
    EventMachine.defer do
      yield send_message_to_resource(message)
    end
  else
    send_HTTP_request_message(@resources_exchange, routing_key, message)
  end
end

- (Object) send_message_to_service(service_name, message)

send a message to a service

service_name

the name of the service

message

the message to be sent

This method can optionally take a block which will be executed asynchronously and yielded the response



311
312
313
314
315
316
317
318
319
# File 'lib/alchemy-flux.rb', line 311

def send_message_to_service(service_name, message)
  if block_given?
    EventMachine.defer do
      yield send_message_to_service(service_name, message)
    end
  else
    send_HTTP_request_message(@channel.default_exchange, service_name, message)
  end
end

- (Object) start

start the service



124
125
126
127
128
129
130
131
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
# File 'lib/alchemy-flux.rb', line 124

def start
  return if @state != :stopped

  Service.start(@options[:ampq_uri], @options[:threadpool_size])
  EM.run do

    @channel = AMQP::Channel.new(@@connection)

    @channel.on_error do |ch, channel_close|
      message = "Channel exception: [#{channel_close.reply_code}] #{channel_close.reply_text}"
      puts message
      raise message
    end

    @channel.prefetch(@options[:prefetch])
    @channel.auto_recovery = true

    @service_queue = @channel.queue( @service_queue_name, {:durable => true})
    @service_queue.subscribe({:ack => true}) do |, payload|
      payload = MessagePack.unpack(payload)
      process_service_queue_message(, payload)
    end

    response_queue = @channel.queue(@response_queue_name, {:exclusive => true, :auto_delete => true})
    response_queue.subscribe({}) do |, payload|
      payload = MessagePack.unpack(payload)
      process_response_queue_message(, payload)
    end

    @channel.default_exchange.on_return do |basic_return, frame, payload|
      payload = MessagePack.unpack(payload)
      process_returned_message(basic_return, frame.properties, payload)
    end

    # RESOURCES HANDLE
    @resources_exchange = @channel.topic("resources.exchange", {:durable => true})
    @resources_exchange.on_return do |basic_return, frame, payload|
      payload = MessagePack.unpack(payload)
      process_returned_message(basic_return, frame.properties, payload)
    end

    bound_resources = 0
    for resource_path in @options[:resource_paths]
      binding_key = "#{path_to_routing_key(resource_path)}.#"
      @service_queue.bind(@resources_exchange, :key => binding_key) {
        bound_resources += 1
      }
    end
    begin
      # simple loop to wait for the resources to be bound
      sleep(0.01)
    end until bound_resources == @options[:resource_paths].length

    @state = :started
  end
end

- (Object) stop

Stop the Service

This method:

  • Stops receiving new messages

  • waits for processing incoming and outgoing messages to be completed

  • close the channel



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/alchemy-flux.rb', line 187

def stop
  return if @state != :started
  # stop receiving new incoming messages
  @service_queue.unsubscribe
  # only stop the service if all incoming and outgoing messages are complete
  decisecond_timeout = @options[:timeout]/100
  waited_deciseconds = 0 # guarantee that this loop will stop
  while (@transactions.length > 0 || @processing_messages > 0) && waited_deciseconds < decisecond_timeout
    sleep(0.1) # wait a decisecond to check the incoming and outgoing messages again
    waited_deciseconds += 1
  end

  @channel.close
  @state = :stopped
end

- (Object) to_s

overriding to_s



69
70
71
# File 'lib/alchemy-flux.rb', line 69

def to_s
  "AlchemyFlux::Service(#{@name.inspect}, #{@options.inspect})"
end