From e4d62d1818dd399d67f449c82f41913d9ab29c55 Mon Sep 17 00:00:00 2001 From: Justin Aiken <60tonangel@gmail.com> Date: Wed, 24 Sep 2014 17:07:57 -0600 Subject: [PATCH 1/2] Add a .queue_name method to CallQueue... ...we can't use .name, because it conflicts with logger or celluloid or something. --- README.markdown | 2 +- lib/electric_slide/call_queue.rb | 9 ++++- spec/electric_slide/call_queue_spec.rb | 47 ++++++++++++++++++-------- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/README.markdown b/README.markdown index d83fae2..a358c50 100644 --- a/README.markdown +++ b/README.markdown @@ -22,7 +22,7 @@ Example Queue ```Ruby class SupportQueue < ElectricSlide::Queue - name "Support Queue" + queue_name "Support Queue" caller_strategy :fifo agent_strategy :fifo diff --git a/lib/electric_slide/call_queue.rb b/lib/electric_slide/call_queue.rb index 4aa31b9..5370fb9 100644 --- a/lib/electric_slide/call_queue.rb +++ b/lib/electric_slide/call_queue.rb @@ -3,6 +3,12 @@ class ElectricSlide class CallQueue include Celluloid + @queue_name = 'call queue' + + def self.queue_name(queue_name) + @queue_name = queue_name + end + def initialize @free_agents = [] # Needed to keep track of waiting order @agents = [] # Needed to keep track of global list of agents @@ -162,7 +168,8 @@ def calls_waiting end def self.work - self.supervise + self.supervise_as @queue_name + Celluloid::Actor[@queue_name] end end end diff --git a/spec/electric_slide/call_queue_spec.rb b/spec/electric_slide/call_queue_spec.rb index 13df11e..cf7275f 100644 --- a/spec/electric_slide/call_queue_spec.rb +++ b/spec/electric_slide/call_queue_spec.rb @@ -6,25 +6,42 @@ let(:call_a) { dummy_call } let(:call_b) { dummy_call } let(:call_c) { dummy_call } - before :each do - queue.enqueue call_a - queue.enqueue call_b - queue.enqueue call_c - end - it "should return callers in the same order they were enqueued" do - expect(queue.get_next_caller).to be call_a - expect(queue.get_next_caller).to be call_b - expect(queue.get_next_caller).to be call_c + describe ".queue_name" do + it "works as a supervised actor with a name" do + expect(ElectricSlide::CallQueue.work).to eq Celluloid::Actor['call queue'] + end + + context "when given a different name" do + after { ElectricSlide::CallQueue.queue_name 'call queue' } + + it "works as a different actor if given a different name" do + ElectricSlide::CallQueue.queue_name :other_queue + expect(ElectricSlide::CallQueue.work).to eq Celluloid::Actor[:other_queue] + end + end end - it "should return a priority caller ahead of the queue" do - call_d = dummy_call - queue.priority_enqueue call_d - expect(queue.get_next_caller).to be call_d - expect(queue.get_next_caller).to be call_a + describe "#.enqueue" do + before :each do + queue.enqueue call_a + queue.enqueue call_b + queue.enqueue call_c + end + + it "should return callers in the same order they were enqueued" do + expect(queue.get_next_caller).to be call_a + expect(queue.get_next_caller).to be call_b + expect(queue.get_next_caller).to be call_c + end + + it "should return a priority caller ahead of the queue" do + call_d = dummy_call + queue.priority_enqueue call_d + expect(queue.get_next_caller).to be call_d + expect(queue.get_next_caller).to be call_a + end end it "should select the agent that has been waiting the longest" - end From 6e63bfefb4fbfbeb75425b7facbba42676fe25b6 Mon Sep 17 00:00:00 2001 From: Justin Aiken <60tonangel@gmail.com> Date: Tue, 30 Sep 2014 11:02:48 -0600 Subject: [PATCH 2/2] Search Celluloid Actors for a named queue before the array... ...this way, if a CallQueue instance crashes, and gets restarted by it's supervisor, #get_queue will return the restarted queue (or at least a usable proxy to it), instead of returning a reference to a dead, crashed, and burned queue that's dead. --- lib/electric_slide.rb | 5 +++-- spec/electric_slide_spec.rb | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/electric_slide.rb b/lib/electric_slide.rb index a61efed..615bad8 100644 --- a/lib/electric_slide.rb +++ b/lib/electric_slide.rb @@ -26,8 +26,9 @@ def create(name, queue_class = nil, *args) end def get_queue(name) - fail "Queue #{name} not found!" unless @queues.key?(name) - @queues[name] + queue = Celluloid::Actor[name] || @queues[name] + fail "Queue #{name} not found!" unless queue + queue end def shutdown_queue(name) diff --git a/spec/electric_slide_spec.rb b/spec/electric_slide_spec.rb index 5924657..d6418cf 100644 --- a/spec/electric_slide_spec.rb +++ b/spec/electric_slide_spec.rb @@ -38,4 +38,9 @@ expect { ElectricSlide.shutdown_queue("does not exist!") }.to raise_error end + it "gets the celluloid named queue before searching the queues array" do + ElectricSlide.create :foo + expect(Celluloid::Actor).to receive(:[]).with(:foo).and_return :celluloid_queue + expect(ElectricSlide.get_queue :foo).to eq :celluloid_queue + end end