thrifty-bunny 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9981672891ec16ef024800ac90c8db849bd095c
4
- data.tar.gz: 4d9ba9f1bb935b9d19a2fa52436b6f046da012ac
3
+ metadata.gz: 4e3d89688b528994464eb6bb36f9df5b307cadb2
4
+ data.tar.gz: ccc99ab3da111fef05d6b278bf9c291ef3d5bbae
5
5
  SHA512:
6
- metadata.gz: a863b47d7fd30c08a8adbe5424f57105d3fe1cda3c961e1fb3bd3e02a7f38a3572e5ff8edb5f0dfc77bb2b6c1275c23ea5e86f1aea865a1030c7903a559851ac
7
- data.tar.gz: 4d851551d98dc06c1abdb2bf70b93a2bc33ed5b9d2526f73aafaa90cbcd6605e2b27f169bbeb3bfd308cbeefd502a0616e54194bf2a1e5292d41c925e10e2efd
6
+ metadata.gz: c372c9bfe06f693c940f6f5990f308a2ef8a31cd6d5bb33033d58c55641396f47b2e4e78c71f7b365ee1f8486023eac65f4032b1a59c2c42e7b555f821d7e9e7
7
+ data.tar.gz: 245f15e874aac20a98aa549820b7a7d77431a14f0fbb0a59b797a2c75bcd39f72e7693e3e2614092b20b4e97827a1533504b4fd4550500e9b45da7f876e16c93
data/.pryrc ADDED
@@ -0,0 +1,6 @@
1
+ #~/.pryrc
2
+
3
+ $:.unshift "examples/calculator"
4
+ $:.unshift "lib/"
5
+
6
+ require 'pry'
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ namespace :generate do
12
12
  outdir = 'examples/calculator'
13
13
  file = "#{outdir}/calculator_service.thrift"
14
14
  %x[thrift -gen rb --out #{outdir} #{file}]
15
- puts "Ruby files generated for #{file} to #{outdir}"
15
+ puts "Ruby files generated for #{file} to #{outdir}. Now go write the handler!"
16
16
  end
17
17
 
18
18
  end
@@ -19,19 +19,40 @@ module Calculator
19
19
 
20
20
  def ping
21
21
  end
22
+
23
+ def dwarves
24
+ %w(sneezy dopey doc)
25
+ end
26
+
27
+ def my_pets
28
+ [
29
+ Pet.new(kind: 'cat', name: 'Winston'),
30
+ Pet.new(kind: 'dog', name: 'Eve')
31
+ ]
32
+ end
33
+
34
+ def age(age_min, age_max)
35
+ age_max - age_min
36
+ end
37
+
38
+ def snooze(sleep_time)
39
+ sleep sleep_time
40
+ end
41
+
22
42
  end
23
43
 
24
44
  class Server
25
45
  attr_reader :service
26
46
 
27
47
  def initialize(options={})
48
+ config = ThriftyBunny::Config.new(options)
28
49
  handler = Handler.new
29
50
  processor = CalculatorService::Processor.new(handler)
30
- @service = ThriftyBunny::RpcServer.new(processor)
51
+ @service = ThriftyBunny::RpcServer.new(processor, config)
31
52
  end
32
53
 
33
54
  def serve
34
- service.serve(log_messages: false, prefetch: 2)
55
+ service.serve(prefetch: 2)
35
56
  end
36
57
  end
37
58
 
@@ -1,5 +1,5 @@
1
1
  #
2
- # Autogenerated by Thrift Compiler (0.9.1)
2
+ # Autogenerated by Thrift Compiler (0.9.2)
3
3
  #
4
4
  # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
5
  #
@@ -71,6 +71,65 @@ module CalculatorService
71
71
  return
72
72
  end
73
73
 
74
+ def dwarves()
75
+ send_dwarves()
76
+ return recv_dwarves()
77
+ end
78
+
79
+ def send_dwarves()
80
+ send_message('dwarves', Dwarves_args)
81
+ end
82
+
83
+ def recv_dwarves()
84
+ result = receive_message(Dwarves_result)
85
+ return result.success unless result.success.nil?
86
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'dwarves failed: unknown result')
87
+ end
88
+
89
+ def my_pets()
90
+ send_my_pets()
91
+ return recv_my_pets()
92
+ end
93
+
94
+ def send_my_pets()
95
+ send_message('my_pets', My_pets_args)
96
+ end
97
+
98
+ def recv_my_pets()
99
+ result = receive_message(My_pets_result)
100
+ return result.success unless result.success.nil?
101
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'my_pets failed: unknown result')
102
+ end
103
+
104
+ def age(age_min, age_max)
105
+ send_age(age_min, age_max)
106
+ return recv_age()
107
+ end
108
+
109
+ def send_age(age_min, age_max)
110
+ send_message('age', Age_args, :age_min => age_min, :age_max => age_max)
111
+ end
112
+
113
+ def recv_age()
114
+ result = receive_message(Age_result)
115
+ return result.success unless result.success.nil?
116
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'age failed: unknown result')
117
+ end
118
+
119
+ def snooze(sleep_time)
120
+ send_snooze(sleep_time)
121
+ recv_snooze()
122
+ end
123
+
124
+ def send_snooze(sleep_time)
125
+ send_message('snooze', Snooze_args, :sleep_time => sleep_time)
126
+ end
127
+
128
+ def recv_snooze()
129
+ result = receive_message(Snooze_result)
130
+ return
131
+ end
132
+
74
133
  end
75
134
 
76
135
  class Processor
@@ -108,6 +167,34 @@ module CalculatorService
108
167
  write_result(result, oprot, 'ping', seqid)
109
168
  end
110
169
 
170
+ def process_dwarves(seqid, iprot, oprot)
171
+ args = read_args(iprot, Dwarves_args)
172
+ result = Dwarves_result.new()
173
+ result.success = @handler.dwarves()
174
+ write_result(result, oprot, 'dwarves', seqid)
175
+ end
176
+
177
+ def process_my_pets(seqid, iprot, oprot)
178
+ args = read_args(iprot, My_pets_args)
179
+ result = My_pets_result.new()
180
+ result.success = @handler.my_pets()
181
+ write_result(result, oprot, 'my_pets', seqid)
182
+ end
183
+
184
+ def process_age(seqid, iprot, oprot)
185
+ args = read_args(iprot, Age_args)
186
+ result = Age_result.new()
187
+ result.success = @handler.age(args.age_min, args.age_max)
188
+ write_result(result, oprot, 'age', seqid)
189
+ end
190
+
191
+ def process_snooze(seqid, iprot, oprot)
192
+ args = read_args(iprot, Snooze_args)
193
+ result = Snooze_result.new()
194
+ @handler.snooze(args.sleep_time)
195
+ write_result(result, oprot, 'snooze', seqid)
196
+ end
197
+
111
198
  end
112
199
 
113
200
  # HELPER FUNCTIONS AND STRUCTURES
@@ -244,5 +331,132 @@ module CalculatorService
244
331
  ::Thrift::Struct.generate_accessors self
245
332
  end
246
333
 
334
+ class Dwarves_args
335
+ include ::Thrift::Struct, ::Thrift::Struct_Union
336
+
337
+ FIELDS = {
338
+
339
+ }
340
+
341
+ def struct_fields; FIELDS; end
342
+
343
+ def validate
344
+ end
345
+
346
+ ::Thrift::Struct.generate_accessors self
347
+ end
348
+
349
+ class Dwarves_result
350
+ include ::Thrift::Struct, ::Thrift::Struct_Union
351
+ SUCCESS = 0
352
+
353
+ FIELDS = {
354
+ SUCCESS => {:type => ::Thrift::Types::LIST, :name => 'success', :element => {:type => ::Thrift::Types::STRING}}
355
+ }
356
+
357
+ def struct_fields; FIELDS; end
358
+
359
+ def validate
360
+ end
361
+
362
+ ::Thrift::Struct.generate_accessors self
363
+ end
364
+
365
+ class My_pets_args
366
+ include ::Thrift::Struct, ::Thrift::Struct_Union
367
+
368
+ FIELDS = {
369
+
370
+ }
371
+
372
+ def struct_fields; FIELDS; end
373
+
374
+ def validate
375
+ end
376
+
377
+ ::Thrift::Struct.generate_accessors self
378
+ end
379
+
380
+ class My_pets_result
381
+ include ::Thrift::Struct, ::Thrift::Struct_Union
382
+ SUCCESS = 0
383
+
384
+ FIELDS = {
385
+ SUCCESS => {:type => ::Thrift::Types::SET, :name => 'success', :element => {:type => ::Thrift::Types::STRUCT, :class => ::Pet}}
386
+ }
387
+
388
+ def struct_fields; FIELDS; end
389
+
390
+ def validate
391
+ end
392
+
393
+ ::Thrift::Struct.generate_accessors self
394
+ end
395
+
396
+ class Age_args
397
+ include ::Thrift::Struct, ::Thrift::Struct_Union
398
+ AGE_MIN = 1
399
+ AGE_MAX = 2
400
+
401
+ FIELDS = {
402
+ AGE_MIN => {:type => ::Thrift::Types::I32, :name => 'age_min'},
403
+ AGE_MAX => {:type => ::Thrift::Types::I32, :name => 'age_max'}
404
+ }
405
+
406
+ def struct_fields; FIELDS; end
407
+
408
+ def validate
409
+ end
410
+
411
+ ::Thrift::Struct.generate_accessors self
412
+ end
413
+
414
+ class Age_result
415
+ include ::Thrift::Struct, ::Thrift::Struct_Union
416
+ SUCCESS = 0
417
+
418
+ FIELDS = {
419
+ SUCCESS => {:type => ::Thrift::Types::I32, :name => 'success'}
420
+ }
421
+
422
+ def struct_fields; FIELDS; end
423
+
424
+ def validate
425
+ end
426
+
427
+ ::Thrift::Struct.generate_accessors self
428
+ end
429
+
430
+ class Snooze_args
431
+ include ::Thrift::Struct, ::Thrift::Struct_Union
432
+ SLEEP_TIME = -1
433
+
434
+ FIELDS = {
435
+ SLEEP_TIME => {:type => ::Thrift::Types::I32, :name => 'sleep_time'}
436
+ }
437
+
438
+ def struct_fields; FIELDS; end
439
+
440
+ def validate
441
+ end
442
+
443
+ ::Thrift::Struct.generate_accessors self
444
+ end
445
+
446
+ class Snooze_result
447
+ include ::Thrift::Struct, ::Thrift::Struct_Union
448
+
449
+ FIELDS = {
450
+
451
+ }
452
+
453
+ def struct_fields; FIELDS; end
454
+
455
+ def validate
456
+ end
457
+
458
+ ::Thrift::Struct.generate_accessors self
459
+ end
460
+
247
461
  end
248
462
 
@@ -2,9 +2,18 @@ exception DivideByZeroException {
2
2
  1: string message
3
3
  }
4
4
 
5
+ struct Pet {
6
+ 1: string kind,
7
+ 2: string name
8
+ }
9
+
5
10
  service CalculatorService {
6
11
  string say_hello(1: string name),
7
12
  i32 add(1: i32 value1, 2: i32 value2),
8
13
  double divide(1: i32 dividend, 2: i32 divisor) throws (1: DivideByZeroException ex),
9
- void ping()
14
+ void ping(),
15
+ list<string> dwarves(),
16
+ set<Pet> my_pets(),
17
+ i32 age(1: i32 age_min, 2: i32 age_max),
18
+ void snooze(i32 sleep_time)
10
19
  }
@@ -1,5 +1,5 @@
1
1
  #
2
- # Autogenerated by Thrift Compiler (0.9.1)
2
+ # Autogenerated by Thrift Compiler (0.9.2)
3
3
  #
4
4
  # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
5
  #
@@ -1,5 +1,5 @@
1
1
  #
2
- # Autogenerated by Thrift Compiler (0.9.1)
2
+ # Autogenerated by Thrift Compiler (0.9.2)
3
3
  #
4
4
  # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
5
  #
@@ -27,3 +27,21 @@ class DivideByZeroException < ::Thrift::Exception
27
27
  ::Thrift::Struct.generate_accessors self
28
28
  end
29
29
 
30
+ class Pet
31
+ include ::Thrift::Struct, ::Thrift::Struct_Union
32
+ KIND = 1
33
+ NAME = 2
34
+
35
+ FIELDS = {
36
+ KIND => {:type => ::Thrift::Types::STRING, :name => 'kind'},
37
+ NAME => {:type => ::Thrift::Types::STRING, :name => 'name'}
38
+ }
39
+
40
+ def struct_fields; FIELDS; end
41
+
42
+ def validate
43
+ end
44
+
45
+ ::Thrift::Struct.generate_accessors self
46
+ end
47
+
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'thrifty_bunny'
3
+
4
+ $:.unshift File.dirname(__FILE__)
3
5
  require_relative 'calculator_server'
4
6
 
5
7
  server = Calculator::Server.new
@@ -30,6 +30,12 @@ module ThriftyBunny
30
30
  @reply_queue = @ch.queue('', exclusive: true)
31
31
  @is_opened = true
32
32
 
33
+ @timeout = config.timeout
34
+ @log = config.log
35
+ end
36
+
37
+ def log?
38
+ @log
33
39
  end
34
40
 
35
41
  def close
@@ -56,8 +62,6 @@ module ThriftyBunny
56
62
 
57
63
  operation = options[:operation] || ""
58
64
  blocking = options.has_key?(:blocking) ? options[:blocking] : true
59
- msg_timeout = options[:msg_timeout] || 10
60
- log_messages = options.has_key?(:log_messages) ? options[:log_messages] : true
61
65
 
62
66
  correlation_id = self.generate_uuid
63
67
 
@@ -69,12 +73,12 @@ module ThriftyBunny
69
73
  }
70
74
 
71
75
  #Publish the message
72
- print_log "Publishing message reply-to: #{@reply_queue.name} - headers: #{headers}", correlation_id if log_messages
76
+ print_log "Publishing message reply-to: #{@reply_queue.name} - headers: #{headers}", correlation_id if log?
73
77
  start_time = Time.now
74
78
  @service_exchange.publish(@outbuf,
75
79
  :routing_key => @service_queue_name,
76
80
  :correlation_id => correlation_id,
77
- :expiration => msg_timeout,
81
+ :expiration => @timeout,
78
82
  :reply_to => @reply_queue.name,
79
83
  :headers => headers)
80
84
 
@@ -84,10 +88,10 @@ module ThriftyBunny
84
88
  @response = ""
85
89
  begin
86
90
  #Adding 1sec to timeout to account for clock differences
87
- Timeout.timeout(msg_timeout + 1, ResponseTimeout) do
91
+ Timeout.timeout(@timeout + 1, ResponseTimeout) do
88
92
  @reply_queue.subscribe(:block => true) do |delivery_info, properties, payload|
89
93
 
90
- if log_messages
94
+ if log?
91
95
  response_time = Time.now - start_time
92
96
  print_log "---- Response Message received in #{response_time}sec for #{@reply_queue.name}", correlation_id
93
97
  print_log "HEADERS: #{properties}", correlation_id
@@ -104,7 +108,7 @@ module ThriftyBunny
104
108
  rescue ResponseTimeout => ex
105
109
  #Trying to work around weirdness being seen in a multi threaded workflow environment
106
110
  if @response == ""
107
- msg = "A timeout has occurred (#{msg_timeout}sec) trying to call #{@service_queue_name}.#{operation}"
111
+ msg = "A timeout has occurred (#{@timeout}sec) trying to call #{@service_queue_name}.#{operation}"
108
112
  print_log msg, correlation_id
109
113
  raise ex, msg
110
114
  else
@@ -2,7 +2,8 @@ require 'bunny'
2
2
 
3
3
  module ThriftyBunny
4
4
  class Config
5
- attr_reader :host, :port, :vhost, :user, :password, :ssl, :queue, :exchange
5
+ attr_reader :host, :port, :vhost, :user, :password, :ssl, :queue, :exchange,
6
+ :timeout, :log
6
7
 
7
8
  def initialize(options={})
8
9
  @host = options[:host] || '127.0.0.1'
@@ -15,6 +16,9 @@ module ThriftyBunny
15
16
 
16
17
  @queue = options[:queue] || 'rpc_queue'
17
18
  @exchange = options[:exchange] || 'rpc_exchange'
19
+
20
+ @timeout = options[:timeout] || 30 # seconds
21
+ @log = options[:log].nil? ? true : options[:log]
18
22
  end
19
23
 
20
24
  def bunny_config
@@ -25,4 +29,4 @@ module ThriftyBunny
25
29
  end
26
30
 
27
31
  end
28
- end
32
+ end
@@ -21,6 +21,12 @@ module ThriftyBunny
21
21
  @protocol_factory = options[:protocol_factory] || Thrift::BinaryProtocolFactory
22
22
  @exchange = config.exchange
23
23
 
24
+ @timeout = config.timeout
25
+ @log = config.log
26
+ end
27
+
28
+ def log?
29
+ @log
24
30
  end
25
31
 
26
32
  def close
@@ -35,9 +41,7 @@ module ThriftyBunny
35
41
  end
36
42
 
37
43
  def serve(options={})
38
- log_messages = options[:log_messages] || false
39
44
  max_messages = options[:max_messages].nil? ? 10 : options[:max_messages]
40
- response_timeout = options[:response_timeout] || 10
41
45
 
42
46
  #Create a channel to the service queue
43
47
  @request_channel = @conn.create_channel(nil, max_messages )
@@ -47,7 +51,7 @@ module ThriftyBunny
47
51
 
48
52
  @request_queue.subscribe(:block => true) do |delivery_info, properties, payload|
49
53
 
50
- if log_messages
54
+ if log?
51
55
  Thread.current["correlation_id"] = properties.correlation_id
52
56
  print_log "---- Message received ----"
53
57
  print_log "HEADERS: #{properties}"
@@ -59,12 +63,13 @@ module ThriftyBunny
59
63
  response_exchange = response_channel.default_exchange
60
64
 
61
65
  response_required = properties.headers.has_key?('response_required') ? properties.headers['response_required'] : true
62
- process_timeout = response_timeout.to_i > properties.expiration.to_i ? response_timeout.to_i : properties.expiration.to_i
66
+ process_timeout = @timeout > properties.expiration.to_i ? @timeout : properties.expiration.to_i
67
+ puts "!!!!!!!!!!! process_timeout: #{process_timeout}"
63
68
 
64
69
  #Binary content will imply thrift based message payload
65
70
  if properties.content_type == 'application/octet-stream'
66
71
 
67
- print_log "Request to process #{@queue_name}.#{properties.headers['operation']} in #{process_timeout}sec" if log_messages
72
+ print_log "Request to process #{@queue_name}.#{properties.headers['operation']} in #{process_timeout}sec" if log?
68
73
 
69
74
  input = StringIO.new payload
70
75
  out = StringIO.new
@@ -82,7 +87,7 @@ module ThriftyBunny
82
87
  if out.length > 0
83
88
  out.rewind
84
89
 
85
- print_log "Time to process request: #{processing_time}sec Response length: #{out.length}" if log_messages
90
+ print_log "Time to process request: #{processing_time}sec Response length: #{out.length}" if log?
86
91
 
87
92
  if response_required
88
93
  response_exchange.publish(out.read(out.length),
@@ -1,3 +1,3 @@
1
1
  module ThriftyBunny
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -9,7 +9,7 @@ describe 'client calls service to' do
9
9
  @pid = fork do
10
10
  Signal.trap("HUP") { puts "Stopping server as planned."; exit }
11
11
  puts "Starting server as planned."
12
- server = Calculator::Server.new
12
+ server = Calculator::Server.new(timeout: 5)
13
13
  server.serve
14
14
  end
15
15
  end
@@ -23,7 +23,8 @@ describe 'client calls service to' do
23
23
  end
24
24
 
25
25
  def start_client
26
- @transport = ThriftyBunny::ClientTransport.new
26
+ config = ThriftyBunny::Config.new(timeout: 10)
27
+ @transport = ThriftyBunny::ClientTransport.new(config)
27
28
  protocol = Thrift::BinaryProtocol.new(@transport)
28
29
  @client = CalculatorService::Client.new(protocol)
29
30
  end
@@ -58,4 +59,17 @@ describe 'client calls service to' do
58
59
  }.to raise_error(DivideByZeroException)
59
60
  end
60
61
 
62
+ it 'successfully calculates age' do
63
+ res = @client.age(10, 65)
64
+ puts res
65
+ expect(res).to eq(55)
66
+ end
67
+
68
+ it 'receives a response timeout' do
69
+ # snooze for one second longer than timeout
70
+ expect {
71
+ @client.snooze(11)
72
+ }.to raise_error(ThriftyBunny::ResponseTimeout)
73
+ end
74
+
61
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thrifty-bunny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bill Siggelkow
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-31 00:00:00.000000000 Z
11
+ date: 2015-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -130,6 +130,7 @@ extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
132
  - ".gitignore"
133
+ - ".pryrc"
133
134
  - Gemfile
134
135
  - LICENSE.txt
135
136
  - README.md