em-beanstalk 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,9 +1,10 @@
1
- = EMJack
1
+ = EM::Beanstalk
2
2
  An attempt to wrap portions of the Beanstalk protocol with EventMachine. Every command
3
- will return a deferrable object. That object will succeed or fail depending on the
4
- reply returned from Beanstalk.
3
+ will return a deferrable object. As well, every command can accept a block with will
4
+ be used as the succeed callback for each command. That object will succeed or fail
5
+ depending on the reply returned from Beanstalk.
5
6
 
6
- The current, default, errback is to print the error message received.
7
+ The default errback is to print the error message received.
7
8
 
8
9
  One thing to keep in mind. The Beanstalk protocol executes all commands serially.
9
10
  So, if you send a reserve command and there are no jobs Beanstalk _won't_ process
@@ -19,7 +20,7 @@ are no available jobs.
19
20
 
20
21
  = Examples
21
22
  EM.run {
22
- jack = EMJack::Connection.new
23
+ jack = EM::Beanstalk.new
23
24
 
24
25
  jack.use('mytube') { |tube| puts "Using #{tube}" }
25
26
 
@@ -40,14 +41,14 @@ are no available jobs.
40
41
  }
41
42
 
42
43
 
43
- EMJack#each_job is useful for scenarios where the client is a job worker
44
+ EM::Beanstalk#each_job is useful for scenarios where the client is a job worker
44
45
  and intended to process jobs continuously as they become available. Once
45
46
  the queue is empty, the client will block and wait for a new job.
46
47
 
47
48
  If multiple workers connect to the queue Beanstalkd will round-robin between
48
49
  the workers.
49
50
  EM.run {
50
- jack = EMJack::Connection.new
51
+ jack = EM::Beanstalk.new
51
52
 
52
53
  jack.each_job do |job|
53
54
  puts "Got job ##{job.id}: #{job}"
@@ -63,11 +64,3 @@ are no available jobs.
63
64
  # Some kind of job processing
64
65
  end
65
66
  }
66
-
67
-
68
- = Contact
69
- If you've got any questions, comments or bugs, please let me know. You can
70
- contact me by email at dj2 at everburning dot com.
71
-
72
- = Contributors
73
- Peter Kieltyka (EMJack#each_job)
data/Rakefile CHANGED
@@ -1,8 +1,6 @@
1
1
  libdir = File.expand_path("lib")
2
2
  $:.unshift(libdir) unless $:.include?(libdir)
3
3
 
4
- require 'em-jack'
5
-
6
4
  begin
7
5
  require 'jeweler'
8
6
  Jeweler::Tasks.new do |s|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -1,12 +1,24 @@
1
1
  require 'eventmachine'
2
2
  require 'yaml'
3
3
 
4
- module EMJack
5
- class Connection
4
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
5
+
6
+ require 'em-beanstalk/job'
7
+ require 'em-beanstalk/connection'
8
+
9
+ module EM
10
+ class Beanstalk
11
+
12
+ Disconnected = Class.new(RuntimeError)
13
+ InvalidCommand = Class.new(RuntimeError)
14
+
15
+ module VERSION
16
+ STRING = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))
17
+ end
6
18
 
7
19
  attr_accessor :host, :port
8
20
  attr_reader :default_priority, :default_delay, :default_ttr
9
-
21
+
10
22
  def initialize(opts = nil)
11
23
  @host = opts && opts[:host] || 'localhost'
12
24
  @port = opts && opts[:port] || 11300
@@ -16,32 +28,32 @@ module EMJack
16
28
  @default_delay = opts && opts[:default_delay] || 0
17
29
  @default_ttr = opts && opts[:default_ttr] || 300
18
30
  @default_timeout = opts && opts[:timeout] || 5
19
-
31
+
20
32
  @used_tube = 'default'
21
33
  @watched_tubes = [@used_tube]
22
-
34
+
23
35
  @data = ""
24
36
  @retries = 0
25
37
  @in_reserve = false
26
38
  @deferrables = []
27
-
28
- @conn = EM::connect(host, port, EMJack::BeanstalkConnection) do |conn|
39
+
40
+ @conn = EM::connect(host, port, EM::Beanstalk::Connection) do |conn|
29
41
  conn.client = self
30
42
  conn.comm_inactivity_timeout = 0
31
43
  conn.pending_connect_timeout = @default_timeout
32
44
  end
33
-
45
+
34
46
  unless @tube.nil?
35
47
  use(@tube)
36
48
  watch(@tube)
37
49
  end
38
50
  end
39
-
51
+
40
52
  def close
41
53
  @disconnect_manually = true
42
54
  @conn.close_connection
43
55
  end
44
-
56
+
45
57
  def drain!(&block)
46
58
  stats do |stats|
47
59
  stats['current-jobs-ready'].zero? ?
@@ -49,22 +61,22 @@ module EMJack
49
61
  reserve{|job| job.delete{ drain!(&block) }}
50
62
  end
51
63
  end
52
-
64
+
53
65
  def use(tube, &block)
54
66
  return if @used_tube == tube
55
67
  @used_tube = tube
56
68
  @conn.send(:use, tube)
57
69
  add_deferrable(&block)
58
-
59
- end
60
70
 
71
+ end
72
+
61
73
  def watch(tube, &block)
62
74
  return if @watched_tubes.include?(tube)
63
75
  @watched_tubes.push(tube)
64
76
  @conn.send(:watch, tube)
65
77
  add_deferrable(&block)
66
78
  end
67
-
79
+
68
80
  def ignore(tube, &block)
69
81
  return if not @watched_tubes.include?(tube)
70
82
  @watched_tubes.delete(tube)
@@ -97,7 +109,7 @@ module EMJack
97
109
  when nil then @conn.send(:stats)
98
110
  when :tube then @conn.send(:'stats-tube', val)
99
111
  when :job then @conn.send(:'stats-job', job_id(val))
100
- else raise EMJack::InvalidCommand.new
112
+ else raise EM::Beanstalk::InvalidCommand.new
101
113
  end
102
114
  add_deferrable(&block)
103
115
  end
@@ -110,13 +122,13 @@ module EMJack
110
122
  val
111
123
  end
112
124
  end
113
-
125
+
114
126
  def list(type = nil, &block)
115
127
  case(type)
116
128
  when nil then @conn.send(:'list-tubes')
117
129
  when :use, :used then @conn.send(:'list-tube-used')
118
130
  when :watch, :watched then @conn.send(:'list-tubes-watched')
119
- else raise EMJack::InvalidCommand.new
131
+ else raise EM::Beanstalk::InvalidCommand.new
120
132
  end
121
133
  add_deferrable(&block)
122
134
  end
@@ -126,7 +138,7 @@ module EMJack
126
138
  @conn.send(:delete, job_id(val))
127
139
  add_deferrable(&block)
128
140
  end
129
-
141
+
130
142
  def put(msg, opts = nil, &block)
131
143
  case msg
132
144
  when Job
@@ -140,16 +152,16 @@ module EMJack
140
152
  ttr = opts && opts[:ttr] || default_ttr
141
153
  body = msg.to_s
142
154
  end
143
-
155
+
144
156
  priority = default_priority if priority < 0
145
157
  priority = 2 ** 32 if priority > (2 ** 32)
146
158
  delay = default_delay if delay < 0
147
159
  ttr = default_ttr if ttr < 0
148
-
160
+
149
161
  @conn.send_with_data(:put, body, priority, delay, ttr, body.size)
150
162
  add_deferrable(&block)
151
163
  end
152
-
164
+
153
165
  def release(job, &block)
154
166
  return if job.nil?
155
167
  @conn.send(:release, job.jobid, 0, 0)
@@ -163,7 +175,7 @@ module EMJack
163
175
  def disconnected
164
176
  @deferrables.each {|d| d.fail }
165
177
  unless @disconnect_manually
166
- raise EMJack::Disconnected if @retries >= @retry_count
178
+ raise EM::Beanstalk::Disconnected if @retries >= @retry_count
167
179
  @retries += 1
168
180
  EM.add_timer(1) { reconnect }
169
181
  end
@@ -183,16 +195,16 @@ module EMJack
183
195
  puts "ERROR"
184
196
  end
185
197
  end
186
-
198
+
187
199
  @deferrables.push(df)
188
200
  df.callback(&block) if block
189
201
  df
190
202
  end
191
-
203
+
192
204
  def on_error(&block)
193
205
  @error_callback = block
194
206
  end
195
-
207
+
196
208
  def received(data)
197
209
  @data << data
198
210
 
@@ -201,7 +213,7 @@ module EMJack
201
213
  break if idx.nil?
202
214
 
203
215
  first = $1
204
-
216
+
205
217
  handled = false
206
218
  %w(OUT_OF_MEMORY INTERNAL_ERROR DRAINING BAD_FORMAT
207
219
  UNKNOWN_COMMAND EXPECTED_CRLF JOB_TOO_BIG DEADLINE_SOON
@@ -259,7 +271,7 @@ module EMJack
259
271
  break if body.nil?
260
272
 
261
273
  df = @deferrables.shift
262
- job = EMJack::Job.new(self, id, body)
274
+ job = EM::Beanstalk::Job.new(self, id, body)
263
275
  df.succeed(job)
264
276
  next
265
277
  else
@@ -268,7 +280,7 @@ module EMJack
268
280
  @data.slice!(0, first.size)
269
281
  end
270
282
  end
271
-
283
+
272
284
  def extract_body(bytes, data)
273
285
  rem = data[(data.index(/\r\n/) + 2)..-1]
274
286
  return [nil, data] if rem.length < bytes
@@ -0,0 +1,38 @@
1
+ module EM
2
+ class Beanstalk
3
+ class Connection < EM::Connection
4
+ attr_accessor :client
5
+
6
+ def connection_completed
7
+ client.connected
8
+ end
9
+
10
+ def receive_data(data)
11
+ client.received(data)
12
+ end
13
+
14
+ def send(command, *args)
15
+ real_send(command, args, nil)
16
+ end
17
+
18
+ def send_with_data(command, data, *args)
19
+ real_send(command, args, data)
20
+ end
21
+
22
+ def unbind
23
+ client.disconnected
24
+ end
25
+
26
+ protected
27
+ def real_send(command, args, data = nil)
28
+ send_data(command.to_s)
29
+ args.each{ |a| send_data(' '); send_data(a) }
30
+ send_data("\r\n")
31
+ if data
32
+ send_data(data)
33
+ send_data("\r\n")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,32 @@
1
+ module EM
2
+ class Beanstalk
3
+ class Job
4
+
5
+ attr_reader :id, :conn
6
+ attr_accessor :body, :ttr, :priority, :delay
7
+
8
+ def initialize(conn, id, body)
9
+ @conn = conn
10
+ @id = id && Integer(id)
11
+ @body = body
12
+ @priority = conn && conn.default_priority
13
+ @delay = conn && conn.default_delay
14
+ @ttr = conn && conn.default_ttr
15
+ end
16
+
17
+ alias_method :jobid, :id
18
+
19
+ def delete(&block)
20
+ conn.delete(self, &block)
21
+ end
22
+
23
+ def stats(&block)
24
+ conn.stats(:job, self, &block)
25
+ end
26
+
27
+ def to_s
28
+ "#{id} -- #{body.inspect}"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,49 +1,49 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
- describe EMJack::Connection do
3
+ describe EM::Beanstalk do
4
4
  before(:each) do
5
5
  @connection_mock = mock(:conn)
6
6
  EM.should_receive(:connect).and_return(@connection_mock)
7
7
  end
8
8
 
9
9
  it 'should use a default host of "localhost"' do
10
- conn = EMJack::Connection.new
10
+ conn = EM::Beanstalk.new
11
11
  conn.host.should == 'localhost'
12
12
  end
13
13
 
14
14
  it 'should use a default port of 11300' do
15
- conn = EMJack::Connection.new
15
+ conn = EM::Beanstalk.new
16
16
  conn.port.should == 11300
17
17
  end
18
18
 
19
19
  it 'should watch and use a provided tube on connect' do
20
20
  @connection_mock.should_receive(:send).once.with(:use, "mytube")
21
21
  @connection_mock.should_receive(:send).once.with(:watch, "mytube")
22
- conn = EMJack::Connection.new(:tube => "mytube")
22
+ conn = EM::Beanstalk.new(:tube => "mytube")
23
23
  end
24
24
 
25
25
  it 'should send the "use" command' do
26
26
  @connection_mock.should_receive(:send).once.with(:use, "mytube")
27
- conn = EMJack::Connection.new
27
+ conn = EM::Beanstalk.new
28
28
  conn.use("mytube")
29
29
  end
30
30
 
31
31
  it 'should not send the use command to the currently used tube' do
32
32
  @connection_mock.should_receive(:send).once.with(:use, "mytube")
33
- conn = EMJack::Connection.new
33
+ conn = EM::Beanstalk.new
34
34
  conn.use("mytube")
35
35
  conn.use("mytube")
36
36
  end
37
37
 
38
38
  it 'should send the "watch" command' do
39
39
  @connection_mock.should_receive(:send).once.with(:watch, "mytube")
40
- conn = EMJack::Connection.new
40
+ conn = EM::Beanstalk.new
41
41
  conn.watch("mytube")
42
42
  end
43
43
 
44
44
  it 'should not send the watch command for a tube currently watched' do
45
45
  @connection_mock.should_receive(:send).once.with(:watch, "mytube")
46
- conn = EMJack::Connection.new
46
+ conn = EM::Beanstalk.new
47
47
  conn.watch("mytube")
48
48
  conn.watch("mytube")
49
49
  end
@@ -52,70 +52,70 @@ describe EMJack::Connection do
52
52
  msg = "my message"
53
53
  @connection_mock.should_receive(:send_with_data).once.
54
54
  with(:put, msg, anything, anything, anything, msg.length)
55
- conn = EMJack::Connection.new
55
+ conn = EM::Beanstalk.new
56
56
  conn.put(msg)
57
57
  end
58
58
 
59
59
  it 'should default the delay, priority and ttr settings' do
60
60
  @connection_mock.should_receive(:send_with_data).once.
61
61
  with(:put, anything, 65536, 0, 300, anything)
62
- conn = EMJack::Connection.new
62
+ conn = EM::Beanstalk.new
63
63
  conn.put("msg")
64
64
  end
65
65
 
66
66
  it 'should accept a delay setting' do
67
67
  @connection_mock.should_receive(:send_with_data).once.
68
68
  with(:put, anything, anything, 42, anything, anything)
69
- conn = EMJack::Connection.new
69
+ conn = EM::Beanstalk.new
70
70
  conn.put("msg", :delay => 42)
71
71
  end
72
72
 
73
73
  it 'should accept a ttr setting' do
74
74
  @connection_mock.should_receive(:send_with_data).once.
75
75
  with(:put, anything, anything, anything, 999, anything)
76
- conn = EMJack::Connection.new
76
+ conn = EM::Beanstalk.new
77
77
  conn.put("msg", :ttr => 999)
78
78
  end
79
79
 
80
80
  it 'should accept a priority setting' do
81
81
  @connection_mock.should_receive(:send_with_data).once.
82
82
  with(:put, anything, 233, anything, anything, anything)
83
- conn = EMJack::Connection.new
83
+ conn = EM::Beanstalk.new
84
84
  conn.put("msg", :priority => 233)
85
85
  end
86
86
 
87
87
  it 'shoudl accept a priority, delay and ttr setting' do
88
88
  @connection_mock.should_receive(:send_with_data).once.
89
89
  with(:put, anything, 99, 42, 2000, anything)
90
- conn = EMJack::Connection.new
90
+ conn = EM::Beanstalk.new
91
91
  conn.put("msg", :priority => 99, :delay => 42, :ttr => 2000)
92
92
  end
93
93
 
94
94
  it 'should force delay to be >= 0' do
95
95
  @connection_mock.should_receive(:send_with_data).once.
96
96
  with(:put, anything, anything, 0, anything, anything)
97
- conn = EMJack::Connection.new
97
+ conn = EM::Beanstalk.new
98
98
  conn.put("msg", :delay => -42)
99
99
  end
100
100
 
101
101
  it 'should force ttr to be >= 0' do
102
102
  @connection_mock.should_receive(:send_with_data).once.
103
103
  with(:put, anything, anything, anything, 300, anything)
104
- conn = EMJack::Connection.new
104
+ conn = EM::Beanstalk.new
105
105
  conn.put("msg", :ttr => -42)
106
106
  end
107
107
 
108
108
  it 'should force priority to be >= 0' do
109
109
  @connection_mock.should_receive(:send_with_data).once.
110
110
  with(:put, anything, 65536, anything, anything, anything)
111
- conn = EMJack::Connection.new
111
+ conn = EM::Beanstalk.new
112
112
  conn.put("msg", :priority => -42)
113
113
  end
114
114
 
115
115
  it 'should force priority to be < 2**32' do
116
116
  @connection_mock.should_receive(:send_with_data).once.
117
117
  with(:put, anything, (2 ** 32), anything, anything, anything)
118
- conn = EMJack::Connection.new
118
+ conn = EM::Beanstalk.new
119
119
  conn.put("msg", :priority => (2 ** 32 + 1))
120
120
  end
121
121
 
@@ -123,51 +123,51 @@ describe EMJack::Connection do
123
123
  msg = 22
124
124
  @connection_mock.should_receive(:send_with_data).once.
125
125
  with(:put, msg.to_s, anything, anything, anything, msg.to_s.length)
126
- conn = EMJack::Connection.new
126
+ conn = EM::Beanstalk.new
127
127
  conn.put(msg)
128
128
  end
129
129
 
130
130
  it 'should send the "delete" command' do
131
131
  @connection_mock.should_receive(:send).once.with(:delete, 1)
132
- job = EMJack::Job.new(nil, 1, "body")
133
- conn = EMJack::Connection.new
132
+ job = EM::Beanstalk::Job.new(nil, 1, "body")
133
+ conn = EM::Beanstalk.new
134
134
  conn.delete(job)
135
135
  end
136
136
 
137
137
  it 'should handle a nil job sent to the "delete" command' do
138
138
  @connection_mock.should_not_receive(:send).with(:delete, nil)
139
- conn = EMJack::Connection.new
139
+ conn = EM::Beanstalk.new
140
140
  conn.delete(nil)
141
141
  end
142
142
 
143
143
  it 'should send the "reserve" command' do
144
144
  @connection_mock.should_receive(:send).with(:reserve)
145
- conn = EMJack::Connection.new
145
+ conn = EM::Beanstalk.new
146
146
  conn.reserve
147
147
  end
148
148
 
149
149
  it 'should raise exception if reconnect fails more then RETRY_COUNT times' do
150
150
  EM.should_receive(:add_timer).exactly(5).times
151
151
 
152
- conn = EMJack::Connection.new
152
+ conn = EM::Beanstalk.new
153
153
  5.times { conn.disconnected }
154
- lambda { conn.disconnected }.should raise_error(EMJack::Disconnected)
154
+ lambda { conn.disconnected }.should raise_error(EM::Beanstalk::Disconnected)
155
155
  end
156
156
 
157
157
  it 'should reset the retry count on connection' do
158
158
  EM.should_receive(:add_timer).at_least(1).times
159
159
 
160
- conn = EMJack::Connection.new
160
+ conn = EM::Beanstalk.new
161
161
  5.times { conn.disconnected }
162
162
  conn.connected
163
- lambda { conn.disconnected }.should_not raise_error(EMJack::Disconnected)
163
+ lambda { conn.disconnected }.should_not raise_error(EM::Beanstalk::Disconnected)
164
164
  end
165
165
 
166
166
  %w(OUT_OF_MEMORY INTERNAL_ERROR DRAINING BAD_FORMAT
167
167
  UNKNOWN_COMMAND EXPECTED_CRLF JOB_TOO_BIG DEADLINE_SOON
168
168
  TIMED_OUT NOT_FOUND).each do |cmd|
169
169
  it "should handle #{cmd} messages" do
170
- conn = EMJack::Connection.new
170
+ conn = EM::Beanstalk.new
171
171
 
172
172
  df = conn.add_deferrable
173
173
  df.should_receive(:fail).with(cmd.downcase.to_sym)
@@ -177,7 +177,7 @@ describe EMJack::Connection do
177
177
  end
178
178
 
179
179
  it 'should handle deleted messages' do
180
- conn = EMJack::Connection.new
180
+ conn = EM::Beanstalk.new
181
181
 
182
182
  df = conn.add_deferrable
183
183
  df.should_receive(:succeed)
@@ -186,7 +186,7 @@ describe EMJack::Connection do
186
186
  end
187
187
 
188
188
  it 'should handle inserted messages' do
189
- conn = EMJack::Connection.new
189
+ conn = EM::Beanstalk.new
190
190
 
191
191
  df = conn.add_deferrable
192
192
  df.should_receive(:succeed).with(40)
@@ -195,7 +195,7 @@ describe EMJack::Connection do
195
195
  end
196
196
 
197
197
  it 'should handle buried messages' do
198
- conn = EMJack::Connection.new
198
+ conn = EM::Beanstalk.new
199
199
 
200
200
  df = conn.add_deferrable
201
201
  df.should_receive(:fail).with(:buried, 40)
@@ -204,7 +204,7 @@ describe EMJack::Connection do
204
204
  end
205
205
 
206
206
  it 'should handle using messages' do
207
- conn = EMJack::Connection.new
207
+ conn = EM::Beanstalk.new
208
208
 
209
209
  df = conn.add_deferrable
210
210
  df.should_receive(:succeed).with("mytube")
@@ -213,7 +213,7 @@ describe EMJack::Connection do
213
213
  end
214
214
 
215
215
  it 'should handle watching messages' do
216
- conn = EMJack::Connection.new
216
+ conn = EM::Beanstalk.new
217
217
 
218
218
  df = conn.add_deferrable
219
219
  df.should_receive(:succeed).with(24)
@@ -222,13 +222,13 @@ describe EMJack::Connection do
222
222
  end
223
223
 
224
224
  it 'should handle reserved messages' do
225
- conn = EMJack::Connection.new
225
+ conn = EM::Beanstalk.new
226
226
 
227
227
  msg = "This is my message"
228
228
 
229
229
  df = conn.add_deferrable
230
230
  df.should_receive(:succeed).with do |job|
231
- job.class.should == EMJack::Job
231
+ job.class.should == EM::Beanstalk::Job
232
232
  job.jobid.should == 42
233
233
  job.body.should == msg
234
234
  end
@@ -237,7 +237,7 @@ describe EMJack::Connection do
237
237
  end
238
238
 
239
239
  it 'should handle receiving multiple replies in one packet' do
240
- conn = EMJack::Connection.new
240
+ conn = EM::Beanstalk.new
241
241
 
242
242
  df = conn.add_deferrable
243
243
  df.should_receive(:succeed).with(24)
@@ -249,7 +249,7 @@ describe EMJack::Connection do
249
249
  end
250
250
 
251
251
  it 'should handle receiving data in chunks' do
252
- conn = EMJack::Connection.new
252
+ conn = EM::Beanstalk.new
253
253
 
254
254
  msg1 = "First half of the message\r\n"
255
255
  msg2 = "Last half of the message"
@@ -265,12 +265,12 @@ describe EMJack::Connection do
265
265
 
266
266
  it 'should send the stat command' do
267
267
  @connection_mock.should_receive(:send).once.with(:stats)
268
- conn = EMJack::Connection.new
268
+ conn = EM::Beanstalk.new
269
269
  conn.stats
270
270
  end
271
271
 
272
272
  it 'should handle receiving the OK command' do
273
- conn = EMJack::Connection.new
273
+ conn = EM::Beanstalk.new
274
274
 
275
275
  msg =<<-HERE
276
276
  ---
@@ -298,51 +298,51 @@ HERE
298
298
  end
299
299
 
300
300
  it 'should support job stats' do
301
- job = EMJack::Job.new(nil, 42, "blah")
301
+ job = EM::Beanstalk::Job.new(nil, 42, "blah")
302
302
 
303
303
  @connection_mock.should_receive(:send).once.with(:'stats-job', 42)
304
- conn = EMJack::Connection.new
304
+ conn = EM::Beanstalk.new
305
305
  conn.stats(:job, job)
306
306
  end
307
307
 
308
308
  it 'should support tube stats' do
309
309
  @connection_mock.should_receive(:send).once.with(:'stats-tube', "mytube")
310
- conn = EMJack::Connection.new
310
+ conn = EM::Beanstalk.new
311
311
  conn.stats(:tube, "mytube")
312
312
  end
313
313
 
314
314
  it 'should throw exception on invalid stats command' do
315
315
  @connection_mock.should_not_receive(:send)
316
- conn = EMJack::Connection.new
317
- lambda { conn.stats(:blah) }.should raise_error(EMJack::InvalidCommand)
316
+ conn = EM::Beanstalk.new
317
+ lambda { conn.stats(:blah) }.should raise_error(EM::Beanstalk::InvalidCommand)
318
318
  end
319
319
 
320
320
  it 'should support listing tubes' do
321
321
  @connection_mock.should_receive(:send).once.with(:'list-tubes')
322
- conn = EMJack::Connection.new
322
+ conn = EM::Beanstalk.new
323
323
  conn.list
324
324
  end
325
325
 
326
326
  it 'should support listing tube used' do
327
327
  @connection_mock.should_receive(:send).once.with(:'list-tube-used')
328
- conn = EMJack::Connection.new
328
+ conn = EM::Beanstalk.new
329
329
  conn.list(:used)
330
330
  end
331
331
 
332
332
  it 'should support listing tubes watched' do
333
333
  @connection_mock.should_receive(:send).once.with(:'list-tubes-watched')
334
- conn = EMJack::Connection.new
334
+ conn = EM::Beanstalk.new
335
335
  conn.list(:watched)
336
336
  end
337
337
 
338
338
  it 'should throw exception on invalid list command' do
339
339
  @connection_mock.should_not_receive(:send)
340
- conn = EMJack::Connection.new
341
- lambda { conn.list(:blah) }.should raise_error(EMJack::InvalidCommand)
340
+ conn = EM::Beanstalk.new
341
+ lambda { conn.list(:blah) }.should raise_error(EM::Beanstalk::InvalidCommand)
342
342
  end
343
343
 
344
344
  it 'should accept a response broken over multiple packets' do
345
- conn = EMJack::Connection.new
345
+ conn = EM::Beanstalk.new
346
346
 
347
347
  msg1 = "First half of the message\r\n"
348
348
  msg2 = "Last half of the message"
@@ -358,7 +358,7 @@ HERE
358
358
  end
359
359
 
360
360
  it 'should accept a response broken over multiple packets' do
361
- conn = EMJack::Connection.new
361
+ conn = EM::Beanstalk.new
362
362
 
363
363
  msg1 = "First half of the message\r\n"
364
364
  msg2 = "Last half of the message"
@@ -1,17 +1,17 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
- describe EMJack::Connection, "integration" do
3
+ describe EM::Beanstalk, "integration" do
4
4
  include EM::Spec
5
5
 
6
6
  it 'should use a default host of "localhost" and port of 11300' do
7
- conn = EMJack::Connection.new
7
+ conn = EM::Beanstalk.new
8
8
  conn.host.should == 'localhost'
9
9
  conn.port.should == 11300
10
10
  done
11
11
  end
12
12
 
13
13
  it 'should watch and use a provided tube on connect' do
14
- conn = EMJack::Connection.new
14
+ conn = EM::Beanstalk.new
15
15
  conn.watch('my-lovely-tube') do
16
16
  conn.list(:watched) do |watched|
17
17
  watched.should include("my-lovely-tube")
@@ -21,7 +21,7 @@ describe EMJack::Connection, "integration" do
21
21
  end
22
22
 
23
23
  it 'should send the "use" command' do
24
- conn = EMJack::Connection.new
24
+ conn = EM::Beanstalk.new
25
25
  conn.use('my-lovely-tube') do
26
26
  conn.list(:used) do |used|
27
27
  used.should include("my-lovely-tube")
@@ -31,7 +31,7 @@ describe EMJack::Connection, "integration" do
31
31
  end
32
32
 
33
33
  it "should put a job" do
34
- conn = EMJack::Connection.new
34
+ conn = EM::Beanstalk.new
35
35
  conn.put('myjob') do
36
36
  conn.reserve do |job|
37
37
  job.body.should == 'myjob'
@@ -41,7 +41,7 @@ describe EMJack::Connection, "integration" do
41
41
  end
42
42
 
43
43
  it "should drain the queue" do
44
- conn = EMJack::Connection.new
44
+ conn = EM::Beanstalk.new
45
45
  conn.put('myjob')
46
46
  conn.put('myjob')
47
47
  conn.put('myjob')
@@ -56,7 +56,7 @@ describe EMJack::Connection, "integration" do
56
56
  end
57
57
 
58
58
  it 'should default the delay, priority and ttr settings' do
59
- conn = EMJack::Connection.new
59
+ conn = EM::Beanstalk.new
60
60
  conn.put('myjob') do
61
61
  conn.reserve do |job|
62
62
  job.delay.should == conn.default_delay
@@ -68,7 +68,7 @@ describe EMJack::Connection, "integration" do
68
68
  end
69
69
 
70
70
  it 'should accept a delay setting' do
71
- conn = EMJack::Connection.new
71
+ conn = EM::Beanstalk.new
72
72
  start = Time.new.to_f
73
73
  conn.put('mydelayedjob', :delay => 3) do |id|
74
74
  conn.reserve do |job|
@@ -80,13 +80,13 @@ describe EMJack::Connection, "integration" do
80
80
  end
81
81
 
82
82
  it 'should accept a ttr setting' do
83
- conn = EMJack::Connection.new
83
+ conn = EM::Beanstalk.new
84
84
  conn.put('mydelayedjob', :ttr => 1) do |id|
85
85
 
86
86
  conn.reserve do |job|
87
87
  job.id.should == id
88
88
  EM.add_timer(3) do
89
- conn2 = EMJack::Connection.new
89
+ conn2 = EM::Beanstalk.new
90
90
  conn2.reserve do |job2|
91
91
  job2.id.should == id
92
92
  job2.delete { done }
@@ -97,7 +97,7 @@ describe EMJack::Connection, "integration" do
97
97
  end
98
98
 
99
99
  it 'should accept a priority setting' do
100
- conn = EMJack::Connection.new
100
+ conn = EM::Beanstalk.new
101
101
  conn.put('myhighpriorityjob', :priority => 800) do |low_id|
102
102
  conn.put('myhighpriorityjob', :priority => 300) do |high_id|
103
103
  conn.reserve do |job|
@@ -109,7 +109,7 @@ describe EMJack::Connection, "integration" do
109
109
  end
110
110
 
111
111
  it 'should handle a non-string provided as the put message' do
112
- conn = EMJack::Connection.new
112
+ conn = EM::Beanstalk.new
113
113
  conn.put(22) do |id|
114
114
  conn.reserve do |job|
115
115
  job.body.should == '22'
@@ -120,45 +120,45 @@ describe EMJack::Connection, "integration" do
120
120
  #
121
121
  #it 'should send the "delete" command' do
122
122
  # @connection_mock.should_receive(:send).once.with(:delete, 1)
123
- # job = EMJack::Job.new(nil, 1, "body")
124
- # conn = EMJack::Connection.new
123
+ # job = EM::Beanstalk::Job.new(nil, 1, "body")
124
+ # conn = EM::Beanstalk.new
125
125
  # conn.delete(job)
126
126
  #end
127
127
  #
128
128
  #it 'should handle a nil job sent to the "delete" command' do
129
129
  # @connection_mock.should_not_receive(:send).with(:delete, nil)
130
- # conn = EMJack::Connection.new
130
+ # conn = EM::Beanstalk.new
131
131
  # conn.delete(nil)
132
132
  #end
133
133
  #
134
134
  #it 'should send the "reserve" command' do
135
135
  # @connection_mock.should_receive(:send).with(:reserve)
136
- # conn = EMJack::Connection.new
136
+ # conn = EM::Beanstalk.new
137
137
  # conn.reserve
138
138
  #end
139
139
  #
140
140
  #it 'should raise exception if reconnect fails more then RETRY_COUNT times' do
141
141
  # EM.should_receive(:add_timer).exactly(5).times
142
142
  #
143
- # conn = EMJack::Connection.new
143
+ # conn = EM::Beanstalk.new
144
144
  # 5.times { conn.disconnected }
145
- # lambda { conn.disconnected }.should raise_error(EMJack::Disconnected)
145
+ # lambda { conn.disconnected }.should raise_error(EM::Beanstalk::Disconnected)
146
146
  #end
147
147
  #
148
148
  #it 'should reset the retry count on connection' do
149
149
  # EM.should_receive(:add_timer).at_least(1).times
150
150
  #
151
- # conn = EMJack::Connection.new
151
+ # conn = EM::Beanstalk.new
152
152
  # 5.times { conn.disconnected }
153
153
  # conn.connected
154
- # lambda { conn.disconnected }.should_not raise_error(EMJack::Disconnected)
154
+ # lambda { conn.disconnected }.should_not raise_error(EM::Beanstalk::Disconnected)
155
155
  #end
156
156
  #
157
157
  #%w(OUT_OF_MEMORY INTERNAL_ERROR DRAINING BAD_FORMAT
158
158
  # UNKNOWN_COMMAND EXPECTED_CRLF JOB_TOO_BIG DEADLINE_SOON
159
159
  # TIMED_OUT NOT_FOUND).each do |cmd|
160
160
  # it "should handle #{cmd} messages" do
161
- # conn = EMJack::Connection.new
161
+ # conn = EM::Beanstalk.new
162
162
  #
163
163
  # df = conn.add_deferrable
164
164
  # df.should_receive(:fail).with(cmd.downcase.to_sym)
@@ -168,7 +168,7 @@ describe EMJack::Connection, "integration" do
168
168
  #end
169
169
  #
170
170
  #it 'should handle deleted messages' do
171
- # conn = EMJack::Connection.new
171
+ # conn = EM::Beanstalk.new
172
172
  #
173
173
  # df = conn.add_deferrable
174
174
  # df.should_receive(:succeed)
@@ -177,7 +177,7 @@ describe EMJack::Connection, "integration" do
177
177
  #end
178
178
  #
179
179
  #it 'should handle inserted messages' do
180
- # conn = EMJack::Connection.new
180
+ # conn = EM::Beanstalk.new
181
181
  #
182
182
  # df = conn.add_deferrable
183
183
  # df.should_receive(:succeed).with(40)
@@ -186,7 +186,7 @@ describe EMJack::Connection, "integration" do
186
186
  #end
187
187
  #
188
188
  #it 'should handle buried messages' do
189
- # conn = EMJack::Connection.new
189
+ # conn = EM::Beanstalk.new
190
190
  #
191
191
  # df = conn.add_deferrable
192
192
  # df.should_receive(:fail).with(:buried, 40)
@@ -195,7 +195,7 @@ describe EMJack::Connection, "integration" do
195
195
  #end
196
196
  #
197
197
  #it 'should handle using messages' do
198
- # conn = EMJack::Connection.new
198
+ # conn = EM::Beanstalk.new
199
199
  #
200
200
  # df = conn.add_deferrable
201
201
  # df.should_receive(:succeed).with("mytube")
@@ -204,7 +204,7 @@ describe EMJack::Connection, "integration" do
204
204
  #end
205
205
  #
206
206
  #it 'should handle watching messages' do
207
- # conn = EMJack::Connection.new
207
+ # conn = EM::Beanstalk.new
208
208
  #
209
209
  # df = conn.add_deferrable
210
210
  # df.should_receive(:succeed).with(24)
@@ -213,13 +213,13 @@ describe EMJack::Connection, "integration" do
213
213
  #end
214
214
  #
215
215
  #it 'should handle reserved messages' do
216
- # conn = EMJack::Connection.new
216
+ # conn = EM::Beanstalk.new
217
217
  #
218
218
  # msg = "This is my message"
219
219
  #
220
220
  # df = conn.add_deferrable
221
221
  # df.should_receive(:succeed).with do |job|
222
- # job.class.should == EMJack::Job
222
+ # job.class.should == EM::Beanstalk::Job
223
223
  # job.id.should == 42
224
224
  # job.body.should == msg
225
225
  # end
@@ -228,7 +228,7 @@ describe EMJack::Connection, "integration" do
228
228
  #end
229
229
  #
230
230
  #it 'should handle receiving multiple replies in one packet' do
231
- # conn = EMJack::Connection.new
231
+ # conn = EM::Beanstalk.new
232
232
  #
233
233
  # df = conn.add_deferrable
234
234
  # df.should_receive(:succeed).with(24)
@@ -240,7 +240,7 @@ describe EMJack::Connection, "integration" do
240
240
  #end
241
241
  #
242
242
  #it 'should handle receiving data in chunks' do
243
- # conn = EMJack::Connection.new
243
+ # conn = EM::Beanstalk.new
244
244
  #
245
245
  # msg1 = "First half of the message\r\n"
246
246
  # msg2 = "Last half of the message"
@@ -256,12 +256,12 @@ describe EMJack::Connection, "integration" do
256
256
  #
257
257
  #it 'should send the stat command' do
258
258
  # @connection_mock.should_receive(:send).once.with(:stats)
259
- # conn = EMJack::Connection.new
259
+ # conn = EM::Beanstalk.new
260
260
  # conn.stats
261
261
  #end
262
262
  #
263
263
  #it 'should handle receiving the OK command' do
264
- # conn = EMJack::Connection.new
264
+ # conn = EM::Beanstalk.new
265
265
  #
266
266
  # msg =<<-HERE
267
267
  #---
@@ -289,51 +289,51 @@ describe EMJack::Connection, "integration" do
289
289
  #end
290
290
  #
291
291
  #it 'should support job stats' do
292
- # job = EMJack::Job.new(nil, 42, "blah")
292
+ # job = EM::Beanstalk::Job.new(nil, 42, "blah")
293
293
  #
294
294
  # @connection_mock.should_receive(:send).once.with(:'stats-job', 42)
295
- # conn = EMJack::Connection.new
295
+ # conn = EM::Beanstalk.new
296
296
  # conn.stats(:job, job)
297
297
  #end
298
298
  #
299
299
  #it 'should support tube stats' do
300
300
  # @connection_mock.should_receive(:send).once.with(:'stats-tube', "mytube")
301
- # conn = EMJack::Connection.new
301
+ # conn = EM::Beanstalk.new
302
302
  # conn.stats(:tube, "mytube")
303
303
  #end
304
304
  #
305
305
  #it 'should throw exception on invalid stats command' do
306
306
  # @connection_mock.should_not_receive(:send)
307
- # conn = EMJack::Connection.new
308
- # lambda { conn.stats(:blah) }.should raise_error(EMJack::InvalidCommand)
307
+ # conn = EM::Beanstalk.new
308
+ # lambda { conn.stats(:blah) }.should raise_error(EM::Beanstalk::InvalidCommand)
309
309
  #end
310
310
  #
311
311
  #it 'should support listing tubes' do
312
312
  # @connection_mock.should_receive(:send).once.with(:'list-tubes')
313
- # conn = EMJack::Connection.new
313
+ # conn = EM::Beanstalk.new
314
314
  # conn.list
315
315
  #end
316
316
  #
317
317
  #it 'should support listing tube used' do
318
318
  # @connection_mock.should_receive(:send).once.with(:'list-tube-used')
319
- # conn = EMJack::Connection.new
319
+ # conn = EM::Beanstalk.new
320
320
  # conn.list(:used)
321
321
  #end
322
322
  #
323
323
  #it 'should support listing tubes watched' do
324
324
  # @connection_mock.should_receive(:send).once.with(:'list-tubes-watched')
325
- # conn = EMJack::Connection.new
325
+ # conn = EM::Beanstalk.new
326
326
  # conn.list(:watched)
327
327
  #end
328
328
  #
329
329
  #it 'should throw exception on invalid list command' do
330
330
  # @connection_mock.should_not_receive(:send)
331
- # conn = EMJack::Connection.new
332
- # lambda { conn.list(:blah) }.should raise_error(EMJack::InvalidCommand)
331
+ # conn = EM::Beanstalk.new
332
+ # lambda { conn.list(:blah) }.should raise_error(EM::Beanstalk::InvalidCommand)
333
333
  #end
334
334
  #
335
335
  #it 'should accept a response broken over multiple packets' do
336
- # conn = EMJack::Connection.new
336
+ # conn = EM::Beanstalk.new
337
337
  #
338
338
  # msg1 = "First half of the message\r\n"
339
339
  # msg2 = "Last half of the message"
@@ -349,7 +349,7 @@ describe EMJack::Connection, "integration" do
349
349
  #end
350
350
  #
351
351
  #it 'should accept a response broken over multiple packets' do
352
- # conn = EMJack::Connection.new
352
+ # conn = EM::Beanstalk.new
353
353
  #
354
354
  # msg1 = "First half of the message\r\n"
355
355
  # msg2 = "Last half of the message"
data/spec/job_spec.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
- describe EMJack::Job do
3
+ describe EM::Beanstalk::Job do
4
4
  it 'should convert id to an integer' do
5
- j = EMJack::Job.new(nil, "123", "body")
5
+ j = EM::Beanstalk::Job.new(nil, "123", "body")
6
6
  j.id.should == 123
7
7
  end
8
8
 
9
9
  it 'should fail if id is not an integer' do
10
10
  proc {
11
- j = EMJack::Job.new(nil, "asd", "body")
11
+ j = EM::Beanstalk::Job.new(nil, "asd", "body")
12
12
  }.should raise_error
13
13
  end
14
14
 
@@ -18,7 +18,7 @@ describe EMJack::Job do
18
18
  conn.should_receive(:default_delay)
19
19
  conn.should_receive(:default_ttr)
20
20
 
21
- j = EMJack::Job.new(conn, 1, "body")
21
+ j = EM::Beanstalk::Job.new(conn, 1, "body")
22
22
 
23
23
  conn.should_receive(:delete).with(j)
24
24
 
@@ -31,7 +31,7 @@ describe EMJack::Job do
31
31
  conn.should_receive(:default_delay)
32
32
  conn.should_receive(:default_ttr)
33
33
 
34
- j = EMJack::Job.new(conn, 2, 'body')
34
+ j = EM::Beanstalk::Job.new(conn, 2, 'body')
35
35
  conn.should_receive(:stats).with(:job, j)
36
36
 
37
37
  j.stats
data/spec/spec_helper.rb CHANGED
@@ -1,14 +1,3 @@
1
- libdir = File.expand_path("lib")
2
- $:.unshift(libdir) unless $:.include?(libdir)
3
-
4
- require 'em-jack'
5
- #require 'rubygems'
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'em-beanstalk')
6
2
  require "em-spec/rspec"
7
3
 
8
- #def with_connection(*args)
9
- # EM.run do
10
- # yield EMJack::Connection.new(*args)
11
- # EM.stop
12
- # end
13
-
14
- #end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-beanstalk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-20 00:00:00 -08:00
12
+ date: 2009-12-04 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -35,11 +35,9 @@ files:
35
35
  - README.rdoc
36
36
  - Rakefile
37
37
  - VERSION
38
- - lib/em-jack.rb
39
- - lib/em-jack/beanstalk_connection.rb
40
- - lib/em-jack/connection.rb
41
- - lib/em-jack/errors.rb
42
- - lib/em-jack/job.rb
38
+ - lib/em-beanstalk.rb
39
+ - lib/em-beanstalk/connection.rb
40
+ - lib/em-beanstalk/job.rb
43
41
  - spec/connection_spec.rb
44
42
  - spec/integration_spec.rb
45
43
  - spec/job_spec.rb
data/lib/em-jack.rb DELETED
@@ -1,12 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__))
2
-
3
- require 'em-jack/job'
4
- require 'em-jack/errors'
5
- require 'em-jack/beanstalk_connection'
6
- require 'em-jack/connection'
7
-
8
- module EMJack
9
- module VERSION
10
- STRING = '0.0.3'
11
- end
12
- end
@@ -1,30 +0,0 @@
1
- require 'eventmachine'
2
-
3
- module EMJack
4
- class BeanstalkConnection < EM::Connection
5
- attr_accessor :client
6
-
7
- def connection_completed
8
- @client.connected
9
- end
10
-
11
- def receive_data(data)
12
- @client.received(data)
13
- end
14
-
15
- def send(command, *args)
16
- cmd = command.to_s
17
- cmd << " #{args.join(" ")}" unless args.length == 0
18
- cmd << "\r\n"
19
- send_data(cmd)
20
- end
21
-
22
- def send_with_data(command, data, *args)
23
- send_data("#{command.to_s} #{args.join(" ")}\r\n#{data}\r\n")
24
- end
25
-
26
- def unbind
27
- @client.disconnected
28
- end
29
- end
30
- end
@@ -1,7 +0,0 @@
1
- module EMJack
2
- class Disconnected < RuntimeError
3
- end
4
-
5
- class InvalidCommand < RuntimeError
6
- end
7
- end
data/lib/em-jack/job.rb DELETED
@@ -1,30 +0,0 @@
1
- module EMJack
2
- class Job
3
-
4
- attr_reader :id, :conn
5
- attr_accessor :body, :ttr, :priority, :delay
6
-
7
- def initialize(conn, id, body)
8
- @conn = conn
9
- @id = id && Integer(id)
10
- @body = body
11
- @priority = conn && conn.default_priority
12
- @delay = conn && conn.default_delay
13
- @ttr = conn && conn.default_ttr
14
- end
15
-
16
- alias_method :jobid, :id
17
-
18
- def delete(&block)
19
- conn.delete(self, &block)
20
- end
21
-
22
- def stats(&block)
23
- conn.stats(:job, self, &block)
24
- end
25
-
26
- def to_s
27
- "#{id} -- #{body.inspect}"
28
- end
29
- end
30
- end