From 9c6c9a28976e54e3f84b8acfccea0bbed95fcc5a Mon Sep 17 00:00:00 2001 From: David Wilkie Date: Sat, 6 Jun 2015 08:31:31 +0700 Subject: [PATCH] Complete phone_calls in the background. Closes #180 --- .../phone_call_completions_controller.rb | 2 +- app/jobs/phone_call_completion_job.rb | 7 +++++++ config/secrets.yml | 2 ++ spec/jobs/phone_call_completion_job_spec.rb | 20 +++++++++++++++++++ spec/requests/phone_call_completions_spec.rb | 3 ++- 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 app/jobs/phone_call_completion_job.rb create mode 100644 spec/jobs/phone_call_completion_job_spec.rb diff --git a/app/controllers/phone_call_completions_controller.rb b/app/controllers/phone_call_completions_controller.rb index 3bc0752..603cc9f 100644 --- a/app/controllers/phone_call_completions_controller.rb +++ b/app/controllers/phone_call_completions_controller.rb @@ -4,7 +4,7 @@ class PhoneCallCompletionsController < ApplicationController before_filter :authenticate_phone_call def create - PhoneCall.complete!(params) + PhoneCallCompletionJob.perform_later(params) render(:nothing => true) end diff --git a/app/jobs/phone_call_completion_job.rb b/app/jobs/phone_call_completion_job.rb new file mode 100644 index 0000000..3f43dc8 --- /dev/null +++ b/app/jobs/phone_call_completion_job.rb @@ -0,0 +1,7 @@ +class PhoneCallCompletionJob < ActiveJob::Base + queue_as Rails.application.secrets[:phone_call_completions_queue] + + def perform(params = {}) + PhoneCall.complete!(params) + end +end diff --git a/config/secrets.yml b/config/secrets.yml index 311d597..f994837 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -49,6 +49,7 @@ production: &production message_processor_queue: <%= ENV["MESSAGE_PROCESSOR_QUEUE"] %> msisdn_discovery_queue: <%= ENV["MSISDN_DISCOVERY_QUEUE"] %> phone_call_processor_queue: <%= ENV["PHONE_CALL_PROCESSOR_QUEUE"] %> + phone_call_completion_queue: <%= ENV["PHONE_CALL_COMPLETION_QUEUE"] %> recent_interaction_cutoff_months: <%= ENV["RECENT_INTERACTION_CUTOFF_MONTHS"] %> reminderer_queue: <%= ENV["REMINDERER_QUEUE"] %> report_generator_queue: <%= ENV["REPORT_GENERATOR_QUEUE"] %> @@ -110,6 +111,7 @@ development: &development message_awaiting_parts_timeout: "300" msisdn_discovery_queue: "very_low" phone_call_processor_queue: "critical" + phone_call_completion_queue: "default" recent_interaction_cutoff_months: 1 reminderer_queue: "very_low" report_generator_queue: "high" diff --git a/spec/jobs/phone_call_completion_job_spec.rb b/spec/jobs/phone_call_completion_job_spec.rb new file mode 100644 index 0000000..04086c3 --- /dev/null +++ b/spec/jobs/phone_call_completion_job_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +describe PhoneCallCompletionJob do + describe "#queue_name" do + it { expect(subject.queue_name).to eq(Rails.application.secrets[:phone_call_completion_queue]) } + end + + describe "#perform(params = {})" do + let(:call_params) { { "Foo" => "Bar" } } + + before do + allow(PhoneCall).to receive(:complete!) + end + + it "should tell the phone call to complete itself" do + expect(PhoneCall).to receive(:complete!).with(call_params) + subject.perform(call_params) + end + end +end diff --git a/spec/requests/phone_call_completions_spec.rb b/spec/requests/phone_call_completions_spec.rb index e92ffcf..85462a5 100644 --- a/spec/requests/phone_call_completions_spec.rb +++ b/spec/requests/phone_call_completions_spec.rb @@ -2,13 +2,14 @@ describe "PhoneCallCompletions" do include PhoneCallHelpers + include ActiveJobHelpers describe "POST /phone_call_completions.xml" do let(:phone_call) { create(:phone_call) } let(:call_duration) { 60 } before do - complete_call(:call_sid => phone_call.sid, :call_duration => call_duration) + trigger_job { complete_call(:call_sid => phone_call.sid, :call_duration => call_duration) } phone_call.reload end