Skip to content

Commit

Permalink
added ability to specify a prefix for amqp queues
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederik Fix committed Jun 14, 2009
1 parent c69902d commit 0881786
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/workling/clients/amqp_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@ def connect
raise WorklingError.new("couldn't start amq client. if you're running this in a server environment, then make sure the server is evented (ie use thin or evented mongrel, not normal mongrel.)")
end
end

# no need for explicit closing. when the event loop
# terminates, the connection is closed anyway.
def close; true; end

# subscribe to a queue
def subscribe(key)
@amq.queue(key).subscribe do |value|
@amq.queue(queue_for(key)).subscribe do |value|
data = Marshal.load(value) rescue value
yield data
end
end

# request and retrieve work
def retrieve(key); @amq.queue(key); end
def request(key, value); @amq.queue(key).publish(Marshal.dump(value)); end
def retrieve(key); @amq.queue(queue_for(key)); end
def request(key, value); @amq.queue(queue_for(key)).publish(Marshal.dump(value)); end

private
def queue_for(key)
"#{Workling.config[:prefix]}#{key}"
end
end
end
end
1 change: 1 addition & 0 deletions lib/workling_daemon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def self.parse_workling_options(args)
opts.on('-l', '--load-path LOADPATH', String, "specify the load_path for the workers") { |v| options[:load_path] = v }
opts.on('-f', '--config-path CONFIGPATH', String, "specify the path to the workling.yml file") { |v| options[:config_path] = v }
opts.on('-e', '--environment ENVIRONMENT', String, "specify the environment") { |v| options[:rails_env] = v }
opts.on('-p', '--prefix PREFIX', String, "specify the prefix for queues") { |v| options[:prefix] = v }
end
opts.parse!(partition_options(args).last)
options
Expand Down
41 changes: 41 additions & 0 deletions test/extensions/clients/amqp_client_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require File.dirname(__FILE__) + '/../../test_helper'

context "The memcachequeue client" do
setup do
Workling.config = {}
Workling::Clients::AmqpClient.load
@client = Workling::Clients::AmqpClient.new
@mq = prepare_mq
@client.connect
end

def prepare_mq
AMQP.stubs(:start)
mq = stub(:mq)
MQ.stubs(:new).returns(mq)
mq
end

specify "publishing an item" do
@mq.expects(:queue).with("queue_key").returns(mock(:publish))
@client.request("queue_key", :value)
end

specify "subscribing to a queue" do
@mq.expects(:queue).with("queue_key").returns(mock(:subscribe))
@client.subscribe("queue_key")
end


specify "publishing an item with a prefix" do
Workling.config[:prefix] = "myapp_"
@mq.expects(:queue).with("myapp_queue_key").returns(mock(:publish))
@client.request("queue_key", :value)
end

specify "subscribing to a queue with a prefix" do
Workling.config[:prefix] = "myapp_"
@mq.expects(:queue).with("myapp_queue_key").returns(mock(:subscribe))
@client.subscribe("queue_key")
end
end
8 changes: 8 additions & 0 deletions test/workling/workling_daemon_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
end

context "parsing workling options" do
specify "should parse the -p option" do
WorklingDaemon.parse_workling_options(["--", "-p", "myapp"]).should == {:prefix => "myapp"}
end

specify "should parse the --prefix option" do
WorklingDaemon.parse_workling_options(["--", "--prefix=myapp"]).should == {:prefix => "myapp"}
end

specify "should parse the -c option" do
WorklingDaemon.parse_workling_options(["--", "-c", "memcache"]).should == {:client => "memcache"}
end
Expand Down

0 comments on commit 0881786

Please sign in to comment.