From 3227d11f90b9186931cafc78947989cff49dc5b8 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 29 Nov 2016 14:55:36 -0800 Subject: [PATCH 1/4] Fixing worker failure when no book is found --- .../app/jobs/lookup_book_details_job.rb | 26 +++++++++++++------ 6-task-queueing/spec/models/book_spec.rb | 4 +-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/6-task-queueing/app/jobs/lookup_book_details_job.rb b/6-task-queueing/app/jobs/lookup_book_details_job.rb index eaf03541..b897c1e9 100644 --- a/6-task-queueing/app/jobs/lookup_book_details_job.rb +++ b/6-task-queueing/app/jobs/lookup_book_details_job.rb @@ -20,17 +20,26 @@ class LookupBookDetailsJob < ActiveJob::Base queue_as :default def perform book - Rails.logger.info "Lookup details for book #{book.id} #{book.title.inspect}" + Rails.logger.info "[BookService] Lookup details for book" + + "#{book.id} #{book.title.inspect}" # Create Book API Client book_service = BooksAPI::BooksService.new - book_service.authorization = nil # Books API does not require authentication + # Books API does not require authentication + book_service.authorization = nil # Lookup a list of relevant books based on the provided book title. book_service.list_volumes book.title, order_by: "relevance" do |results, error| + # Error ocurred soft-failure if error - Rails.logger.error "[BookService] " + error - raise "BookService list_volumes ERROR!" + Rails.logger.error "[BookService] #{error.inspect}" + break + end + + # Book was not found + if results.total_items.zero? + Rails.logger.info "[BookService] #{book.title} was not found." + break end # List of relevant books @@ -57,14 +66,15 @@ def perform book publication_date = Date.parse publication_date book.author = info.authors.join(", ") unless book.author.present? - book.published_on = publication_date unless book.published_on.present? - book.description = info.description unless book.description.present? - book.image_url = images.try(:thumbnail) unless book.image_url.present? + book.published_on = publication_date unless book.published_on.present? + book.description = info.description unless book.description.present? + book.image_url = images.try(:thumbnail) unless book.image_url. + present? book.save end # [END update_book] - Rails.logger.info "(#{book.id}) Complete" + Rails.logger.info "[BookService] (#{book.id}) Complete" end end end diff --git a/6-task-queueing/spec/models/book_spec.rb b/6-task-queueing/spec/models/book_spec.rb index 61420d2d..cd4b7a71 100644 --- a/6-task-queueing/spec/models/book_spec.rb +++ b/6-task-queueing/spec/models/book_spec.rb @@ -76,10 +76,10 @@ def run_enqueued_job! job allow(book_service).to receive(:authorization=) expect(book_service).to receive(:list_volumes).with( "A Tale of Two Cities", order_by: "relevance" - ).and_yield(double(items: [book_response]), nil) + ).and_yield(double(total_items: 1, items: [book_response]), nil) allow(Google::Apis::BooksV1::BooksService).to receive(:new). - and_return book_service + and_return book_service run_enqueued_jobs! From 9b048ee9121c3f8ef9296fe45d8db93e10b3c0c8 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 29 Nov 2016 15:46:04 -0800 Subject: [PATCH 2/4] Updated step 6 book_spec.rb to reflect step 7 --- 6-task-queueing/spec/models/book_spec.rb | 30 +++--------------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/6-task-queueing/spec/models/book_spec.rb b/6-task-queueing/spec/models/book_spec.rb index cd4b7a71..a0da1335 100644 --- a/6-task-queueing/spec/models/book_spec.rb +++ b/6-task-queueing/spec/models/book_spec.rb @@ -58,39 +58,15 @@ def run_enqueued_job! job expect(job[:job]).to eq LookupBookDetailsJob expect(job[:args]).to eq [{ "_aj_globalid" => book.to_global_id.to_s }] - # Mock Books API RPC method - book_service = double - - # Mock response from call to Books API - book_response = double( - self_link: "https://link/to/book", - volume_info: double( - title: "A Tale of Two Cities", - authors: ["Charles Dickens"], - published_date: "1859", - description: "A Tale of Two Cities is a novel by Charles Dickens.", - image_links: double(thumbnail: "https://path/to/cover/image.png") - ) - ) - - allow(book_service).to receive(:authorization=) - expect(book_service).to receive(:list_volumes).with( - "A Tale of Two Cities", order_by: "relevance" - ).and_yield(double(total_items: 1, items: [book_response]), nil) - - allow(Google::Apis::BooksV1::BooksService).to receive(:new). - and_return book_service - run_enqueued_jobs! expect(enqueued_jobs).to be_empty - book.reload + book = Book.find book.id expect(book.title).to eq "A Tale of Two Cities" expect(book.author).to eq "Charles Dickens" - expect(book.published_on.to_date).to eq Date.parse("1859-01-01") - expect(book.description).to eq "A Tale of Two Cities is a novel by Charles Dickens." - expect(book.image_url).to eq "https://path/to/cover/image.png" + expect(book.description).to include "Charles Dickens' classic novel" + expect(book.image_url).to eq "http://books.google.com/books/content?id=5EIPAAAAQAAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api" end it "book details are only looked up when fields are blank" From a3b929e763a6bfc02b8e05843f7265769927492f Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 29 Nov 2016 15:47:24 -0800 Subject: [PATCH 3/4] Updating book lookup in step 7 to reflect step 6 --- .../app/jobs/lookup_book_details_job.rb | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/7-compute-engine/app/jobs/lookup_book_details_job.rb b/7-compute-engine/app/jobs/lookup_book_details_job.rb index 617c7f66..b897c1e9 100644 --- a/7-compute-engine/app/jobs/lookup_book_details_job.rb +++ b/7-compute-engine/app/jobs/lookup_book_details_job.rb @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +# [START lookup_books] require "google/apis/books_v1" BooksAPI = Google::Apis::BooksV1 @@ -19,22 +20,32 @@ class LookupBookDetailsJob < ActiveJob::Base queue_as :default def perform book - Rails.logger.info "Lookup details for book #{book.id} #{book.title.inspect}" + Rails.logger.info "[BookService] Lookup details for book" + + "#{book.id} #{book.title.inspect}" # Create Book API Client book_service = BooksAPI::BooksService.new - book_service.authorization = nil # Books API does not require authentication + # Books API does not require authentication + book_service.authorization = nil # Lookup a list of relevant books based on the provided book title. book_service.list_volumes book.title, order_by: "relevance" do |results, error| + # Error ocurred soft-failure if error - Rails.logger.error "[BookService] " + error - raise "BookService list_volumes ERROR!" + Rails.logger.error "[BookService] #{error.inspect}" + break + end + + # Book was not found + if results.total_items.zero? + Rails.logger.info "[BookService] #{book.title} was not found." + break end # List of relevant books volumes = results.items - +# [END lookup_books] + # [START choose_volume] # To provide the best results, find the first returned book that # includes title and author information as well as a book cover image. best_match = volumes.find {|volume| @@ -43,7 +54,9 @@ def perform book } volume = best_match || volumes.first + # [END choose_volume] + # [START update_book] if volume info = volume.volume_info images = info.image_links @@ -53,13 +66,16 @@ def perform book publication_date = Date.parse publication_date book.author = info.authors.join(", ") unless book.author.present? - book.published_on = publication_date unless book.published_on.present? - book.description = info.description unless book.description.present? - book.image_url = images.try(:thumbnail) unless book.image_url.present? + book.published_on = publication_date unless book.published_on.present? + book.description = info.description unless book.description.present? + book.image_url = images.try(:thumbnail) unless book.image_url. + present? book.save end + # [END update_book] - Rails.logger.info "(#{book.id}) Complete" + Rails.logger.info "[BookService] (#{book.id}) Complete" end end end +# [END book_lookup] From 06052d6ca33e62a23f6102938a0a422ae788de34 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Tue, 29 Nov 2016 15:49:38 -0800 Subject: [PATCH 4/4] Removed region tags. --- 7-compute-engine/app/jobs/lookup_book_details_job.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/7-compute-engine/app/jobs/lookup_book_details_job.rb b/7-compute-engine/app/jobs/lookup_book_details_job.rb index b897c1e9..82a54027 100644 --- a/7-compute-engine/app/jobs/lookup_book_details_job.rb +++ b/7-compute-engine/app/jobs/lookup_book_details_job.rb @@ -11,7 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# [START lookup_books] require "google/apis/books_v1" BooksAPI = Google::Apis::BooksV1 @@ -44,8 +43,7 @@ def perform book # List of relevant books volumes = results.items -# [END lookup_books] - # [START choose_volume] + # To provide the best results, find the first returned book that # includes title and author information as well as a book cover image. best_match = volumes.find {|volume| @@ -54,9 +52,7 @@ def perform book } volume = best_match || volumes.first - # [END choose_volume] - # [START update_book] if volume info = volume.volume_info images = info.image_links @@ -72,10 +68,8 @@ def perform book present? book.save end - # [END update_book] Rails.logger.info "[BookService] (#{book.id}) Complete" end end end -# [END book_lookup]