Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #74 from GoogleCloudPlatform/fix-book-not-foundw
Browse files Browse the repository at this point in the history
Fixing worker failure when no book is found
  • Loading branch information
remi Taylor authored Dec 5, 2016
2 parents be561f4 + 06052d6 commit 1fd32e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
26 changes: 18 additions & 8 deletions 6-task-queueing/app/jobs/lookup_book_details_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 3 additions & 27 deletions 6-task-queueing/spec/models/book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(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"
Expand Down
26 changes: 18 additions & 8 deletions 7-compute-engine/app/jobs/lookup_book_details_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,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
Expand All @@ -53,13 +62,14 @@ 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

Rails.logger.info "(#{book.id}) Complete"
Rails.logger.info "[BookService] (#{book.id}) Complete"
end
end
end

0 comments on commit 1fd32e7

Please sign in to comment.