From 6edbe0ecd1f8ec6b633ed5371a2df25a539ba545 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 7 Oct 2009 23:13:52 -0400 Subject: [PATCH] renaming due to jack name conflict --- README.rdoc | 4 +- Rakefile | 6 +- jack.gemspec => em-jack.gemspec | 12 +- lib/em-jack.rb | 12 ++ lib/{jack => em-jack}/beanstalk_connection.rb | 2 +- lib/{jack => em-jack}/connection.rb | 12 +- lib/{jack => em-jack}/errors.rb | 2 +- lib/{jack => em-jack}/job.rb | 2 +- lib/jack.rb | 12 -- shell.rb | 8 +- spec/connection_spec.rb | 104 +++++++++--------- spec/job_spec.rb | 10 +- 12 files changed, 93 insertions(+), 93 deletions(-) rename jack.gemspec => em-jack.gemspec (54%) create mode 100644 lib/em-jack.rb rename lib/{jack => em-jack}/beanstalk_connection.rb (97%) rename lib/{jack => em-jack}/connection.rb (94%) rename lib/{jack => em-jack}/errors.rb (83%) rename lib/{jack => em-jack}/job.rb (95%) delete mode 100644 lib/jack.rb diff --git a/README.rdoc b/README.rdoc index e6bcfbd..227a3e0 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,4 +1,4 @@ -= Jack += EMJack An attempt to wrap portions of the Beanstalk protocol with EventMachine. Every command will return a deferrable object. That object will succeed or fail depending on the reply returned from Beanstalk. @@ -18,7 +18,7 @@ are no available jobs. = Examples EM.run { - jack = Jack::Connection.new + jack = EMJack::Connection.new r = jack.use('mytube') r.callback { |tube| puts "Using #{tube}" } diff --git a/Rakefile b/Rakefile index fc7a1b4..d068197 100644 --- a/Rakefile +++ b/Rakefile @@ -2,15 +2,15 @@ require 'rake' require 'rake/rdoctask' require 'rake/gempackagetask' -spec = eval(File.read(File.join(File.dirname(__FILE__), "jack.gemspec"))) +spec = eval(File.read(File.join(File.dirname(__FILE__), "em-jack.gemspec"))) task :default => :gem -desc 'Generate RDoc documentation for Jack' +desc 'Generate RDoc documentation for EMJack' Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('README.rdoc', 'COPYING', 'lib/**/*.rb') rdoc.main = 'README.rdoc' - rdoc.title = 'Jack Documentation' + rdoc.title = 'EM Jack Documentation' rdoc.rdoc_dir = 'doc' rdoc.options << '--line-numbers' diff --git a/jack.gemspec b/em-jack.gemspec similarity index 54% rename from jack.gemspec rename to em-jack.gemspec index eeee0af..e5f6b2f 100644 --- a/jack.gemspec +++ b/em-jack.gemspec @@ -1,9 +1,9 @@ Gem::Specification.new do |s| - s.name = %q{jack} - s.version = "0.0.2" + s.name = %q{em-jack} + s.version = "0.0.3" s.authors = ["dan sinclair"] s.email = %q{dj2@everburning.com} - s.homepage = %q{http://http://github.com/dj2/jack/} + s.homepage = %q{http://http://github.com/dj2/em-jack/} s.summary = %q{An evented Beanstalk client.} s.description = %q{An evented Beanstalk client.} @@ -12,10 +12,10 @@ Gem::Specification.new do |s| s.has_rdoc = true s.extra_rdoc_files = ['README.rdoc'] - s.rdoc_options << '--title' << 'Jack Documentation' << + s.rdoc_options << '--title' << 'EM-Jack Documentation' << '--main' << 'README.rdoc' << '--line-numbers' - s.files = %w(README.rdoc COPYING lib/jack.rb lib/jack/beanstalk_connection.rb - lib/jack/connection.rb lib/jack/errors.rb lib/jack/job.rb) + s.files = %w(README.rdoc COPYING lib/em-jack.rb lib/em-jack/beanstalk_connection.rb + lib/em-jack/connection.rb lib/em-jack/errors.rb lib/em-jack/job.rb) end diff --git a/lib/em-jack.rb b/lib/em-jack.rb new file mode 100644 index 0000000..ff42276 --- /dev/null +++ b/lib/em-jack.rb @@ -0,0 +1,12 @@ +$:.unshift(File.dirname(__FILE__)) + +require 'em-jack/job' +require 'em-jack/errors' +require 'em-jack/beanstalk_connection' +require 'em-jack/connection' + +module EMJack + module VERSION + STRING = '0.0.3' + end +end diff --git a/lib/jack/beanstalk_connection.rb b/lib/em-jack/beanstalk_connection.rb similarity index 97% rename from lib/jack/beanstalk_connection.rb rename to lib/em-jack/beanstalk_connection.rb index 0988040..b5c10f5 100644 --- a/lib/jack/beanstalk_connection.rb +++ b/lib/em-jack/beanstalk_connection.rb @@ -1,6 +1,6 @@ require 'eventmachine' -module Jack +module EMJack class BeanstalkConnection < EM::Connection attr_accessor :client diff --git a/lib/jack/connection.rb b/lib/em-jack/connection.rb similarity index 94% rename from lib/jack/connection.rb rename to lib/em-jack/connection.rb index c92a99e..e106706 100644 --- a/lib/jack/connection.rb +++ b/lib/em-jack/connection.rb @@ -1,7 +1,7 @@ require 'eventmachine' require 'yaml' -module Jack +module EMJack class Connection RETRY_COUNT = 5 @@ -20,7 +20,7 @@ def initialize(opts = {}) @in_reserve = false @deferrables = [] - @conn = EM::connect(host, port, Jack::BeanstalkConnection) do |conn| + @conn = EM::connect(host, port, EMJack::BeanstalkConnection) do |conn| conn.client = self end @@ -61,7 +61,7 @@ def stats(type = nil, val = nil) when nil then @conn.send(:stats) when :tube then @conn.send(:'stats-tube', val) when :job then @conn.send(:'stats-job', val.jobid) - else raise Jack::InvalidCommand.new + else raise EMJack::InvalidCommand.new end add_deferrable end @@ -71,7 +71,7 @@ def list(type = nil) when nil then @conn.send(:'list-tubes') when :used then @conn.send(:'list-tube-used') when :watched then @conn.send(:'list-tubes-watched') - else raise Jack::InvalidCommand.new + else raise EMJack::InvalidCommand.new end add_deferrable end @@ -110,7 +110,7 @@ def disconnected # XXX I think I need to run out the deferrables as failed here # since the connection was dropped - raise Jack::Disconnected if @retries >= RETRY_COUNT + raise EMJack::Disconnected if @retries >= RETRY_COUNT @retries += 1 EM.add_timer(1) { @conn.reconnect(@host, @port) } end @@ -185,7 +185,7 @@ def received(data) break if body.nil? df = @deferrables.shift - job = Jack::Job.new(self, id, body) + job = EMJack::Job.new(self, id, body) df.succeed(job) next diff --git a/lib/jack/errors.rb b/lib/em-jack/errors.rb similarity index 83% rename from lib/jack/errors.rb rename to lib/em-jack/errors.rb index 4165b7a..c6c04f1 100644 --- a/lib/jack/errors.rb +++ b/lib/em-jack/errors.rb @@ -1,4 +1,4 @@ -module Jack +module EMJack class Disconnected < RuntimeError end diff --git a/lib/jack/job.rb b/lib/em-jack/job.rb similarity index 95% rename from lib/jack/job.rb rename to lib/em-jack/job.rb index 129d8ee..b82f255 100644 --- a/lib/jack/job.rb +++ b/lib/em-jack/job.rb @@ -1,4 +1,4 @@ -module Jack +module EMJack class Job attr_accessor :jobid, :body, :ttr, :conn diff --git a/lib/jack.rb b/lib/jack.rb deleted file mode 100644 index 247cd4a..0000000 --- a/lib/jack.rb +++ /dev/null @@ -1,12 +0,0 @@ -$:.unshift(File.dirname(__FILE__)) - -require 'jack/job' -require 'jack/errors' -require 'jack/beanstalk_connection' -require 'jack/connection' - -module Jack - module VERSION - STRING = '0.0.2' - end -end diff --git a/shell.rb b/shell.rb index fca4f17..8aa444f 100644 --- a/shell.rb +++ b/shell.rb @@ -2,7 +2,7 @@ require 'rubygems' require 'eventmachine' -require 'jack' +require 'em-jack' require 'pp' $stdout.sync = true @@ -11,7 +11,7 @@ class KeyboardHandler < EM::Connection include EM::Protocols::LineText2 def post_init - @jack = Jack::Connection.new + @jack = EMJack::Connection.new print "> " end @@ -45,7 +45,7 @@ def receive_line(line) when /^delete / then id = line.gsub(/delete /, '').to_i - job = Jack::Job.new(@jack, id, "asdf") + job = EMJack::Job.new(@jack, id, "asdf") df = job.delete df.callback { puts "Deleted" } df @@ -81,7 +81,7 @@ def receive_line(line) df when /^stats-job\s+(\d+)/ then - j = Jack::Job.new(@jack, $1, "blah") + j = EMJack::Job.new(@jack, $1, "blah") df = j.stats df.callback { |stats| pp stats } df diff --git a/spec/connection_spec.rb b/spec/connection_spec.rb index 762427f..decb225 100644 --- a/spec/connection_spec.rb +++ b/spec/connection_spec.rb @@ -3,52 +3,52 @@ require 'rubygems' require 'spec' -require 'jack' +require 'em-jack' -describe Jack::Connection do +describe EMJack::Connection do before(:each) do @connection_mock = mock(:conn) EM.should_receive(:connect).and_return(@connection_mock) end it 'should use a default host of "localhost"' do - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.host.should == 'localhost' end it 'should use a default port of 11300' do - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.port.should == 11300 end it 'should watch and use a provided tube on connect' do @connection_mock.should_receive(:send).once.with(:use, "mytube") @connection_mock.should_receive(:send).once.with(:watch, "mytube") - conn = Jack::Connection.new(:tube => "mytube") + conn = EMJack::Connection.new(:tube => "mytube") end it 'should send the "use" command' do @connection_mock.should_receive(:send).once.with(:use, "mytube") - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.use("mytube") end it 'should not send the use command to the currently used tube' do @connection_mock.should_receive(:send).once.with(:use, "mytube") - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.use("mytube") conn.use("mytube") end it 'should send the "watch" command' do @connection_mock.should_receive(:send).once.with(:watch, "mytube") - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.watch("mytube") end it 'should not send the watch command for a tube currently watched' do @connection_mock.should_receive(:send).once.with(:watch, "mytube") - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.watch("mytube") conn.watch("mytube") end @@ -57,70 +57,70 @@ msg = "my message" @connection_mock.should_receive(:send_with_data).once. with(:put, msg, anything, anything, anything, msg.length) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put(msg) end it 'should default the delay, priority and ttr settings' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, 65536, 0, 300, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg") end it 'should accept a delay setting' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, anything, 42, anything, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :delay => 42) end it 'should accept a ttr setting' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, anything, anything, 999, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :ttr => 999) end it 'should accept a priority setting' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, 233, anything, anything, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :priority => 233) end it 'shoudl accept a priority, delay and ttr setting' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, 99, 42, 2000, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :priority => 99, :delay => 42, :ttr => 2000) end it 'should force delay to be >= 0' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, anything, 0, anything, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :delay => -42) end it 'should force ttr to be >= 0' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, anything, anything, 300, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :ttr => -42) end it 'should force priority to be >= 0' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, 65536, anything, anything, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :priority => -42) end it 'should force priority to be < 2**32' do @connection_mock.should_receive(:send_with_data).once. with(:put, anything, (2 ** 32), anything, anything, anything) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put("msg", :priority => (2 ** 32 + 1)) end @@ -128,51 +128,51 @@ msg = 22 @connection_mock.should_receive(:send_with_data).once. with(:put, msg.to_s, anything, anything, anything, msg.to_s.length) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.put(msg) end it 'should send the "delete" command' do @connection_mock.should_receive(:send).once.with(:delete, 1) - job = Jack::Job.new(nil, 1, "body") - conn = Jack::Connection.new + job = EMJack::Job.new(nil, 1, "body") + conn = EMJack::Connection.new conn.delete(job) end it 'should handle a nil job sent to the "delete" command' do @connection_mock.should_not_receive(:send).with(:delete, nil) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.delete(nil) end it 'should send the "reserve" command' do @connection_mock.should_receive(:send).with(:reserve) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.reserve end it 'should raise exception if reconnect fails more then RETRY_COUNT times' do EM.should_receive(:add_timer).exactly(5).times - conn = Jack::Connection.new + conn = EMJack::Connection.new 5.times { conn.disconnected } - lambda { conn.disconnected }.should raise_error(Jack::Disconnected) + lambda { conn.disconnected }.should raise_error(EMJack::Disconnected) end it 'should reset the retry count on connection' do EM.should_receive(:add_timer).at_least(1).times - conn = Jack::Connection.new + conn = EMJack::Connection.new 5.times { conn.disconnected } conn.connected - lambda { conn.disconnected }.should_not raise_error(Jack::Disconnected) + lambda { conn.disconnected }.should_not raise_error(EMJack::Disconnected) end %w(OUT_OF_MEMORY INTERNAL_ERROR DRAINING BAD_FORMAT UNKNOWN_COMMAND EXPECTED_CRLF JOB_TOO_BIG DEADLINE_SOON TIMED_OUT NOT_FOUND).each do |cmd| it 'should handle #{cmd} messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:fail).with(cmd.downcase.to_sym) @@ -182,7 +182,7 @@ end it 'should handle deleted messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:succeed) @@ -191,7 +191,7 @@ end it 'should handle inserted messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:succeed).with(40) @@ -200,7 +200,7 @@ end it 'should handle buried messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:fail).with(:buried, 40) @@ -209,7 +209,7 @@ end it 'should handle using messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:succeed).with("mytube") @@ -218,7 +218,7 @@ end it 'should handle watching messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:succeed).with(24) @@ -227,13 +227,13 @@ end it 'should handle reserved messages' do - conn = Jack::Connection.new + conn = EMJack::Connection.new msg = "This is my message" df = conn.add_deferrable df.should_receive(:succeed).with do |job| - job.class.should == Jack::Job + job.class.should == EMJack::Job job.jobid.should == 42 job.body.should == msg end @@ -242,7 +242,7 @@ end it 'should handle receiving multiple replies in one packet' do - conn = Jack::Connection.new + conn = EMJack::Connection.new df = conn.add_deferrable df.should_receive(:succeed).with(24) @@ -254,7 +254,7 @@ end it 'should handle receiving data in chunks' do - conn = Jack::Connection.new + conn = EMJack::Connection.new msg1 = "First half of the message\r\n" msg2 = "Last half of the message" @@ -270,12 +270,12 @@ it 'should send the stat command' do @connection_mock.should_receive(:send).once.with(:stats) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.stats end it 'should handle receiving the OK command' do - conn = Jack::Connection.new + conn = EMJack::Connection.new msg =<<-HERE --- @@ -303,51 +303,51 @@ end it 'should support job stats' do - job = Jack::Job.new(nil, 42, "blah") + job = EMJack::Job.new(nil, 42, "blah") @connection_mock.should_receive(:send).once.with(:'stats-job', 42) - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.stats(:job, job) end it 'should support tube stats' do @connection_mock.should_receive(:send).once.with(:'stats-tube', "mytube") - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.stats(:tube, "mytube") end it 'should throw exception on invalid stats command' do @connection_mock.should_not_receive(:send) - conn = Jack::Connection.new - lambda { conn.stats(:blah) }.should raise_error(Jack::InvalidCommand) + conn = EMJack::Connection.new + lambda { conn.stats(:blah) }.should raise_error(EMJack::InvalidCommand) end it 'should support listing tubes' do @connection_mock.should_receive(:send).once.with(:'list-tubes') - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.list end it 'should support listing tube used' do @connection_mock.should_receive(:send).once.with(:'list-tube-used') - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.list(:used) end it 'should support listing tubes watched' do @connection_mock.should_receive(:send).once.with(:'list-tubes-watched') - conn = Jack::Connection.new + conn = EMJack::Connection.new conn.list(:watched) end it 'should throw exception on invalid list command' do @connection_mock.should_not_receive(:send) - conn = Jack::Connection.new - lambda { conn.list(:blah) }.should raise_error(Jack::InvalidCommand) + conn = EMJack::Connection.new + lambda { conn.list(:blah) }.should raise_error(EMJack::InvalidCommand) end it 'should accept a response broken over multiple packets' do - conn = Jack::Connection.new + conn = EMJack::Connection.new msg1 = "First half of the message\r\n" msg2 = "Last half of the message" @@ -363,7 +363,7 @@ end it 'should accept a response broken over multiple packets' do - conn = Jack::Connection.new + conn = EMJack::Connection.new msg1 = "First half of the message\r\n" msg2 = "Last half of the message" diff --git a/spec/job_spec.rb b/spec/job_spec.rb index 3bf6d97..9397470 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -2,18 +2,18 @@ require 'rubygems' require 'spec' -require 'jack' +require 'em-jack' -describe Jack::Job do +describe EMJack::Job do it 'should convert jobid to an integer' do - j = Jack::Job.new(nil, "1", "body") + j = EMJack::Job.new(nil, "1", "body") j.jobid.class.should == Fixnum end it 'should send a delete command to the connection' do conn = mock(:conn) - j = Jack::Job.new(conn, 1, "body") + j = EMJack::Job.new(conn, 1, "body") conn.should_receive(:delete).with(j) j.delete @@ -22,7 +22,7 @@ it 'should send a stats command to the connection' do conn = mock(:conn) - j = Jack::Job.new(conn, 2, 'body') + j = EMJack::Job.new(conn, 2, 'body') conn.should_receive(:stats).with(:job, j) j.stats