fleck 0.1.2 → 0.2.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: 459b048a5d3ec2ee5c6035829059fcb394978d67
4
- data.tar.gz: 675b6149f083da223220ede2da5df5201200a5a8
3
+ metadata.gz: 3844f5f141564257d45c59386c82683d9adc62ec
4
+ data.tar.gz: d5fec104b21c27ebd65b439a66ac702617324d93
5
5
  SHA512:
6
- metadata.gz: 0aad519394a954fa98c57b1434a405ebba4fb449b87b081f2c8823d0d2ac7fad6e60ea632159e52d7451cfdeb22374d38b16a4f4ca6a925ebff8f9c5b3448963
7
- data.tar.gz: 892197f7f42232fc10695ef13060fee5ccc0c9233731590ef278172953f2df7245adac06208073397eece063509648fecc441c9e2bee4af9f13c17cc28c64330
6
+ metadata.gz: bd36645f66fea70b5347acbb86673eee29ac8a3e89e02ad43dcc0c9d5d585efbe38f5d3ebb175ae5f4075aa94779387c4e17dfc8839f0e6029642aa8a95b10c7
7
+ data.tar.gz: e107b55e67021305429092c26c69d62f5c7af55f011763ab5dbf7950d80bb627d8d826fe2303218e5c543879f81aefc1ce75eac313f7f46b701f006a6104aef2
data/README.md CHANGED
@@ -55,7 +55,7 @@ ASYNC = false # a flag to indicate if the request is asy
55
55
 
56
56
  connection = Fleck.connection(host: '127.0.0.1', port: 5672, user: 'guest', pass: 'guest', vhost: '/')
57
57
  client = Fleck::Client.new(connection, QUEUE)
58
- response = client.request(HEADERS, PARAMS, ASYNC)
58
+ response = client.request(headers: HEADERS, params: PARAMS, async: ASYNC)
59
59
 
60
60
  response.status # => returns the status code of the response
61
61
  response.headers # => returns the headers Hash of the response
@@ -63,13 +63,20 @@ response.body # => returns the body of the response
63
63
  response.errors # => returns the Array of errors
64
64
  ```
65
65
 
66
+ All the options of the requests are optional. The available options for request are:
67
+ - `headers:` - (default: `{}`) - allows to set headers for the request
68
+ - `params` - (default: `{}`) - allows to set the parameters of the request
69
+ - `async` - (default: `false`) - indicates if the request should be executed asynchronously
70
+ - `timeout` - (default: `nil`) - when set, indicates the request timeout in seconds after which the request will be canceled
71
+ - `queue` - (default: `<client queue>`) - allows to specify a different queue where to enqueue the request
72
+
66
73
  #### Request with block
67
74
 
68
75
  You might want to process the response of asynchronous requests when the response is ready. In that case you could pass a block to the request,
69
76
  so that the block is called when the response is completed:
70
77
 
71
78
  ```ruby
72
- client.request({}, {param1: 'myparam'}, true) do |request, response|
79
+ client.request(headers: {}, params: {param1: 'myparam'}, async: true) do |request, response|
73
80
  if response.status == 200
74
81
  puts "#{response.status} #{response.body}"
75
82
  else
@@ -78,6 +85,7 @@ client.request({}, {param1: 'myparam'}, true) do |request, response|
78
85
  end
79
86
  ```
80
87
 
88
+
81
89
  ### Fleck::Consumer
82
90
 
83
91
  To use `Fleck::Consumer` all you need is to inherit it by an another class:
@@ -51,5 +51,11 @@ module Fleck
51
51
  @ended_at = Time.now.to_f
52
52
  logger.debug "Done in #{((@ended_at - @started_at).round(5) * 1000).round(2)} ms"
53
53
  end
54
+
55
+ def cancel!
56
+ logger.warn "Request canceled!"
57
+ self.response = Fleck::Client::Response.new(Oj.dump({status: 503, errors: ['Service Unavailable'], body: nil} , mode: :compat))
58
+ complete!
59
+ end
54
60
  end
55
61
  end
data/lib/fleck/client.rb CHANGED
@@ -11,7 +11,7 @@ module Fleck
11
11
  @reply_queue = @channel.queue("", exclusive: true)
12
12
  @requests = ThreadSafe::Hash.new
13
13
 
14
- @reply_queue.subscribe do |delivery_info, metadata, payload|
14
+ @subscription = @reply_queue.subscribe do |delivery_info, metadata, payload|
15
15
  begin
16
16
  logger.debug "Response received: #{payload}"
17
17
  request = @requests[metadata[:correlation_id]]
@@ -32,22 +32,37 @@ module Fleck
32
32
  end
33
33
  end
34
34
 
35
- def request(headers = {}, payload = {}, async = false, &block)
36
- request = Fleck::Client::Request.new(@exchange, @queue_name, @reply_queue.name, headers, payload, &block)
35
+ def request(headers: {}, params: {}, async: false, timeout: nil, queue: @queue_name, &block)
36
+ request = Fleck::Client::Request.new(@exchange, queue, @reply_queue.name, headers, params, &block)
37
37
  @requests[request.id] = request
38
- request.send!(async)
38
+ if timeout && !async
39
+ begin
40
+ Timeout.timeout(timeout.to_f) do
41
+ request.send!(false)
42
+ end
43
+ rescue Timeout::Error => e
44
+ logger.warn "Failed to get any response in #{timeout} seconds for request #{request.id.to_s.color(:red)}! The request will be canceled."
45
+ request.cancel!
46
+ @requests.delete request.id
47
+ end
48
+ else
49
+ request.send!(async)
50
+ end
39
51
 
40
52
  return request.response
41
53
  end
42
54
 
43
55
  def terminate
56
+ logger.info "Unsubscribing from #{@reply_queue.name}"
44
57
  @requests.each do |id, request|
45
58
  begin
46
- request.complete!
59
+ request.cancel!
47
60
  rescue => e
48
61
  logger.error e.inspect + "\n" + e.backtrace.join("\n")
49
62
  end
50
63
  end
64
+ @requests.clear
65
+ @subscription.cancel
51
66
  end
52
67
  end
53
68
  end
data/lib/fleck/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fleck
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/fleck.rb CHANGED
@@ -17,10 +17,6 @@ module Fleck
17
17
  @consumers = ThreadSafe::Array.new
18
18
  @connections = ThreadSafe::Hash.new
19
19
 
20
- at_exit do
21
- Fleck.terminate
22
- end
23
-
24
20
  def self.configure
25
21
  yield @config if block_given?
26
22
  @config
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fleck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Groza Sergiu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-17 00:00:00.000000000 Z
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler