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 +4 -4
- data/README.md +10 -2
- data/lib/fleck/client/request.rb +6 -0
- data/lib/fleck/client.rb +20 -5
- data/lib/fleck/version.rb +1 -1
- data/lib/fleck.rb +0 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3844f5f141564257d45c59386c82683d9adc62ec
|
4
|
+
data.tar.gz: d5fec104b21c27ebd65b439a66ac702617324d93
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
data/lib/fleck/client/request.rb
CHANGED
@@ -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
|
36
|
-
request = Fleck::Client::Request.new(@exchange,
|
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
|
-
|
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.
|
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
data/lib/fleck.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|