vx-common-amqp 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +276 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/bin/vx-consumers +12 -0
- data/lib/vx/common/amqp/cli.rb +88 -0
- data/lib/vx/common/amqp/config.rb +74 -0
- data/lib/vx/common/amqp/consumer/ack.rb +19 -0
- data/lib/vx/common/amqp/consumer/configuration.rb +119 -0
- data/lib/vx/common/amqp/consumer/publish.rb +32 -0
- data/lib/vx/common/amqp/consumer/subscribe.rb +67 -0
- data/lib/vx/common/amqp/consumer.rb +70 -0
- data/lib/vx/common/amqp/formatter.rb +105 -0
- data/lib/vx/common/amqp/mixins/callbacks.rb +35 -0
- data/lib/vx/common/amqp/mixins/logger.rb +17 -0
- data/lib/vx/common/amqp/session.rb +154 -0
- data/lib/vx/common/amqp/supervisor/threaded.rb +171 -0
- data/lib/vx/common/amqp/testing.rb +54 -0
- data/lib/vx/common/amqp/version.rb +7 -0
- data/lib/vx/common/amqp.rb +68 -0
- data/spec/integration/multi_threaded_spec.rb +89 -0
- data/spec/integration/threaded_supervisor_spec.rb +85 -0
- data/spec/lib/amqp/config_spec.rb +32 -0
- data/spec/lib/amqp/consumer_spec.rb +316 -0
- data/spec/lib/amqp/formatter_spec.rb +47 -0
- data/spec/lib/amqp/mixins/callbacks_spec.rb +26 -0
- data/spec/lib/amqp/session_spec.rb +144 -0
- data/spec/lib/amqp/supervisor/threaded_spec.rb +124 -0
- data/spec/lib/amqp_spec.rb +9 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/support/amqp.rb +15 -0
- data/spec/support/ignore_me_error.rb +1 -0
- data/vx-common-amqp.gemspec +30 -0
- metadata +178 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d22e0e16e443910a96c0200d9c7e01eec6f9c5b5
|
4
|
+
data.tar.gz: b78374356e36e00925cff0f076673e7adea1f815
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 13c65d9bbbd886758e22d4b712a0792c9386ffb9ecfd72c48c00926f8213efabbb03915b03fa590dac6e7e81c2338ddbd8d16d32a8eb9cd2d11e8e44fb976f22
|
7
|
+
data.tar.gz: 046cab0abc468287b048036bd6491800a4cf3e1f2473a8645e5e9ed18fe139c22cfb1da683b496a03446d234b7f1636ed767f9774dc736eb317bf476dc6d8933
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,276 @@
|
|
1
|
+
Copyright (c) 2013 Dmitry Galinsky
|
2
|
+
|
3
|
+
The GNU General Public License, Version 2, June 1991 (GPLv2)
|
4
|
+
============================================================
|
5
|
+
|
6
|
+
> Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
7
|
+
> 59 Temple Place, Suite 330
|
8
|
+
> Boston, MA 02111-1307 USA
|
9
|
+
|
10
|
+
Everyone is permitted to copy and distribute verbatim copies of this license
|
11
|
+
document, but changing it is not allowed.
|
12
|
+
|
13
|
+
|
14
|
+
Preamble
|
15
|
+
--------
|
16
|
+
|
17
|
+
The licenses for most software are designed to take away your freedom to share
|
18
|
+
and change it. By contrast, the GNU General Public License is intended to
|
19
|
+
guarantee your freedom to share and change free software--to make sure the
|
20
|
+
software is free for all its users. This General Public License applies to most
|
21
|
+
of the Free Software Foundation's software and to any other program whose
|
22
|
+
authors commit to using it. (Some other Free Software Foundation software is
|
23
|
+
covered by the GNU Library General Public License instead.) You can apply it to
|
24
|
+
your programs, too.
|
25
|
+
|
26
|
+
When we speak of free software, we are referring to freedom, not price. Our
|
27
|
+
General Public Licenses are designed to make sure that you have the freedom to
|
28
|
+
distribute copies of free software (and charge for this service if you wish),
|
29
|
+
that you receive source code or can get it if you want it, that you can change
|
30
|
+
the software or use pieces of it in new free programs; and that you know you can
|
31
|
+
do these things.
|
32
|
+
|
33
|
+
To protect your rights, we need to make restrictions that forbid anyone to deny
|
34
|
+
you these rights or to ask you to surrender the rights. These restrictions
|
35
|
+
translate to certain responsibilities for you if you distribute copies of the
|
36
|
+
software, or if you modify it.
|
37
|
+
|
38
|
+
For example, if you distribute copies of such a program, whether gratis or for a
|
39
|
+
fee, you must give the recipients all the rights that you have. You must make
|
40
|
+
sure that they, too, receive or can get the source code. And you must show them
|
41
|
+
these terms so they know their rights.
|
42
|
+
|
43
|
+
We protect your rights with two steps: (1) copyright the software, and (2) offer
|
44
|
+
you this license which gives you legal permission to copy, distribute and/or
|
45
|
+
modify the software.
|
46
|
+
|
47
|
+
Also, for each author's protection and ours, we want to make certain that
|
48
|
+
everyone understands that there is no warranty for this free software. If the
|
49
|
+
software is modified by someone else and passed on, we want its recipients to
|
50
|
+
know that what they have is not the original, so that any problems introduced by
|
51
|
+
others will not reflect on the original authors' reputations.
|
52
|
+
|
53
|
+
Finally, any free program is threatened constantly by software patents. We wish
|
54
|
+
to avoid the danger that redistributors of a free program will individually
|
55
|
+
obtain patent licenses, in effect making the program proprietary. To prevent
|
56
|
+
this, we have made it clear that any patent must be licensed for everyone's free
|
57
|
+
use or not licensed at all.
|
58
|
+
|
59
|
+
The precise terms and conditions for copying, distribution and modification
|
60
|
+
follow.
|
61
|
+
|
62
|
+
|
63
|
+
Terms And Conditions For Copying, Distribution And Modification
|
64
|
+
---------------------------------------------------------------
|
65
|
+
0. This License applies to any program or other work which contains a notice
|
66
|
+
placed by the copyright holder saying it may be distributed under the terms
|
67
|
+
of this General Public License. The "Program", below, refers to any such
|
68
|
+
program or work, and a "work based on the Program" means either the Program
|
69
|
+
or any derivative work under copyright law: that is to say, a work
|
70
|
+
containing the Program or a portion of it, either verbatim or with
|
71
|
+
modifications and/or translated into another language. (Hereinafter,
|
72
|
+
translation is included without limitation in the term "modification".)
|
73
|
+
Each licensee is addressed as "you".
|
74
|
+
|
75
|
+
Activities other than copying, distribution and modification are not
|
76
|
+
covered by this License; they are outside its scope. The act of running the
|
77
|
+
Program is not restricted, and the output from the Program is covered only
|
78
|
+
if its contents constitute a work based on the Program (independent of
|
79
|
+
having been made by running the Program). Whether that is true depends on
|
80
|
+
what the Program does.
|
81
|
+
|
82
|
+
1. You may copy and distribute verbatim copies of the Program's source code as
|
83
|
+
you receive it, in any medium, provided that you conspicuously and
|
84
|
+
appropriately publish on each copy an appropriate copyright notice and
|
85
|
+
disclaimer of warranty; keep intact all the notices that refer to this
|
86
|
+
License and to the absence of any warranty; and give any other recipients
|
87
|
+
of the Program a copy of this License along with the Program.
|
88
|
+
|
89
|
+
You may charge a fee for the physical act of transferring a copy, and you
|
90
|
+
may at your option offer warranty protection in exchange for a fee.
|
91
|
+
|
92
|
+
2. You may modify your copy or copies of the Program or any portion of it,
|
93
|
+
thus forming a work based on the Program, and copy and distribute such
|
94
|
+
modifications or work under the terms of Section 1 above, provided that you
|
95
|
+
also meet all of these conditions:
|
96
|
+
|
97
|
+
a) You must cause the modified files to carry prominent notices stating
|
98
|
+
that you changed the files and the date of any change.
|
99
|
+
|
100
|
+
b) You must cause any work that you distribute or publish, that in whole
|
101
|
+
or in part contains or is derived from the Program or any part thereof,
|
102
|
+
to be licensed as a whole at no charge to all third parties under the
|
103
|
+
terms of this License.
|
104
|
+
|
105
|
+
c) If the modified program normally reads commands interactively when run,
|
106
|
+
you must cause it, when started running for such interactive use in the
|
107
|
+
most ordinary way, to print or display an announcement including an
|
108
|
+
appropriate copyright notice and a notice that there is no warranty (or
|
109
|
+
else, saying that you provide a warranty) and that users may
|
110
|
+
redistribute the program under these conditions, and telling the user
|
111
|
+
how to view a copy of this License. (Exception: if the Program itself
|
112
|
+
is interactive but does not normally print such an announcement, your
|
113
|
+
work based on the Program is not required to print an announcement.)
|
114
|
+
|
115
|
+
These requirements apply to the modified work as a whole. If identifiable
|
116
|
+
sections of that work are not derived from the Program, and can be
|
117
|
+
reasonably considered independent and separate works in themselves, then
|
118
|
+
this License, and its terms, do not apply to those sections when you
|
119
|
+
distribute them as separate works. But when you distribute the same
|
120
|
+
sections as part of a whole which is a work based on the Program, the
|
121
|
+
distribution of the whole must be on the terms of this License, whose
|
122
|
+
permissions for other licensees extend to the entire whole, and thus to
|
123
|
+
each and every part regardless of who wrote it.
|
124
|
+
|
125
|
+
Thus, it is not the intent of this section to claim rights or contest your
|
126
|
+
rights to work written entirely by you; rather, the intent is to exercise
|
127
|
+
the right to control the distribution of derivative or collective works
|
128
|
+
based on the Program.
|
129
|
+
|
130
|
+
In addition, mere aggregation of another work not based on the Program with
|
131
|
+
the Program (or with a work based on the Program) on a volume of a storage
|
132
|
+
or distribution medium does not bring the other work under the scope of
|
133
|
+
this License.
|
134
|
+
|
135
|
+
3. You may copy and distribute the Program (or a work based on it, under
|
136
|
+
Section 2) in object code or executable form under the terms of Sections 1
|
137
|
+
and 2 above provided that you also do one of the following:
|
138
|
+
|
139
|
+
a) Accompany it with the complete corresponding machine-readable source
|
140
|
+
code, which must be distributed under the terms of Sections 1 and 2
|
141
|
+
above on a medium customarily used for software interchange; or,
|
142
|
+
|
143
|
+
b) Accompany it with a written offer, valid for at least three years, to
|
144
|
+
give any third party, for a charge no more than your cost of physically
|
145
|
+
performing source distribution, a complete machine-readable copy of the
|
146
|
+
corresponding source code, to be distributed under the terms of
|
147
|
+
Sections 1 and 2 above on a medium customarily used for software
|
148
|
+
interchange; or,
|
149
|
+
|
150
|
+
c) Accompany it with the information you received as to the offer to
|
151
|
+
distribute corresponding source code. (This alternative is allowed only
|
152
|
+
for noncommercial distribution and only if you received the program in
|
153
|
+
object code or executable form with such an offer, in accord with
|
154
|
+
Subsection b above.)
|
155
|
+
|
156
|
+
The source code for a work means the preferred form of the work for making
|
157
|
+
modifications to it. For an executable work, complete source code means all
|
158
|
+
the source code for all modules it contains, plus any associated interface
|
159
|
+
definition files, plus the scripts used to control compilation and
|
160
|
+
installation of the executable. However, as a special exception, the source
|
161
|
+
code distributed need not include anything that is normally distributed (in
|
162
|
+
either source or binary form) with the major components (compiler, kernel,
|
163
|
+
and so on) of the operating system on which the executable runs, unless
|
164
|
+
that component itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering access to
|
167
|
+
copy from a designated place, then offering equivalent access to copy the
|
168
|
+
source code from the same place counts as distribution of the source code,
|
169
|
+
even though third parties are not compelled to copy the source along with
|
170
|
+
the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program except as
|
173
|
+
expressly provided under this License. Any attempt otherwise to copy,
|
174
|
+
modify, sublicense or distribute the Program is void, and will
|
175
|
+
automatically terminate your rights under this License. However, parties
|
176
|
+
who have received copies, or rights, from you under this License will not
|
177
|
+
have their licenses terminated so long as such parties remain in full
|
178
|
+
compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not signed it.
|
181
|
+
However, nothing else grants you permission to modify or distribute the
|
182
|
+
Program or its derivative works. These actions are prohibited by law if you
|
183
|
+
do not accept this License. Therefore, by modifying or distributing the
|
184
|
+
Program (or any work based on the Program), you indicate your acceptance of
|
185
|
+
this License to do so, and all its terms and conditions for copying,
|
186
|
+
distributing or modifying the Program or works based on it.
|
187
|
+
|
188
|
+
6. Each time you redistribute the Program (or any work based on the Program),
|
189
|
+
the recipient automatically receives a license from the original licensor
|
190
|
+
to copy, distribute or modify the Program subject to these terms and
|
191
|
+
conditions. You may not impose any further restrictions on the recipients'
|
192
|
+
exercise of the rights granted herein. You are not responsible for
|
193
|
+
enforcing compliance by third parties to this License.
|
194
|
+
|
195
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
196
|
+
infringement or for any other reason (not limited to patent issues),
|
197
|
+
conditions are imposed on you (whether by court order, agreement or
|
198
|
+
otherwise) that contradict the conditions of this License, they do not
|
199
|
+
excuse you from the conditions of this License. If you cannot distribute so
|
200
|
+
as to satisfy simultaneously your obligations under this License and any
|
201
|
+
other pertinent obligations, then as a consequence you may not distribute
|
202
|
+
the Program at all. For example, if a patent license would not permit
|
203
|
+
royalty-free redistribution of the Program by all those who receive copies
|
204
|
+
directly or indirectly through you, then the only way you could satisfy
|
205
|
+
both it and this License would be to refrain entirely from distribution of
|
206
|
+
the Program.
|
207
|
+
|
208
|
+
If any portion of this section is held invalid or unenforceable under any
|
209
|
+
particular circumstance, the balance of the section is intended to apply
|
210
|
+
and the section as a whole is intended to apply in other circumstances.
|
211
|
+
|
212
|
+
It is not the purpose of this section to induce you to infringe any patents
|
213
|
+
or other property right claims or to contest validity of any such claims;
|
214
|
+
this section has the sole purpose of protecting the integrity of the free
|
215
|
+
software distribution system, which is implemented by public license
|
216
|
+
practices. Many people have made generous contributions to the wide range
|
217
|
+
of software distributed through that system in reliance on consistent
|
218
|
+
application of that system; it is up to the author/donor to decide if he or
|
219
|
+
she is willing to distribute software through any other system and a
|
220
|
+
licensee cannot impose that choice.
|
221
|
+
|
222
|
+
This section is intended to make thoroughly clear what is believed to be a
|
223
|
+
consequence of the rest of this License.
|
224
|
+
|
225
|
+
8. If the distribution and/or use of the Program is restricted in certain
|
226
|
+
countries either by patents or by copyrighted interfaces, the original
|
227
|
+
copyright holder who places the Program under this License may add an
|
228
|
+
explicit geographical distribution limitation excluding those countries, so
|
229
|
+
that distribution is permitted only in or among countries not thus excluded.
|
230
|
+
In such case, this License incorporates the limitation as if written in
|
231
|
+
the body of this License.
|
232
|
+
|
233
|
+
9. The Free Software Foundation may publish revised and/or new versions of the
|
234
|
+
General Public License from time to time. Such new versions will be similar
|
235
|
+
in spirit to the present version, but may differ in detail to address new
|
236
|
+
problems or concerns.
|
237
|
+
|
238
|
+
Each version is given a distinguishing version number. If the Program
|
239
|
+
specifies a version number of this License which applies to it and "any
|
240
|
+
later version", you have the option of following the terms and conditions
|
241
|
+
either of that version or of any later version published by the Free
|
242
|
+
Software Foundation. If the Program does not specify a version number of
|
243
|
+
this License, you may choose any version ever published by the Free
|
244
|
+
Software Foundation.
|
245
|
+
|
246
|
+
10. If you wish to incorporate parts of the Program into other free programs
|
247
|
+
whose distribution conditions are different, write to the author to ask for
|
248
|
+
permission. For software which is copyrighted by the Free Software
|
249
|
+
Foundation, write to the Free Software Foundation; we sometimes make
|
250
|
+
exceptions for this. Our decision will be guided by the two goals of
|
251
|
+
preserving the free status of all derivatives of our free software and of
|
252
|
+
promoting the sharing and reuse of software generally.
|
253
|
+
|
254
|
+
|
255
|
+
No Warranty
|
256
|
+
-----------
|
257
|
+
|
258
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
|
259
|
+
THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
260
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
261
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
262
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
263
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
|
264
|
+
THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM
|
265
|
+
PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
|
266
|
+
CORRECTION.
|
267
|
+
|
268
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
|
269
|
+
ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
|
270
|
+
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
271
|
+
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
272
|
+
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
273
|
+
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
274
|
+
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
275
|
+
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
276
|
+
SUCH DAMAGES.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Vx::Common::Amqp
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'vx-common-amqp'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install vx-common-amqp
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/vx-consumers
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
trap "INT" do
|
5
|
+
Vx::Common::AMQP.shutdown
|
6
|
+
end
|
7
|
+
|
8
|
+
module Vx
|
9
|
+
module Common
|
10
|
+
module AMQP
|
11
|
+
class CLI
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@options = parse_options
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
require_before_executing
|
19
|
+
load_consumers
|
20
|
+
|
21
|
+
consumers = find_consumers.inject({}) do |a,c|
|
22
|
+
a[c] = 2
|
23
|
+
a
|
24
|
+
end
|
25
|
+
|
26
|
+
supervisor = Common::AMQP::Supervisor::Threaded.new
|
27
|
+
consumers.each do |k,v|
|
28
|
+
v.times do |n|
|
29
|
+
supervisor.add k, :subscribe, n
|
30
|
+
end
|
31
|
+
end
|
32
|
+
supervisor.run
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def find_consumers
|
38
|
+
Vx::Common::AMQP::Consumer.classes.inject([]) do |a,c|
|
39
|
+
c = Kernel.const_get(c)
|
40
|
+
a << c if c.method_defined? :perform
|
41
|
+
a
|
42
|
+
end.uniq
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_consumers
|
46
|
+
if l = @options[:load_from]
|
47
|
+
path = File.expand_path(l, Dir.pwd)
|
48
|
+
Dir["#{path}/**.rb"].each do |f|
|
49
|
+
load f
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def require_before_executing
|
55
|
+
if r = @options[:require]
|
56
|
+
require File.expand_path(r, Dir.pwd)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def parse_options
|
61
|
+
options = {}
|
62
|
+
|
63
|
+
OptionParser.new do |opts|
|
64
|
+
opts.banner = "Usage: amqp_consumers [options]"
|
65
|
+
|
66
|
+
opts.on("-r", "--require FILE", String, 'Require file before executing') do |r|
|
67
|
+
options[:require] = r
|
68
|
+
end
|
69
|
+
|
70
|
+
opts.on("-l", "--load DIR", "Load consumers from directory") do |l|
|
71
|
+
options[:load_from] = l
|
72
|
+
end
|
73
|
+
|
74
|
+
opts.on("-c", "--consumer CONSUMER[,NUM]", "a", "b") do |l|
|
75
|
+
name,num = l.split(",")
|
76
|
+
num ||= 1
|
77
|
+
options[:consumers] ||= []
|
78
|
+
options[:consumers] << [name, num.to_i]
|
79
|
+
end
|
80
|
+
end.parse!
|
81
|
+
|
82
|
+
options
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'vx/common/rack/builder'
|
3
|
+
|
4
|
+
module Vx
|
5
|
+
module Common
|
6
|
+
module AMQP
|
7
|
+
class Config
|
8
|
+
|
9
|
+
attr_accessor :url, :default_exchange_options, :default_queue_options,
|
10
|
+
:default_publish_options, :default_exchange_type, :logger, :pool_timeout,
|
11
|
+
:heartbeat, :spawn_attempts, :content_type, :callbacks
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
reset!
|
15
|
+
end
|
16
|
+
|
17
|
+
def formatter
|
18
|
+
Common::AMQP::Formatter
|
19
|
+
end
|
20
|
+
|
21
|
+
%w{ before after }.each do |p|
|
22
|
+
%w{ subscribe publish recieve }.each do |m|
|
23
|
+
define_method "#{p}_#{m}" do |&callback|
|
24
|
+
callbacks["#{p}_#{m}".to_sym] = callback
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def on_error(&block)
|
30
|
+
callbacks[:on_error] = block
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_exchange_name
|
34
|
+
"amq.#{default_exchange_type}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def reset!
|
38
|
+
@url = nil
|
39
|
+
@logger = ::Logger.new(STDOUT)
|
40
|
+
@default_exchange_type = :topic
|
41
|
+
@pool_timeout = 0.1
|
42
|
+
@heartbeat = 10
|
43
|
+
|
44
|
+
@publishing_builder = nil
|
45
|
+
@recieving_builder = nil
|
46
|
+
@subscribing_builder = nil
|
47
|
+
|
48
|
+
@spawn_attempts = 5
|
49
|
+
|
50
|
+
@content_type = 'application/json'
|
51
|
+
|
52
|
+
@callbacks = {}
|
53
|
+
|
54
|
+
@default_exchange_options = {
|
55
|
+
durable: true,
|
56
|
+
auto_delete: false
|
57
|
+
}
|
58
|
+
|
59
|
+
@default_queue_options = {
|
60
|
+
durable: true,
|
61
|
+
autodelete: false,
|
62
|
+
exclusive: false
|
63
|
+
}
|
64
|
+
|
65
|
+
@default_publish_options = {
|
66
|
+
durable: false
|
67
|
+
}
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Vx
|
2
|
+
module Common
|
3
|
+
module AMQP
|
4
|
+
module Consumer::Ack
|
5
|
+
|
6
|
+
def ack!(multiple = false)
|
7
|
+
self.class.session.channel.ack delivery_info.delivery_tag, multiple
|
8
|
+
debug "commit ##{delivery_info.delivery_tag.to_i}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def nack!(multiple = false, requeue = false)
|
12
|
+
self.class.session.channel.ack delivery_info.delivery_tag, multiple, requeue
|
13
|
+
debug "reject ##{delivery_info.delivery_tag.to_i}"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'thread'
|
3
|
+
|
4
|
+
module Vx
|
5
|
+
module Common
|
6
|
+
module AMQP
|
7
|
+
module Consumer::Configuration
|
8
|
+
|
9
|
+
@@consumer_configuration_lock = Mutex.new
|
10
|
+
|
11
|
+
def consumer_configuration
|
12
|
+
@consumer_configuration || reset_consumer_configuration!
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset_consumer_configuration!
|
16
|
+
@@consumer_configuration_lock.synchronize do
|
17
|
+
@consumer_configuration =
|
18
|
+
OpenStruct.new(exchange: OpenStruct.new(options: {}),
|
19
|
+
queue: OpenStruct.new(options: {}),
|
20
|
+
consumer_name: make_consumer_name,
|
21
|
+
ack: false,
|
22
|
+
content_type: nil)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
%w{ exchange queue }.each do |m|
|
27
|
+
define_method m do |*name, &b|
|
28
|
+
options = name.last.is_a?(Hash) ? name.pop : {}
|
29
|
+
val = b || name.first
|
30
|
+
consumer_configuration.__send__(m).name = val
|
31
|
+
consumer_configuration.__send__(m).options = options
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method "#{m}_name" do
|
35
|
+
value_maybe_proc(consumer_configuration.__send__(m).name || consumer_name)
|
36
|
+
end
|
37
|
+
|
38
|
+
define_method "#{m}_options" do
|
39
|
+
value_maybe_proc(consumer_configuration.__send__(m).options)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def routing_key(name = nil, &block)
|
44
|
+
val = block || name
|
45
|
+
if val
|
46
|
+
consumer_configuration.routing_key = val
|
47
|
+
else
|
48
|
+
value_maybe_proc consumer_configuration.routing_key
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def headers(values = nil, &block)
|
53
|
+
val = block || values
|
54
|
+
if val
|
55
|
+
consumer_configuration.headers = val
|
56
|
+
else
|
57
|
+
value_maybe_proc consumer_configuration.headers
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def model(value = nil)
|
62
|
+
consumer_configuration.model = value unless value == nil
|
63
|
+
consumer_configuration.model
|
64
|
+
end
|
65
|
+
|
66
|
+
def content_type(value = nil)
|
67
|
+
consumer_configuration.content_type = value if value
|
68
|
+
consumer_configuration.content_type
|
69
|
+
end
|
70
|
+
|
71
|
+
def ack(value = nil)
|
72
|
+
consumer_configuration.ack = value unless value == nil
|
73
|
+
consumer_configuration.ack
|
74
|
+
end
|
75
|
+
|
76
|
+
def consumer_name
|
77
|
+
consumer_configuration.consumer_name
|
78
|
+
end
|
79
|
+
|
80
|
+
def consumer_id
|
81
|
+
if cid = Thread.current[:vx_amqp_consumer_id]
|
82
|
+
"#{consumer_name}.#{cid}"
|
83
|
+
else
|
84
|
+
consumer_name
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def bind_options
|
89
|
+
consumer_configuration.bind_options ||
|
90
|
+
@@consumer_configuration_lock.synchronize do
|
91
|
+
opts = {}
|
92
|
+
opts[:routing_key] = routing_key if routing_key
|
93
|
+
opts[:headers] = headers if headers
|
94
|
+
consumer_configuration.bind_options = opts
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def value_maybe_proc(val)
|
101
|
+
case val
|
102
|
+
when Proc
|
103
|
+
val.call
|
104
|
+
else
|
105
|
+
val
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def make_consumer_name
|
110
|
+
to_s.split("::")
|
111
|
+
.last
|
112
|
+
.scan(/[A-Z][a-z]*/).join("_")
|
113
|
+
.downcase
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|