fmq 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +24 -0
- data/License.txt +675 -0
- data/PostInstall.txt +3 -0
- data/README.txt +69 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/default-server/admin-interface/index.html +7 -7
- data/default-server/queues/my_test.rb +6 -15
- data/lib/fmq/client.rb +52 -19
- data/lib/fmq/mongrel_server.rb +43 -24
- data/lib/fmq/queue_manager.rb +265 -260
- data/lib/fmq/queues/README.txt +12 -21
- data/lib/fmq/queues/admin.rb +11 -22
- data/lib/fmq/queues/base.rb +75 -0
- data/lib/fmq/queues/file.rb +6 -19
- data/lib/fmq/queues/forward.rb +7 -12
- data/lib/fmq/queues/linked.rb +28 -59
- data/lib/fmq/queues/load_balanced.rb +105 -104
- data/lib/fmq/queues/syncronized.rb +54 -55
- data/lib/fmq/version.rb +2 -2
- data/test/test_basic.rb +36 -0
- data/test/test_fmq_client.rb +33 -26
- data/test/test_helper.rb +5 -1
- data/test/test_linked.rb +65 -0
- data/test/test_queue_manager.rb +90 -0
- metadata +22 -5
- data/setup.rb +0 -1585
- data/test/test_fmq_queue.rb +0 -47
data/lib/fmq/queue_manager.rb
CHANGED
@@ -1,261 +1,266 @@
|
|
1
|
-
#
|
2
|
-
# Copyright (c) 2008 Vincent Landgraf
|
3
|
-
#
|
4
|
-
# This file is part of the Free Message Queue.
|
5
|
-
#
|
6
|
-
# Free Message Queue is free software: you can redistribute it and/or modify
|
7
|
-
# it under the terms of the GNU General Public License as published by
|
8
|
-
# the Free Software Foundation, either version 3 of the License, or
|
9
|
-
# (at your option) any later version.
|
10
|
-
#
|
11
|
-
# Free Message Queue is distributed in the hope that it will be useful,
|
12
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
-
# GNU General Public License for more details.
|
15
|
-
#
|
16
|
-
# You should have received a copy of the GNU General Public License
|
17
|
-
# along with Free Message Queue. If not, see <http://www.gnu.org/licenses/>.
|
18
|
-
#
|
19
|
-
module FreeMessageQueue
|
20
|
-
# All queue manager exceptions are raised using this class
|
21
|
-
class QueueManagerException < Exception
|
22
|
-
attr_accessor :message, :backtrace
|
23
|
-
|
24
|
-
# Create exception with message and backtrace (if needed)
|
25
|
-
def initialize(message, callstack = [])
|
26
|
-
@message = message
|
27
|
-
@backtrace = callstack
|
28
|
-
end
|
29
|
-
|
30
|
-
# Returns the message of the exception
|
31
|
-
def to_s
|
32
|
-
@message
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# The queue manager is one of the core components of the system.
|
37
|
-
# This component manages the queues by pathname and checks on the
|
38
|
-
# corresponding constraints. Every queue that is created by this
|
39
|
-
# queue manager will get a reference (<em>manager</em>) for later use.
|
40
|
-
class QueueManager
|
41
|
-
#
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
#
|
47
|
-
#
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
#
|
61
|
-
# (
|
62
|
-
def
|
63
|
-
@
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
#
|
169
|
-
def
|
170
|
-
@
|
171
|
-
end
|
172
|
-
|
173
|
-
# Returns the
|
174
|
-
def
|
175
|
-
@queue[name]
|
176
|
-
end
|
177
|
-
|
178
|
-
# Is the name (path) of the queue in use allready
|
179
|
-
def queue_exists?(name)
|
180
|
-
!queue
|
181
|
-
end
|
182
|
-
|
183
|
-
# create a queue from a configuration hash.
|
184
|
-
# The <em>queue_name</em> is just for debugging and organizing the queue.
|
185
|
-
# The <em>queue_config</em> contains the following parameter:
|
186
|
-
# * path: the path to the queue (with leading "/" and 3 characters at minimum) e.g. "/test_queue"
|
187
|
-
# * [optional] max-size: the maximum size e.g. "10mb", "100kb", "2gb" or (black or -1) for infinite
|
188
|
-
# * [optional] max-messages: the maximim messages that can be in the queue e.g. 1500 or (black or -1) for infinite
|
189
|
-
# * [optional] class: the class that implements this queue e.g. FreeMessageQueue::SystemQueue
|
190
|
-
# All other parameter will be send to the queue directly using a naming convention. So if you have the extra parameter
|
191
|
-
# expire-date: 1h
|
192
|
-
# the QueueManager will set the expire date using this assignment
|
193
|
-
# queue.expire_date = "1h"
|
194
|
-
# therefore your queue must implement this method
|
195
|
-
# def expire_date=(time)
|
196
|
-
# @expires_after = parse_seconds(time)
|
197
|
-
# end
|
198
|
-
def create_queue_from_config(queue_name, queue_config)
|
199
|
-
@log.debug("[QueueManager] setup queue from config '#{queue_name}'")
|
200
|
-
|
201
|
-
# path need to be specified
|
202
|
-
raise QueueManagerException.new("[QueueManager] There is now path specified for queue '#{queue_name}'", caller) if queue_config["path"].nil?
|
203
|
-
path = queue_config["path"]
|
204
|
-
queue_config.delete "path"
|
205
|
-
|
206
|
-
# set max size parameter -- this parameter is optional
|
207
|
-
max_size = str_bytes(queue_config["max-size"])
|
208
|
-
max_size = INFINITE if max_size.nil? || max_size <= 0
|
209
|
-
queue_config.delete "max-size"
|
210
|
-
|
211
|
-
# set max messages parameter -- this parameter is optional
|
212
|
-
max_messages = queue_config["max-messages"].to_i
|
213
|
-
max_messages = INFINITE if max_messages.nil? || max_messages <= 0
|
214
|
-
queue_config.delete "max-messages"
|
215
|
-
|
216
|
-
# set class parameter -- this parameter is optional
|
217
|
-
default_class = queue_config["class"]
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
for parameter
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
#
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
1
|
+
#
|
2
|
+
# Copyright (c) 2008 Vincent Landgraf
|
3
|
+
#
|
4
|
+
# This file is part of the Free Message Queue.
|
5
|
+
#
|
6
|
+
# Free Message Queue is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU General Public License as published by
|
8
|
+
# the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# Free Message Queue is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU General Public License
|
17
|
+
# along with Free Message Queue. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#
|
19
|
+
module FreeMessageQueue
|
20
|
+
# All queue manager exceptions are raised using this class
|
21
|
+
class QueueManagerException < Exception
|
22
|
+
attr_accessor :message, :backtrace
|
23
|
+
|
24
|
+
# Create exception with message and backtrace (if needed)
|
25
|
+
def initialize(message, callstack = [])
|
26
|
+
@message = message
|
27
|
+
@backtrace = callstack
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the message of the exception
|
31
|
+
def to_s
|
32
|
+
@message
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# The queue manager is one of the core components of the system.
|
37
|
+
# This component manages the queues by pathname and checks on the
|
38
|
+
# corresponding constraints. Every queue that is created by this
|
39
|
+
# queue manager will get a reference (<em>manager</em>) for later use.
|
40
|
+
class QueueManager
|
41
|
+
# Returns the queue that is passed otherwise nil
|
42
|
+
attr_reader :queue
|
43
|
+
|
44
|
+
# Returns the queue constrains as a hash. The hash has the following structure:
|
45
|
+
# {
|
46
|
+
# :max_size => "100mb",
|
47
|
+
# :max_messages => 1000
|
48
|
+
# }
|
49
|
+
attr_reader :queue_constraints
|
50
|
+
|
51
|
+
# This value is used to decribe that a constraint has no limit e.g.
|
52
|
+
# :max_messages => INFINITE
|
53
|
+
# means that there is no limitation for messages
|
54
|
+
INFINITE = -1
|
55
|
+
|
56
|
+
# this is the default queue class if no other is specified this
|
57
|
+
# class will be created when setting up a queue
|
58
|
+
DEFAULT_QUEUE_CLASS = FreeMessageQueue::SyncronizedQueue
|
59
|
+
|
60
|
+
# setup the queue manager using the configuration from the configuration
|
61
|
+
# file (which is basically a hash)
|
62
|
+
def initialize(config)
|
63
|
+
@queue = {}
|
64
|
+
@config = config
|
65
|
+
@queue_constraints = {}
|
66
|
+
@log = FreeMessageQueue.logger
|
67
|
+
setup_queue_manager()
|
68
|
+
end
|
69
|
+
|
70
|
+
# returns if the creation of queues should be done on demand
|
71
|
+
# (if someone sends a post to a queue)
|
72
|
+
def auto_create_queues?
|
73
|
+
@config["auto-create-queues"]
|
74
|
+
end
|
75
|
+
|
76
|
+
# Create a queue (<em>name</em> => <em>path</em>). The path must contain a leading "/" and a 3 character name
|
77
|
+
# at minimum. Exceptions will be raised if the queue allready exists.
|
78
|
+
def create_queue(name, max_messages = INFINITE, max_size = INFINITE, default_class = DEFAULT_QUEUE_CLASS)
|
79
|
+
# path must begin with /
|
80
|
+
raise QueueManagerException.new("[QueueManager] Leading / in path '#{name}' missing", caller) if name[0..0] != "/"
|
81
|
+
|
82
|
+
# path must have a minimus lenght of 3 character
|
83
|
+
raise QueueManagerException.new("[QueueManager] The queue path '#{name}' is to short 3 character is minimum", caller) if name.size - 1 < 3
|
84
|
+
|
85
|
+
# don't create a queue twice
|
86
|
+
raise QueueManagerException.new("[QueueManager] The queue '#{name}' allready exists", caller) if queue_exists? name
|
87
|
+
|
88
|
+
@log.info("[QueueManager] Create queue '#{name}' {type: #{default_class}, max_messages: #{max_messages}, max_size: #{max_size}}")
|
89
|
+
|
90
|
+
@queue[name] = default_class.new(self)
|
91
|
+
@queue_constraints[name] = {
|
92
|
+
:max_messages => max_messages,
|
93
|
+
:max_size => max_size
|
94
|
+
}
|
95
|
+
|
96
|
+
@queue[name]
|
97
|
+
end
|
98
|
+
|
99
|
+
# Delete the queue by name (path)
|
100
|
+
def delete_queue(name)
|
101
|
+
if @queue[name]
|
102
|
+
@log.info("[QueueManager] Delete queue '#{name}' with #{@queue[name].size} messages")
|
103
|
+
@queue[name].clear
|
104
|
+
@queue.delete name
|
105
|
+
true
|
106
|
+
else
|
107
|
+
raise QueueManagerException.new("[QueueManager] There is no queue '#{name}'", caller)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# This returns one message from the passed queue
|
112
|
+
def poll(name)
|
113
|
+
if @queue[name]
|
114
|
+
@log.debug("[QueueManager] Poll from queue '#{name}' with #{@queue[name].size} messages")
|
115
|
+
if @queue[name].respond_to? :poll
|
116
|
+
queue_item = @queue[name].poll
|
117
|
+
else
|
118
|
+
raise QueueManagerException.new("[QueueManager] You can't poll from queue '#{name}'", caller)
|
119
|
+
end
|
120
|
+
else
|
121
|
+
raise QueueManagerException.new("[QueueManager] There is no queue '#{name}'", caller)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
alias get poll
|
126
|
+
|
127
|
+
# Puts a message (<em>data</em>) to the queue and checks if the constraints are vaild otherwise
|
128
|
+
# it will raise a QueueManagerException. If <em>auto_create_queues</em> is set to *true* the queue
|
129
|
+
# will be generated if there isn't a queue with the passed name (path). Otherwise
|
130
|
+
# it will raise a QueueManagerException if the passed queue doesn't exists.
|
131
|
+
def put(name, message)
|
132
|
+
unless @queue[name]
|
133
|
+
# only auto create queues if it is configured
|
134
|
+
if auto_create_queues?
|
135
|
+
create_queue(name)
|
136
|
+
else
|
137
|
+
raise QueueManagerException.new("[QueueManager] There is no queue '#{name}'", caller)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# check max size constraints
|
142
|
+
if @queue_constraints[name][:max_size] != INFINITE &&
|
143
|
+
@queue_constraints[name][:max_size] < queue[name].bytes + message.bytes
|
144
|
+
raise QueueManagerException.new("[QueueManager] The queue '#{name}' is full, max amount of space (#{@queue_constraints[name][:max_size]}) is exceeded", caller)
|
145
|
+
end
|
146
|
+
|
147
|
+
# check max messages constraints
|
148
|
+
if @queue_constraints[name][:max_messages] != INFINITE &&
|
149
|
+
@queue_constraints[name][:max_messages] < queue[name].size + 1
|
150
|
+
raise QueueManagerException.new("[QueueManager] The queue '#{name}' is full, max amount of messages (#{@queue_constraints[name][:max_messages]}) is exceeded", caller)
|
151
|
+
end
|
152
|
+
|
153
|
+
@log.debug("[QueueManager] put message to queue '#{name}' with #{@queue[name].size} messages")
|
154
|
+
if @queue[name].respond_to? :put
|
155
|
+
@queue[name].put(message)
|
156
|
+
else
|
157
|
+
raise QueueManagerException.new("[QueueManager] You can't put to queue '#{name}'", caller)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
alias post put
|
162
|
+
|
163
|
+
# Returns the names (paths) of all queues managed by this queue manager
|
164
|
+
def queues
|
165
|
+
@queue.keys
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns the size (number of messages)
|
169
|
+
def queue_size(name)
|
170
|
+
@queue[name].size
|
171
|
+
end
|
172
|
+
|
173
|
+
# Returns the byte size of the queue
|
174
|
+
def queue_bytes(name)
|
175
|
+
@queue[name].bytes
|
176
|
+
end
|
177
|
+
|
178
|
+
# Is the name (path) of the queue in use allready
|
179
|
+
def queue_exists?(name)
|
180
|
+
!queue[name].nil?
|
181
|
+
end
|
182
|
+
|
183
|
+
# create a queue from a configuration hash.
|
184
|
+
# The <em>queue_name</em> is just for debugging and organizing the queue.
|
185
|
+
# The <em>queue_config</em> contains the following parameter:
|
186
|
+
# * path: the path to the queue (with leading "/" and 3 characters at minimum) e.g. "/test_queue"
|
187
|
+
# * [optional] max-size: the maximum size e.g. "10mb", "100kb", "2gb" or (black or -1) for infinite
|
188
|
+
# * [optional] max-messages: the maximim messages that can be in the queue e.g. 1500 or (black or -1) for infinite
|
189
|
+
# * [optional] class: the class that implements this queue e.g. FreeMessageQueue::SystemQueue
|
190
|
+
# All other parameter will be send to the queue directly using a naming convention. So if you have the extra parameter
|
191
|
+
# expire-date: 1h
|
192
|
+
# the QueueManager will set the expire date using this assignment
|
193
|
+
# queue.expire_date = "1h"
|
194
|
+
# therefore your queue must implement this method
|
195
|
+
# def expire_date=(time)
|
196
|
+
# @expires_after = parse_seconds(time)
|
197
|
+
# end
|
198
|
+
def create_queue_from_config(queue_name, queue_config)
|
199
|
+
@log.debug("[QueueManager] setup queue from config '#{queue_name}'")
|
200
|
+
|
201
|
+
# path need to be specified
|
202
|
+
raise QueueManagerException.new("[QueueManager] There is now path specified for queue '#{queue_name}'", caller) if queue_config["path"].nil?
|
203
|
+
path = queue_config["path"]
|
204
|
+
queue_config.delete "path"
|
205
|
+
|
206
|
+
# set max size parameter -- this parameter is optional
|
207
|
+
max_size = str_bytes(queue_config["max-size"])
|
208
|
+
max_size = INFINITE if max_size.nil? || max_size <= 0
|
209
|
+
queue_config.delete "max-size"
|
210
|
+
|
211
|
+
# set max messages parameter -- this parameter is optional
|
212
|
+
max_messages = queue_config["max-messages"].to_i
|
213
|
+
max_messages = INFINITE if max_messages.nil? || max_messages <= 0
|
214
|
+
queue_config.delete "max-messages"
|
215
|
+
|
216
|
+
# set class parameter -- this parameter is optional
|
217
|
+
default_class = queue_config["class"]
|
218
|
+
puts default_class
|
219
|
+
default_class = eval(default_class) unless default_class.nil?
|
220
|
+
queue_config.delete "class"
|
221
|
+
|
222
|
+
if default_class.nil?
|
223
|
+
queue = create_queue(path, max_messages, max_size)
|
224
|
+
else
|
225
|
+
queue = create_queue(path, max_messages, max_size, default_class)
|
226
|
+
end
|
227
|
+
|
228
|
+
if queue_config.size > 0
|
229
|
+
@log.debug("[QueueManager] Configure addional parameters for queue '#{queue_name}'; parameter: #{queue_config.inspect}")
|
230
|
+
for parameter in queue_config.keys
|
231
|
+
method_name = parameter.gsub("-", "_")
|
232
|
+
queue.send(method_name + "=", queue_config[parameter])
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
private
|
238
|
+
# Retuns count of bytes to a expression with kb, mb or gb
|
239
|
+
# e.g 10kb will return 10240
|
240
|
+
def str_bytes(str)
|
241
|
+
case str
|
242
|
+
when /([0-9]+)kb/i
|
243
|
+
bs = $1.to_i * 1024
|
244
|
+
when /([0-9]+)mb/i
|
245
|
+
bs = $1.to_i * 1024 * 1024
|
246
|
+
when /([0-9]+)gb/i
|
247
|
+
bs = $1.to_i * 1024 * 1024 * 1024
|
248
|
+
else
|
249
|
+
bs = INFINITE
|
250
|
+
end
|
251
|
+
bs
|
252
|
+
end
|
253
|
+
|
254
|
+
# Create the queues that are defined in the configuration
|
255
|
+
def setup_queue_manager
|
256
|
+
if @config.nil?
|
257
|
+
raise QueueManagerException.new("[QueueManager] there is no queue manager configuration" , caller)
|
258
|
+
else
|
259
|
+
@log.info("[QueueManager] Create defined queues (#{@config["defined-queues"].size})")
|
260
|
+
for defined_queue in @config["defined-queues"]
|
261
|
+
create_queue_from_config(defined_queue[0], defined_queue[1])
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
261
266
|
end
|
data/lib/fmq/queues/README.txt
CHANGED
@@ -6,31 +6,22 @@ First of all, this template is where you start.
|
|
6
6
|
2. Name your file to the name of the queue. In this example we have the queue "MyTestQueue"
|
7
7
|
so the file will be "my_test.rb". Save your new file to the queue folder of your projects folder "queues/my_test.rb"
|
8
8
|
3. Change the queue implementation to something you like
|
9
|
-
* every queue must have an <em>manager</em>.
|
10
|
-
* this manager must be able to read <em>bytes</em> and <em>size</em>
|
11
|
-
* the queue should have at least one of the <tt>put(
|
12
|
-
* when implementing the poll queue the object you returning needs to have a <em>
|
13
|
-
This examples uses
|
9
|
+
* every queue must have an <em>manager</em>. (for this just inherit from FreeMessageQueue::BaseQueue)
|
10
|
+
* this manager must be able to read <em>bytes</em> and <em>size</em> (for this just inherit from FreeMessageQueue::BaseQueue)
|
11
|
+
* the queue should have at least one of the <tt>put(message)</tt> or <tt>poll()</tt> methods defined
|
12
|
+
* when implementing the poll queue the object you returning needs to have a <em>payload</em> method.
|
13
|
+
This examples uses FreeMessageQueue::Message for this purpose
|
14
14
|
|
15
15
|
# FILE: my_project/queues/my_test.rb
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
attr_accessor :manager
|
21
|
-
attr_reader :bytes, :size
|
22
|
-
|
23
|
-
def initialize
|
24
|
-
@bytes = @size = 1
|
25
|
-
end
|
26
|
-
|
27
|
-
def put(data)
|
28
|
-
puts "NEW MESSAGE"
|
17
|
+
class MyTestQueue < FreeMessageQueue::BaseQueue
|
18
|
+
def put(message)
|
19
|
+
puts "INCOMMING: #{message.payload}"
|
29
20
|
end
|
30
|
-
|
21
|
+
|
31
22
|
def poll
|
32
|
-
|
33
|
-
|
34
|
-
|
23
|
+
msg = FreeMessageQueue::Message.new "Hello World", "text/plain"
|
24
|
+
msg.option["Time"] = Time.now
|
25
|
+
msg
|
35
26
|
end
|
36
27
|
end
|
data/lib/fmq/queues/admin.rb
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
# You should have received a copy of the GNU General Public License
|
17
17
|
# along with Free Message Queue. If not, see <http://www.gnu.org/licenses/>.
|
18
18
|
#
|
19
|
-
require '
|
19
|
+
require File.dirname(__FILE__) + '/base'
|
20
20
|
|
21
21
|
module FreeMessageQueue
|
22
22
|
# This queue is dedicated to the AJAX based admin interface.
|
@@ -29,24 +29,14 @@ module FreeMessageQueue
|
|
29
29
|
# path: /admin/queue
|
30
30
|
# class: FreeMessageQueue::AdminQueue
|
31
31
|
# filter: /admin
|
32
|
-
class AdminQueue
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
# Bytes size is -1. Size is allways 1 message
|
37
|
-
attr_reader :bytes, :size
|
38
|
-
|
39
|
-
def initialize()
|
40
|
-
super
|
41
|
-
@bytes = -1
|
42
|
-
@size = 1
|
32
|
+
class AdminQueue < BaseQueue
|
33
|
+
def initialize(manager)
|
34
|
+
super(manager)
|
43
35
|
@filter_queues = []
|
44
36
|
end
|
45
37
|
|
46
38
|
# returns an json list of visible queues
|
47
39
|
def poll()
|
48
|
-
item = OpenStruct.new
|
49
|
-
|
50
40
|
queues_code = []
|
51
41
|
manager.queues.each do |queue_name|
|
52
42
|
# skip if it is filterd
|
@@ -56,16 +46,15 @@ module FreeMessageQueue
|
|
56
46
|
queues_code << queue_to_json(queue_name)
|
57
47
|
end
|
58
48
|
|
59
|
-
|
60
|
-
item
|
49
|
+
Message.new("[%s]" % queues_code.join(","), "application/json")
|
61
50
|
end
|
62
51
|
|
63
52
|
# can be either used to *create* or *delete* a queue
|
64
|
-
def put(
|
65
|
-
if
|
53
|
+
def put(message)
|
54
|
+
if message.payload.match(/_method=delete&path=(.*)/)
|
66
55
|
# delete queue
|
67
56
|
manager.delete_queue($1)
|
68
|
-
elsif
|
57
|
+
elsif message.payload.match(/_method=create&data=(.*)/)
|
69
58
|
# create queue
|
70
59
|
conf = eval($1.gsub(":", "=>").gsub("null", "-1"))
|
71
60
|
manager.create_queue_from_config("dynamic-created-queue", conf)
|
@@ -91,13 +80,13 @@ module FreeMessageQueue
|
|
91
80
|
|
92
81
|
# converts the data of one queue to json format
|
93
82
|
def queue_to_json(queue_name)
|
94
|
-
constraints = manager.queue_constraints
|
83
|
+
constraints = manager.queue_constraints[queue_name]
|
95
84
|
|
96
85
|
"[\"%s\", %d, %d, %d, %d]" % [
|
97
86
|
queue_name,
|
98
|
-
manager.queue
|
87
|
+
manager.queue[queue_name].bytes,
|
99
88
|
constraints[:max_size],
|
100
|
-
manager.queue
|
89
|
+
manager.queue[queue_name].size,
|
101
90
|
constraints[:max_messages],
|
102
91
|
]
|
103
92
|
end
|