beanstalk-client 1.1.0 → 1.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.
data/.gitignore CHANGED
@@ -1,4 +1,3 @@
1
1
  .DS_Store
2
2
  pkg
3
3
  doc
4
- *.gemspec
@@ -0,0 +1,18 @@
1
+ Beanstalk Ruby Client
2
+ =====================
3
+
4
+ Beanstalk is a simple, fast work queue. Its interface is generic, but was
5
+ originally designed for reducing the latency of page views in high-volume web
6
+ applications by running time-consuming tasks asynchronously.
7
+
8
+ For more information, see:
9
+
10
+ - <http://kr.github.com/beanstalkd/>
11
+ - <http://github.com/kr/beanstalkd/raw/master/doc/protocol.txt>
12
+
13
+ ## Contributors
14
+
15
+ - Isaac Feliu
16
+ - Peter Kieltyka
17
+ - Martyn Loughran
18
+ - Dustin Sallings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
@@ -27,6 +27,7 @@ module Beanstalk
27
27
 
28
28
  def initialize(addr, default_tube=nil)
29
29
  @mutex = Mutex.new
30
+ @tube_mutex = Mutex.new
30
31
  @waiting = false
31
32
  @addr = addr
32
33
  connect
@@ -53,8 +54,8 @@ module Beanstalk
53
54
  pri = pri.to_i
54
55
  delay = delay.to_i
55
56
  ttr = ttr.to_i
56
- body = body.to_s # Make sure that body.size gives a useful number
57
- interact("put #{pri} #{delay} #{ttr} #{body.size}\r\n#{body}\r\n",
57
+ body = "#{body}" # Make sure that body.bytesize gives a useful number
58
+ interact("put #{pri} #{delay} #{ttr} #{body.bytesize}\r\n#{body}\r\n",
58
59
  %w(INSERTED BURIED))[0].to_i
59
60
  end
60
61
 
@@ -78,6 +79,14 @@ module Beanstalk
78
79
  interact("peek-buried\r\n", :job)
79
80
  end
80
81
 
82
+ def on_tube(tube, &block)
83
+ @tube_mutex.lock
84
+ use tube
85
+ yield self
86
+ ensure
87
+ @tube_mutex.unlock
88
+ end
89
+
81
90
  def reserve(timeout=nil)
82
91
  raise WaitingForJobError if @waiting
83
92
  @mutex.lock
@@ -289,6 +298,10 @@ module Beanstalk
289
298
  send_to_rand_conn(:yput, obj, pri, delay, ttr)
290
299
  end
291
300
 
301
+ def on_tube(tube, &block)
302
+ send_to_rand_conn(:on_tube, tube, &block)
303
+ end
304
+
292
305
  # Reserve a job from the queue.
293
306
  #
294
307
  # == Parameters
@@ -376,9 +389,9 @@ module Beanstalk
376
389
 
377
390
  private
378
391
 
379
- def call_wrap(c, *args)
392
+ def call_wrap(c, *args, &block)
380
393
  self.last_conn = c
381
- c.send(*args)
394
+ c.send(*args, &block)
382
395
  rescue UnexpectedResponse => ex
383
396
  raise ex
384
397
  rescue EOFError, Errno::ECONNRESET, Errno::EPIPE => ex
@@ -401,9 +414,9 @@ module Beanstalk
401
414
  retry_wrap{open_connections.inject(nil) {|r,c| r or call_wrap(c, *args)}}
402
415
  end
403
416
 
404
- def send_to_rand_conn(*args)
417
+ def send_to_rand_conn(*args, &block)
405
418
  connect()
406
- retry_wrap{call_wrap(pick_connection, *args)}
419
+ retry_wrap{call_wrap(pick_connection, *args, &block)}
407
420
  end
408
421
 
409
422
  def send_to_all_conns(*args)
@@ -4,4 +4,84 @@ class TestBeanstalkClient < Test::Unit::TestCase
4
4
  def test_truth
5
5
  assert true
6
6
  end
7
+
8
+ def setup
9
+ @beanstalk = Beanstalk::Pool.new(['127.0.0.1:11300'])
10
+ @tubes = ['one', 'two']
11
+
12
+ # Put something on each tube so they exist
13
+ @beanstalk.use('one')
14
+ @beanstalk.put('one')
15
+
16
+ @beanstalk.use('two')
17
+ @beanstalk.put('two')
18
+ end
19
+
20
+ def test_not_thread_safe
21
+ # Create threads that will execute
22
+ # A: use one
23
+ # B: use one
24
+ # B: put two
25
+ # A: put one
26
+ a = Thread.new do
27
+ @beanstalk.use('one')
28
+ sleep 4
29
+ @beanstalk.put('one')
30
+ end
31
+
32
+ b = Thread.new do
33
+ sleep 1
34
+ @beanstalk.use('two')
35
+ @beanstalk.put('two')
36
+ end
37
+
38
+ a.join
39
+ b.join
40
+
41
+ one = @beanstalk.stats_tube 'one'
42
+ two = @beanstalk.stats_tube 'two'
43
+
44
+ assert_equal one['current-jobs-ready'], 1
45
+ assert_equal two['current-jobs-ready'], 3
46
+ end
47
+
48
+ def test_thread_safe
49
+ a = Thread.new do
50
+ @beanstalk.on_tube('one') do |conn|
51
+ sleep 4
52
+ conn.put('one')
53
+ end
54
+ end
55
+
56
+ b = Thread.new do
57
+ @beanstalk.on_tube('two') do |conn|
58
+ sleep 1
59
+ conn.put('two')
60
+ end
61
+ end
62
+
63
+ a.join
64
+ b.join
65
+
66
+ one = @beanstalk.stats_tube 'one'
67
+ two = @beanstalk.stats_tube 'two'
68
+
69
+ assert_equal one['current-jobs-ready'], 2
70
+ assert_equal two['current-jobs-ready'], 2
71
+ end
72
+
73
+ def teardown
74
+ # Clear the tubes
75
+ @tubes.each do |tube|
76
+ stats = @beanstalk.stats_tube tube
77
+ num_jobs = stats['current-jobs-ready']
78
+ @beanstalk.watch tube
79
+ num_jobs.times do
80
+ job = @beanstalk.reserve
81
+ job.delete
82
+ end
83
+ @beanstalk.ignore tube
84
+ end
85
+ end
86
+
7
87
  end
metadata CHANGED
@@ -1,37 +1,27 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: beanstalk-client
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 1
9
- - 0
10
- version: 1.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Keith Rarick
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-08-20 00:00:00 -07:00
19
- default_executable:
12
+ date: 2010-08-20 00:00:00.000000000Z
20
13
  dependencies: []
21
-
22
14
  description: Ruby client for beanstalkd
23
15
  email: kr@xph.us
24
16
  executables: []
25
-
26
17
  extensions: []
27
-
28
- extra_rdoc_files:
18
+ extra_rdoc_files:
29
19
  - LICENSE
30
- - README.rdoc
31
- files:
20
+ - README.md
21
+ files:
32
22
  - .gitignore
33
23
  - LICENSE
34
- - README.rdoc
24
+ - README.md
35
25
  - Rakefile
36
26
  - VERSION
37
27
  - lib/beanstalk-client.rb
@@ -45,40 +35,31 @@ files:
45
35
  - website/javascripts/rounded_corners_lite.inc.js
46
36
  - website/stylesheets/screen.css
47
37
  - website/template.rhtml
48
- has_rdoc: true
49
38
  homepage: http://github.com/kr/beanstalk-client-ruby
50
39
  licenses: []
51
-
52
40
  post_install_message:
53
- rdoc_options:
41
+ rdoc_options:
54
42
  - --charset=UTF-8
55
- require_paths:
43
+ require_paths:
56
44
  - lib
57
- required_ruby_version: !ruby/object:Gem::Requirement
45
+ required_ruby_version: !ruby/object:Gem::Requirement
58
46
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- hash: 3
63
- segments:
64
- - 0
65
- version: "0"
66
- required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
52
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
75
57
  requirements: []
76
-
77
58
  rubyforge_project:
78
- rubygems_version: 1.3.7
59
+ rubygems_version: 1.8.10
79
60
  signing_key:
80
61
  specification_version: 3
81
62
  summary: Ruby client for beanstalkd
82
- test_files:
63
+ test_files:
83
64
  - test/helper.rb
84
65
  - test/test_beanstalk-client.rb
@@ -1,20 +0,0 @@
1
- = Beanstalk Ruby Client
2
- By: Keith Rarick
3
-
4
- Beanstalk is a simple, fast workqueue service. Its interface is generic, but was originally designed for reducing the latency of page views in high-volume web applications by running time-consuming tasks asynchronously.
5
-
6
- For more information see:
7
- - http://kr.github.com/beanstalkd/
8
- - http://github.com/kr/beanstalkd/blob/master/doc/protocol.txt?raw=true
9
-
10
- = Notes
11
-
12
- == Multiple Threads
13
-
14
- If you want to use this library from multiple concurrent threads, you should synchronize access to the connection. This library does no internal synchronization.
15
-
16
- = Contributors
17
- - Isaac Feliu
18
- - Peter Kieltyka
19
- - Martyn Loughran
20
- - Dustin Sallings