Last active
January 19, 2021 05:47
-
-
Save frankyn/a24b4b9567e1eabb6fab993d0c5f7b0c to your computer and use it in GitHub Desktop.
Patch for Kubernetes Engine tutorial.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/optional-kubernetes-engine/Gemfile b/optional-kubernetes-engine/Gemfile | |
index 33d4baf..aa9f26d 100644 | |
--- a/optional-kubernetes-engine/Gemfile | |
+++ b/optional-kubernetes-engine/Gemfile | |
@@ -19,10 +19,11 @@ gem "fog" | |
gem "omniauth" | |
gem "omniauth-google-oauth2" | |
# [START gcloud] | |
-gem "gcloud", "~> 0.8.0" | |
+gem "google-cloud-datastore" | |
+gem "google-cloud-pubsub" | |
# [END gcloud] | |
# [START google_api_client] | |
-gem "google-api-client" | |
+gem "google-api-client", "0.8" | |
# [END google_api_client] | |
gem "foreman" | |
diff --git a/optional-kubernetes-engine/app/models/book.rb b/optional-kubernetes-engine/app/models/book.rb | |
index 047919d..b1fe80e 100644 | |
--- a/optional-kubernetes-engine/app/models/book.rb | |
+++ b/optional-kubernetes-engine/app/models/book.rb | |
@@ -11,7 +11,7 @@ | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
-require "gcloud/datastore" | |
+require "google/cloud/datastore" | |
class Book | |
include ActiveModel::Model | |
@@ -22,11 +22,11 @@ class Book | |
validates :title, presence: true | |
- # Return a Gcloud::Datastore::Dataset for the configured dataset. | |
+ # Return a Google::Cloud::Datastore::Dataset for the configured dataset. | |
# The dataset is used to create, read, update, and delete entity objects. | |
def self.dataset | |
- @dataset ||= Gcloud.datastore( | |
- Rails.application.config.database_configuration[Rails.env]["dataset_id"] | |
+ @dataset ||= Google::Cloud::Datastore.new( | |
+ project_id: Rails.application.config.database_configuration[Rails.env]["dataset_id"] | |
) | |
end | |
@@ -36,7 +36,7 @@ class Book | |
# that can be used to query for additional results. | |
# [START books_by_creator] | |
def self.query options = {} | |
- query = Gcloud::Datastore::Query.new | |
+ query = Google::Cloud::Datastore::Query.new | |
query.kind "Book" | |
query.limit options[:limit] if options[:limit] | |
query.cursor options[:cursor] if options[:cursor] | |
@@ -67,7 +67,7 @@ class Book | |
# Lookup Book by ID. Returns Book or nil. | |
def self.find id | |
- query = Gcloud::Datastore::Key.new "Book", id.to_i | |
+ query = Google::Cloud::Datastore::Key.new "Book", id.to_i | |
entities = dataset.lookup query | |
from_entity entities.first if entities.any? | |
@@ -77,8 +77,8 @@ class Book | |
singleton_class.send(:alias_method, :find_by_id, :find) | |
def to_entity | |
- entity = Gcloud::Datastore::Entity.new | |
- entity.key = Gcloud::Datastore::Key.new "Book", id | |
+ entity = Google::Cloud::Datastore::Entity.new | |
+ entity.key = Google::Cloud::Datastore::Key.new "Book", id | |
entity["title"] = title | |
entity["author"] = author if author.present? | |
entity["published_on"] = published_on.to_time if published_on.present? | |
@@ -98,7 +98,7 @@ class Book | |
def destroy | |
delete_image if image_url.present? | |
- Book.dataset.delete Gcloud::Datastore::Key.new "Book", id | |
+ Book.dataset.delete Google::Cloud::Datastore::Key.new "Book", id | |
end | |
def persisted? | |
diff --git a/optional-kubernetes-engine/lib/active_job/queue_adapters/pub_sub_queue_adapter.rb b/optional-kubernetes-engine/lib/active_job/queue_adapters/pub_sub_queue_adapter.rb | |
index b4d1b7e..2c27ab0 100644 | |
--- a/optional-kubernetes-engine/lib/active_job/queue_adapters/pub_sub_queue_adapter.rb | |
+++ b/optional-kubernetes-engine/lib/active_job/queue_adapters/pub_sub_queue_adapter.rb | |
@@ -1,5 +1,5 @@ | |
# [START pub_sub_enqueue] | |
-require "gcloud" | |
+require "google/cloud/pubsub" | |
module ActiveJob | |
module QueueAdapters | |
@@ -7,9 +7,7 @@ module ActiveJob | |
def self.pubsub | |
project_id = Rails.application.config.x.settings["project_id"] | |
- gcloud = Gcloud.new project_id | |
- | |
- gcloud.pubsub | |
+ Google::Cloud::Pubsub.new project_id: project_id | |
end | |
def self.enqueue job | |
@@ -31,9 +29,11 @@ module ActiveJob | |
topic = pubsub.topic "lookup_book_details_queue" | |
subscription = topic.subscription "lookup_book_details" | |
- topic.subscribe "lookup_book_details" unless subscription.exists? | |
+ subscription = topic.subscribe "lookup_book_details" if subscription.nil? | |
+ | |
+ subscriber = subscription.listen do |message| | |
+ message.acknowledge! | |
- subscription.listen autoack: true do |message| | |
Rails.logger.info "Book lookup request (#{message.data})" | |
book_id = message.data.to_i | |
@@ -41,6 +41,13 @@ module ActiveJob | |
LookupBookDetailsJob.perform_now book if book | |
end | |
+ | |
+ # Start background threads that will call block passed to listen. | |
+ subscriber.start | |
+ | |
+ loop do | |
+ sleep 10 | |
+ end | |
end | |
# [END pub_sub_worker] | |
diff --git a/optional-kubernetes-engine/structured_data/datastore/app/models/book.rb b/optional-kubernetes-engine/structured_data/datastore/app/models/book.rb | |
index 047919d..b1fe80e 100644 | |
--- a/optional-kubernetes-engine/structured_data/datastore/app/models/book.rb | |
+++ b/optional-kubernetes-engine/structured_data/datastore/app/models/book.rb | |
@@ -11,7 +11,7 @@ | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
-require "gcloud/datastore" | |
+require "google/cloud/datastore" | |
class Book | |
include ActiveModel::Model | |
@@ -22,11 +22,11 @@ class Book | |
validates :title, presence: true | |
- # Return a Gcloud::Datastore::Dataset for the configured dataset. | |
+ # Return a Google::Cloud::Datastore::Dataset for the configured dataset. | |
# The dataset is used to create, read, update, and delete entity objects. | |
def self.dataset | |
- @dataset ||= Gcloud.datastore( | |
- Rails.application.config.database_configuration[Rails.env]["dataset_id"] | |
+ @dataset ||= Google::Cloud::Datastore.new( | |
+ project_id: Rails.application.config.database_configuration[Rails.env]["dataset_id"] | |
) | |
end | |
@@ -36,7 +36,7 @@ class Book | |
# that can be used to query for additional results. | |
# [START books_by_creator] | |
def self.query options = {} | |
- query = Gcloud::Datastore::Query.new | |
+ query = Google::Cloud::Datastore::Query.new | |
query.kind "Book" | |
query.limit options[:limit] if options[:limit] | |
query.cursor options[:cursor] if options[:cursor] | |
@@ -67,7 +67,7 @@ class Book | |
# Lookup Book by ID. Returns Book or nil. | |
def self.find id | |
- query = Gcloud::Datastore::Key.new "Book", id.to_i | |
+ query = Google::Cloud::Datastore::Key.new "Book", id.to_i | |
entities = dataset.lookup query | |
from_entity entities.first if entities.any? | |
@@ -77,8 +77,8 @@ class Book | |
singleton_class.send(:alias_method, :find_by_id, :find) | |
def to_entity | |
- entity = Gcloud::Datastore::Entity.new | |
- entity.key = Gcloud::Datastore::Key.new "Book", id | |
+ entity = Google::Cloud::Datastore::Entity.new | |
+ entity.key = Google::Cloud::Datastore::Key.new "Book", id | |
entity["title"] = title | |
entity["author"] = author if author.present? | |
entity["published_on"] = published_on.to_time if published_on.present? | |
@@ -98,7 +98,7 @@ class Book | |
def destroy | |
delete_image if image_url.present? | |
- Book.dataset.delete Gcloud::Datastore::Key.new "Book", id | |
+ Book.dataset.delete Google::Cloud::Datastore::Key.new "Book", id | |
end | |
def persisted? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment