Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fd32e7

Browse files
author
remi Taylor
authored
Merge pull request GoogleCloudPlatform#74 from GoogleCloudPlatform/fix-book-not-foundw
Fixing worker failure when no book is found
2 parents be561f4 + 06052d6 commit 1fd32e7

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

6-task-queueing/app/jobs/lookup_book_details_job.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,26 @@ class LookupBookDetailsJob < ActiveJob::Base
2020
queue_as :default
2121

2222
def perform book
23-
Rails.logger.info "Lookup details for book #{book.id} #{book.title.inspect}"
23+
Rails.logger.info "[BookService] Lookup details for book" +
24+
"#{book.id} #{book.title.inspect}"
2425

2526
# Create Book API Client
2627
book_service = BooksAPI::BooksService.new
27-
book_service.authorization = nil # Books API does not require authentication
28+
# Books API does not require authentication
29+
book_service.authorization = nil
2830

2931
# Lookup a list of relevant books based on the provided book title.
3032
book_service.list_volumes book.title, order_by: "relevance" do |results, error|
33+
# Error ocurred soft-failure
3134
if error
32-
Rails.logger.error "[BookService] " + error
33-
raise "BookService list_volumes ERROR!"
35+
Rails.logger.error "[BookService] #{error.inspect}"
36+
break
37+
end
38+
39+
# Book was not found
40+
if results.total_items.zero?
41+
Rails.logger.info "[BookService] #{book.title} was not found."
42+
break
3443
end
3544

3645
# List of relevant books
@@ -57,14 +66,15 @@ def perform book
5766
publication_date = Date.parse publication_date
5867

5968
book.author = info.authors.join(", ") unless book.author.present?
60-
book.published_on = publication_date unless book.published_on.present?
61-
book.description = info.description unless book.description.present?
62-
book.image_url = images.try(:thumbnail) unless book.image_url.present?
69+
book.published_on = publication_date unless book.published_on.present?
70+
book.description = info.description unless book.description.present?
71+
book.image_url = images.try(:thumbnail) unless book.image_url.
72+
present?
6373
book.save
6474
end
6575
# [END update_book]
6676

67-
Rails.logger.info "(#{book.id}) Complete"
77+
Rails.logger.info "[BookService] (#{book.id}) Complete"
6878
end
6979
end
7080
end

6-task-queueing/spec/models/book_spec.rb

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,15 @@ def run_enqueued_job! job
5858
expect(job[:job]).to eq LookupBookDetailsJob
5959
expect(job[:args]).to eq [{ "_aj_globalid" => book.to_global_id.to_s }]
6060

61-
# Mock Books API RPC method
62-
book_service = double
63-
64-
# Mock response from call to Books API
65-
book_response = double(
66-
self_link: "https://link/to/book",
67-
volume_info: double(
68-
title: "A Tale of Two Cities",
69-
authors: ["Charles Dickens"],
70-
published_date: "1859",
71-
description: "A Tale of Two Cities is a novel by Charles Dickens.",
72-
image_links: double(thumbnail: "https://path/to/cover/image.png")
73-
)
74-
)
75-
76-
allow(book_service).to receive(:authorization=)
77-
expect(book_service).to receive(:list_volumes).with(
78-
"A Tale of Two Cities", order_by: "relevance"
79-
).and_yield(double(items: [book_response]), nil)
80-
81-
allow(Google::Apis::BooksV1::BooksService).to receive(:new).
82-
and_return book_service
83-
8461
run_enqueued_jobs!
8562

8663
expect(enqueued_jobs).to be_empty
8764

88-
book.reload
65+
book = Book.find book.id
8966
expect(book.title).to eq "A Tale of Two Cities"
9067
expect(book.author).to eq "Charles Dickens"
91-
expect(book.published_on.to_date).to eq Date.parse("1859-01-01")
92-
expect(book.description).to eq "A Tale of Two Cities is a novel by Charles Dickens."
93-
expect(book.image_url).to eq "https://path/to/cover/image.png"
68+
expect(book.description).to include "Charles Dickens' classic novel"
69+
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"
9470
end
9571

9672
it "book details are only looked up when fields are blank"

7-compute-engine/app/jobs/lookup_book_details_job.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@ class LookupBookDetailsJob < ActiveJob::Base
1919
queue_as :default
2020

2121
def perform book
22-
Rails.logger.info "Lookup details for book #{book.id} #{book.title.inspect}"
22+
Rails.logger.info "[BookService] Lookup details for book" +
23+
"#{book.id} #{book.title.inspect}"
2324

2425
# Create Book API Client
2526
book_service = BooksAPI::BooksService.new
26-
book_service.authorization = nil # Books API does not require authentication
27+
# Books API does not require authentication
28+
book_service.authorization = nil
2729

2830
# Lookup a list of relevant books based on the provided book title.
2931
book_service.list_volumes book.title, order_by: "relevance" do |results, error|
32+
# Error ocurred soft-failure
3033
if error
31-
Rails.logger.error "[BookService] " + error
32-
raise "BookService list_volumes ERROR!"
34+
Rails.logger.error "[BookService] #{error.inspect}"
35+
break
36+
end
37+
38+
# Book was not found
39+
if results.total_items.zero?
40+
Rails.logger.info "[BookService] #{book.title} was not found."
41+
break
3342
end
3443

3544
# List of relevant books
@@ -53,13 +62,14 @@ def perform book
5362
publication_date = Date.parse publication_date
5463

5564
book.author = info.authors.join(", ") unless book.author.present?
56-
book.published_on = publication_date unless book.published_on.present?
57-
book.description = info.description unless book.description.present?
58-
book.image_url = images.try(:thumbnail) unless book.image_url.present?
65+
book.published_on = publication_date unless book.published_on.present?
66+
book.description = info.description unless book.description.present?
67+
book.image_url = images.try(:thumbnail) unless book.image_url.
68+
present?
5969
book.save
6070
end
6171

62-
Rails.logger.info "(#{book.id}) Complete"
72+
Rails.logger.info "[BookService] (#{book.id}) Complete"
6373
end
6474
end
6575
end

0 commit comments

Comments
 (0)