aliyun-mqs 0.0.3 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +2 -1
- data/README.md +160 -66
- data/Rakefile +6 -1
- data/aliyun-mqs.gemspec +7 -3
- data/bin/mqs +4 -0
- data/lib/aliyun/mqs.rb +39 -15
- data/lib/aliyun/mqs/cli.rb +54 -0
- data/lib/aliyun/mqs/message.rb +56 -0
- data/lib/aliyun/mqs/queue.rb +50 -60
- data/lib/aliyun/mqs/request.rb +88 -0
- data/lib/aliyun/mqs/version.rb +1 -1
- data/spec/lib/aliyun/mqs/message_spec.rb +63 -0
- data/spec/lib/aliyun/mqs/queue_spec.rb +179 -0
- data/spec/lib/aliyun/mqs/request_spec.rb +83 -0
- data/spec/spec_helper.rb +17 -0
- metadata +81 -11
- data/lib/aliyun/mqs/configuration.rb +0 -8
- data/lib/aliyun/mqs/http.rb +0 -58
- data/lib/aliyun/mqs/response.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a3074e0ab5236d6fd2250bf20a109efb4476f41
|
4
|
+
data.tar.gz: a84cbd6fb8bd7d4600a0c897b0c6443c660c094d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb742df399b32d36da37cf5daa1eed5c12fd43db3c9ef2e66dc5be4f1244e9229084a02f067d2db9122bc25cf0c1d3ae9b3bac0d447f76a58d45b87343c3dc3e
|
7
|
+
data.tar.gz: 70fe12855fac56cf0654d464aff3c6a98bf88d2242707b0034c35b836757c40081ff4207e7340410076fa9d4fdadaa2cf9fbec3c3c774bb9836bf0483f683d11
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# aliyun-mqs
|
2
|
+
[](https://travis-ci.org/skinnyworm/aliyun-mqs) [](https://codeclimate.com/github/skinnyworm/aliyun-mqs) [](https://codeclimate.com/github/skinnyworm/aliyun-mqs) [](http://badge.fury.io/rb/aliyun-mqs)
|
2
3
|
|
3
4
|
Talk to the mighty Aliyun MQS with charming ruby.
|
4
5
|
|
@@ -14,110 +15,207 @@ And then execute:
|
|
14
15
|
|
15
16
|
$ bundle
|
16
17
|
|
17
|
-
|
18
|
+
As this branch of the gem is not yet merged into the master branch, you can not install it via `gem install` for now.
|
18
19
|
|
19
|
-
|
20
|
+
## Configuration
|
20
21
|
|
21
|
-
|
22
|
+
### Command line configuration
|
22
23
|
|
23
|
-
|
24
|
+
The gem come with a command line tool `mqs`. It can help you easily manage the aliyun mqs within the terminal. In order to use this tool, you need to provide a configuration yaml file in your home directory which contains information about your mqs access_id and keys.
|
25
|
+
|
26
|
+
The configuration file should be stored at `~/.aliyun-mqs.yml`
|
24
27
|
|
25
|
-
```ruby
|
26
|
-
require 'aliyun/mqs'
|
27
28
|
```
|
28
29
|
|
29
|
-
|
30
|
+
access_id: 'lUxxxxxxxx'
|
31
|
+
key: 'VWxxxxxxxxxxxxxxxxxxxxx'
|
32
|
+
region: 'cn-hangzhou'
|
33
|
+
owner_id: 'ckxxxxxxxx'
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
### Rails configuration
|
30
39
|
|
31
|
-
|
40
|
+
If you are going to use this gem in a rails environment. You need to create a configuration file at `<RAILS_ROOT>/config/aliyun-mqs.yml`. In this way, you can use different set of queues for your development or production environments.
|
41
|
+
|
42
|
+
```
|
43
|
+
|
44
|
+
development:
|
45
|
+
access_id: 'lUxxxxxxxx'
|
46
|
+
key: 'VWxxxxxxxxxxxxxxxxxxxxx'
|
47
|
+
region: 'cn-hangzhou'
|
48
|
+
owner_id: 'ckxxxxxxxx'
|
49
|
+
|
50
|
+
production:
|
51
|
+
access_id: 'lUxxxxxxxx'
|
52
|
+
key: 'VWxxxxxxxxxxxxxxxxxxxxx'
|
53
|
+
region: 'cn-hangzhou'
|
54
|
+
owner_id: 'ckxxxxxxxx'
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
### Config in an application
|
59
|
+
|
60
|
+
At last you can also config the gem in place, by excute the following code before invoking and queue service.
|
32
61
|
|
33
62
|
```ruby
|
34
63
|
Aliyun::Mqs.configure do |config|
|
35
|
-
config.
|
36
|
-
config.
|
37
|
-
config.
|
38
|
-
config.
|
64
|
+
config.access_id = 'access-id'
|
65
|
+
config.key = "key"
|
66
|
+
config.region = 'region'
|
67
|
+
config.owner_id = 'owner-id'
|
39
68
|
end
|
40
69
|
```
|
41
70
|
|
42
|
-
### Response
|
43
71
|
|
44
|
-
|
45
|
-
|
72
|
+
## Commandline
|
73
|
+
|
74
|
+
This gem comes with a handy commandline tool `mqs` to help you manage your queue. Once the queue is installed. Execute 'mqs --help' to find out what commands are supported.
|
75
|
+
|
76
|
+
```
|
77
|
+
$ mqs --help
|
78
|
+
|
79
|
+
Commands:
|
80
|
+
mqs consume [queue] -wait <wait_seconds> # 从[queue]队列接受消息并删除
|
81
|
+
mqs create [queue] # 创建一个消息队列
|
82
|
+
mqs delete [queue] # 删除一个消息队列
|
83
|
+
mqs peek [queue] # 从[queue]队列中peek消息
|
84
|
+
mqs queues # 列出所有消息队列列表
|
85
|
+
mqs send [queue] [message] # 往[queue]队列发送[message]消息
|
86
|
+
```
|
87
|
+
|
88
|
+
Following are few examples.
|
89
|
+
|
90
|
+
#### 消息队列列表
|
91
|
+
|
92
|
+
```
|
93
|
+
$ mqs queues
|
94
|
+
消息队列列表
|
95
|
+
another
|
96
|
+
another1
|
97
|
+
another2
|
98
|
+
another3
|
99
|
+
another4
|
100
|
+
another5
|
101
|
+
```
|
102
|
+
|
103
|
+
#### 往队列发送消息
|
46
104
|
|
47
|
-
```
|
105
|
+
```
|
106
|
+
$ mqs send another "Test message"
|
107
|
+
发送消息到another队列
|
48
108
|
<?xml version="1.0"?>
|
49
109
|
<Message xmlns="http://mqs.aliyuncs.com/doc/v1">
|
50
|
-
<MessageBodyMD5>
|
51
|
-
<MessageId>
|
110
|
+
<MessageBodyMD5>82DFA5549EBC9AFC168EB7931EBECE5F</MessageBodyMD5>
|
111
|
+
<MessageId>55D5B01D1AE93D78-1-14979D45F33-200000001</MessageId>
|
52
112
|
</Message>
|
53
113
|
```
|
54
|
-
can be accessed as:
|
55
114
|
|
56
|
-
|
57
|
-
|
58
|
-
|
115
|
+
#### Peek队列中的消息
|
116
|
+
```
|
117
|
+
$ mqs peek another
|
118
|
+
Peek 队列another中的消息
|
119
|
+
=============================================
|
120
|
+
队列: another
|
121
|
+
ID: 55D5B01D1AE93D78-1-14979D45F33-200000001
|
122
|
+
MD5: 82DFA5549EBC9AFC168EB7931EBECE5F
|
123
|
+
Enqueue at: 2014-11-04 16:03:21 +0800
|
124
|
+
First enqueue at: 2014-11-04 16:03:21 +0800
|
125
|
+
Dequeue count: 0
|
126
|
+
Priority: 10
|
127
|
+
=============================================
|
128
|
+
Test message
|
59
129
|
```
|
60
130
|
|
61
|
-
To determine wheater a request call is succeed, use `Response::success?`. `Response` is an imutable object, all instance method with side-effects will return a new instance of `Response`. So, instead of
|
62
131
|
|
63
|
-
|
64
|
-
|
65
|
-
message
|
66
|
-
|
132
|
+
#### 消费队列中的消息
|
133
|
+
|
134
|
+
Be careful, consume command will first receive the message and then delete the message within a time period specfied by the queue.
|
135
|
+
|
136
|
+
```
|
137
|
+
$ mqs consume another
|
138
|
+
Consume 队列another中的消息
|
139
|
+
=============================================
|
140
|
+
队列: another
|
141
|
+
ID: 55D5B01D1AE93D78-1-14979D45F33-200000001
|
142
|
+
MD5: 82DFA5549EBC9AFC168EB7931EBECE5F
|
143
|
+
Receipt handle: 1-ODU4OTkzNDU5My0xNDE1MDg4MzU2LTEtMTA=
|
144
|
+
Enqueue at: 2014-11-04 16:03:21 +0800
|
145
|
+
First enqueue at: 2014-11-04 16:05:26 +0800
|
146
|
+
Next visible at: 2014-11-04 16:05:56 +0800
|
147
|
+
Dequeue count: 1
|
148
|
+
Priority: 10
|
149
|
+
=============================================
|
150
|
+
Test message
|
67
151
|
```
|
68
152
|
|
69
|
-
you shoud pass the return value (agian, an `Response`) around:
|
70
153
|
|
71
|
-
```ruby
|
72
|
-
resp = queue.receive
|
73
|
-
resp = resp.visibility = 1.minute
|
74
|
-
resp = resp.visibility = 8.minute
|
75
|
-
```
|
76
154
|
|
77
|
-
|
155
|
+
## Usage
|
156
|
+
|
157
|
+
Following are some example useage of the gem. You can read the specs to understand the full features of this gem
|
78
158
|
|
79
159
|
```ruby
|
80
|
-
|
81
|
-
|
82
|
-
```
|
160
|
+
#get a list of queue object
|
161
|
+
queues = Aliyun::Mqs::Queue.queues
|
83
162
|
|
84
|
-
|
163
|
+
#get all queues start with name 'query'
|
164
|
+
queues =Aliyun::Mqs::Queue.queues(query: "query")
|
85
165
|
|
86
|
-
|
87
|
-
|
88
|
-
```
|
166
|
+
#get all queues start with name 'query'
|
167
|
+
queues = Aliyun::Mqs::Queue.queues(size: 5)
|
89
168
|
|
90
|
-
|
169
|
+
#Obtain a queue object with name "aQueue"
|
170
|
+
queue = Aliyun::Mqs::Queue["aQueue"]
|
91
171
|
|
92
|
-
|
93
|
-
|
94
|
-
resp = queue.send "Hello, Cirno!", delay_seconds: 9, priority: 9
|
95
|
-
```
|
172
|
+
#Create a new queue
|
173
|
+
Aliyun::Mqs::Queue["aQueue"].create
|
96
174
|
|
97
|
-
|
175
|
+
#Create a new queue with polling wait 30 seconds
|
176
|
+
Aliyun::Mqs::Queue["aQueue"].create(:PollingWaitSeconds => 30)
|
98
177
|
|
99
|
-
|
100
|
-
|
101
|
-
message = queue.receive waitseconds: 10, peekonly: true
|
102
|
-
message = queue.peek
|
103
|
-
```
|
178
|
+
#Delete an existing queue
|
179
|
+
Aliyun::Mqs::Queue["aQueue"].delete
|
104
180
|
|
105
|
-
|
181
|
+
#Send a text message
|
182
|
+
Aliyun::Mqs::Queue["aQueue"].send_message "text message"
|
106
183
|
|
107
|
-
|
184
|
+
#Send a text message with priority option
|
185
|
+
Aliyun::Mqs::Queue["aQueue"].send_message "text message", :Priority=>1
|
186
|
+
|
187
|
+
#Receive a message
|
188
|
+
message = Aliyun::Mqs::Queue["aQueue"].receive_message
|
189
|
+
|
190
|
+
#Sample rspec for a message
|
191
|
+
expect(message).not_to be_nil
|
192
|
+
expect(message.id).to eq("5fea7756-0ea4-451a-a703-a558b933e274")
|
193
|
+
expect(message.body).to eq("This is a test message")
|
194
|
+
expect(message.body_md5).to eq("fafb00f5732ab283681e124bf8747ed1")
|
195
|
+
expect(message.receipt_handle).to eq("MbZj6wDWli+QEauMZc8ZRv37sIW2iJKq3M9Mx/KSbkJ0")
|
196
|
+
expect(message.enqueue_at).to eq(Time.at(1250700979248000/1000.0))
|
197
|
+
expect(message.first_enqueue_at).to eq(Time.at(1250700779318000/1000.0))
|
198
|
+
expect(message.next_visible_at).to eq(Time.at(1250700799348000/1000.0))
|
199
|
+
expect(message.dequeue_count).to eq(1)
|
200
|
+
expect(message.priority).to eq(8)
|
201
|
+
|
202
|
+
#Receive a message with option to override the default poll wait time of the queue.
|
203
|
+
message = Aliyun::Mqs::Queue["aQueue"].receive_message wait_seconds: 60
|
204
|
+
|
205
|
+
#Peek message in the queue
|
206
|
+
message = Aliyun::Mqs::Queue["aQueue"].peek_message
|
207
|
+
|
208
|
+
#Delete received message
|
209
|
+
message = Aliyun::Mqs::Queue["aQueue"].receive_message
|
108
210
|
message.delete
|
109
|
-
queue.delete message
|
110
|
-
queue.delete "#{you_message_recipet_handlerer}"
|
111
|
-
```
|
112
211
|
|
113
|
-
|
212
|
+
#Change message visibility
|
213
|
+
message = Aliyun::Mqs::Queue["aQueue"].receive_message
|
214
|
+
message.change_visibility 10
|
114
215
|
|
115
|
-
```ruby
|
116
|
-
resp = message.visibility = Time.now + 1.hour
|
117
|
-
resp = message.visibility = Time.now.to_f * 1000 + 10
|
118
|
-
resp = message.visibility += 1.hour
|
119
216
|
```
|
120
217
|
|
218
|
+
|
121
219
|
## Contributing
|
122
220
|
|
123
221
|
1. Fork it ( https://github.com/mgampkay/aliyun-mqs/fork )
|
@@ -125,7 +223,3 @@ resp = message.visibility += 1.hour
|
|
125
223
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
126
224
|
4. Push to the branch (`git push origin my-new-feature`)
|
127
225
|
5. Create a new Pull Request
|
128
|
-
|
129
|
-
## References
|
130
|
-
|
131
|
-
+ xiaohuilam's [aliyun-mqs-php-library](https://github.com/xiaohuilam/aliyun-mqs-php-library)
|
data/Rakefile
CHANGED
data/aliyun-mqs.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'aliyun/mqs/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'aliyun-mqs'
|
8
8
|
spec.version = Aliyun::Mqs::VERSION
|
9
|
-
spec.authors = ["mgampkay"]
|
10
|
-
spec.email = ["mgampkay@gmail.com"]
|
9
|
+
spec.authors = ["mgampkay","skinnyworm"]
|
10
|
+
spec.email = ["mgampkay@gmail.com", "askinnyworm@gmail.com"]
|
11
11
|
spec.summary = 'Ruby SDK for Aliyun MQS (non-official)'
|
12
12
|
spec.description = 'Non-official SDK for Aliyun MQS'
|
13
13
|
spec.homepage = 'https://github.com/mgampkay/aliyun-mqs'
|
@@ -20,7 +20,11 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_dependency 'nokogiri', '>= 1.6'
|
22
22
|
spec.add_dependency 'activesupport', '>= 4.1'
|
23
|
+
spec.add_dependency "rest-client"
|
24
|
+
spec.add_dependency "thor"
|
23
25
|
|
24
|
-
spec.add_development_dependency "bundler"
|
26
|
+
spec.add_development_dependency "bundler"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec"
|
29
|
+
spec.add_development_dependency "pry"
|
26
30
|
end
|
data/bin/mqs
ADDED
data/lib/aliyun/mqs.rb
CHANGED
@@ -1,26 +1,50 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'yaml'
|
1
5
|
require 'aliyun/mqs/version'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
# RestClient.log=STDOUT
|
7
|
+
|
8
|
+
# Monkey patch hash to support xml array and xml object
|
9
|
+
class Hash
|
10
|
+
def self.xml_array content, *path
|
11
|
+
o = xml_object(content, *path)
|
12
|
+
return (o.is_a?(Array) ? o : [o]).reject{|n| n.empty?}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.xml_object content, *path
|
16
|
+
h = from_xml(content)
|
17
|
+
path.reduce(h){|memo, node| memo = memo[node] || {}}
|
18
|
+
end
|
19
|
+
end
|
6
20
|
|
7
21
|
module Aliyun
|
8
22
|
module Mqs
|
23
|
+
require 'aliyun/mqs/queue'
|
24
|
+
require 'aliyun/mqs/message'
|
25
|
+
require 'aliyun/mqs/request'
|
26
|
+
require 'aliyun/mqs/cli'
|
9
27
|
|
10
28
|
class << self
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
29
|
+
def configuration
|
30
|
+
@configuration ||= begin
|
31
|
+
if defined? Rails
|
32
|
+
config_file = Rails.root.join("config/aliyun-mqs.yml")
|
33
|
+
else
|
34
|
+
config_file = File.expand_path("~/.aliyun-mqs.yml")
|
35
|
+
end
|
17
36
|
|
18
|
-
|
19
|
-
|
20
|
-
|
37
|
+
if (File.exist?(config_file))
|
38
|
+
config = YAML.load(ERB.new(File.new(config_file).read).result)
|
39
|
+
config = config[Rails.env] if defined? Rails
|
40
|
+
end
|
41
|
+
OpenStruct.new(config || {access_id:"", key:"", region:"", owner_id:""})
|
42
|
+
end
|
43
|
+
end
|
21
44
|
|
22
|
-
|
23
|
-
|
45
|
+
def configure
|
46
|
+
yield(configuration)
|
47
|
+
end
|
24
48
|
end
|
25
49
|
|
26
50
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
module Aliyun::Mqs
|
4
|
+
class Cli < Thor
|
5
|
+
|
6
|
+
desc "queues", "列出 QueueOwnerId 下的消息队列列表"
|
7
|
+
def queues()
|
8
|
+
execute("消息队列列表"){ Queue.queues }
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "delete [queue]", "删除一个消息队列"
|
12
|
+
def delete(name)
|
13
|
+
execute("删除消息队列'#{name}'"){ Queue[name].delete }
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "create [queue]", "创建一个消息队列"
|
17
|
+
def create(name)
|
18
|
+
execute("创建消息队列'#{name}'"){ Queue[name].create }
|
19
|
+
end
|
20
|
+
|
21
|
+
desc "consume [queue] -wait <wait_seconds>", "从[queue]队列接受消息并删除"
|
22
|
+
option :wait
|
23
|
+
def consume(name)
|
24
|
+
execute("Consume 队列#{name}中的消息") do
|
25
|
+
message = Queue[name].receive_message(wait_seconds: options[:wait])
|
26
|
+
message.delete
|
27
|
+
message
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
desc "send [queue] [message]", "往[queue]队列发送[message]消息"
|
32
|
+
def send(name, content)
|
33
|
+
execute("发送消息到#{name}队列"){ Queue[name].send_message content }
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "peek [queue]", "从[queue]队列中peek消息"
|
37
|
+
def peek(name)
|
38
|
+
execute("Peek 队列#{name}中的消息"){ Queue[name].peek_message }
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def execute info=nil
|
43
|
+
begin
|
44
|
+
puts info
|
45
|
+
result = yield()
|
46
|
+
puts result
|
47
|
+
rescue RequestException => ex
|
48
|
+
puts "#{ex['Code']}: #{ex['Message']}"
|
49
|
+
end
|
50
|
+
puts "\n"
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|