google-subscriber 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.circleci/config.yml +15 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +97 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/google-subscriber.gemspec +31 -0
- data/lib/google-subscriber.rb +19 -0
- data/lib/google_subscriber/base_subscriber.rb +38 -0
- data/lib/google_subscriber/configuration.rb +20 -0
- data/lib/google_subscriber/graceful_shutdown.rb +42 -0
- data/lib/google_subscriber/life_cycle.rb +34 -0
- data/lib/google_subscriber/pub_sub_factory.rb +28 -0
- data/lib/google_subscriber/railtie.rb +12 -0
- data/lib/google_subscriber/subscriber_registry.rb +30 -0
- data/lib/google_subscriber/subscription_starter.rb +47 -0
- data/lib/google_subscriber/version.rb +3 -0
- data/lib/tasks/google_subscriber.rake +8 -0
- metadata +84 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ffd4cd44d38dac2b520c5394441c96ffecd1f7877592debd0140f6cf8ee0b020
|
|
4
|
+
data.tar.gz: 6a4dcd2f68cd95570e5800bcdc8dd0bb2ded72db76ae05d5591742adee9e7266
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1bcbdf4443999ebc511f97b6f74253d86d3c1fa546a92240a6a1a97cc49ff483dd55c4f844fe5ac04fb902ffde6002ff5b473b050eb36abaa1bd6725e8d9c35a
|
|
7
|
+
data.tar.gz: 6538337baea24b97c89a7f596692755bf03ab809c8da188699ab7e27bfb45db08d9b70a8faa1e81b4bdfa1fe1d4211f4bc1423ebb77057c5e947dd9a091dc9c3
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
version: 2.1
|
|
2
|
+
orbs:
|
|
3
|
+
ruby: circleci/ruby@0.1.2
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
build:
|
|
7
|
+
docker:
|
|
8
|
+
- image: circleci/ruby:2.6.3-stretch-node
|
|
9
|
+
executor: ruby/default
|
|
10
|
+
steps:
|
|
11
|
+
- checkout
|
|
12
|
+
- run:
|
|
13
|
+
name: Which bundler?
|
|
14
|
+
command: bundle -v
|
|
15
|
+
- ruby/bundle-install
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
|
File without changes
|
data/Gemfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
source "https://rubygems.org"
|
|
2
|
+
|
|
3
|
+
# Specify your gem's dependencies in google_subscriber-subscriber.gemspec
|
|
4
|
+
gemspec
|
|
5
|
+
|
|
6
|
+
gem "rake", "~> 12.0"
|
|
7
|
+
gem "rspec", "~> 3.0"
|
|
8
|
+
|
|
9
|
+
# gem 'pronto'
|
|
10
|
+
# gem 'pronto-brakeman', require: false
|
|
11
|
+
# gem 'pronto-fasterer', require: false
|
|
12
|
+
# gem 'pronto-flay', require: false
|
|
13
|
+
# gem 'pronto-rails_best_practices', require: false
|
|
14
|
+
# gem 'pronto-rubocop', require: false
|
|
15
|
+
# gem 'colorize'
|
|
16
|
+
gem 'byebug'
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
google-subscriber (0.1.0)
|
|
5
|
+
google-cloud-pubsub
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
addressable (2.7.0)
|
|
11
|
+
public_suffix (>= 2.0.2, < 5.0)
|
|
12
|
+
byebug (11.1.3)
|
|
13
|
+
concurrent-ruby (1.1.8)
|
|
14
|
+
diff-lcs (1.4.4)
|
|
15
|
+
faraday (1.3.0)
|
|
16
|
+
faraday-net_http (~> 1.0)
|
|
17
|
+
multipart-post (>= 1.2, < 3)
|
|
18
|
+
ruby2_keywords
|
|
19
|
+
faraday-net_http (1.0.1)
|
|
20
|
+
gapic-common (0.4.1)
|
|
21
|
+
faraday (~> 1.3)
|
|
22
|
+
google-protobuf (~> 3.15, >= 3.15.2)
|
|
23
|
+
googleapis-common-protos (>= 1.3.11, < 2.0)
|
|
24
|
+
googleapis-common-protos-types (>= 1.0.6, < 2.0)
|
|
25
|
+
googleauth (~> 0.15, >= 0.15.1)
|
|
26
|
+
grpc (~> 1.36)
|
|
27
|
+
google-cloud-core (1.6.0)
|
|
28
|
+
google-cloud-env (~> 1.0)
|
|
29
|
+
google-cloud-errors (~> 1.0)
|
|
30
|
+
google-cloud-env (1.5.0)
|
|
31
|
+
faraday (>= 0.17.3, < 2.0)
|
|
32
|
+
google-cloud-errors (1.1.0)
|
|
33
|
+
google-cloud-pubsub (2.5.0)
|
|
34
|
+
concurrent-ruby (~> 1.1)
|
|
35
|
+
google-cloud-core (~> 1.5)
|
|
36
|
+
google-cloud-pubsub-v1 (~> 0.0)
|
|
37
|
+
google-cloud-pubsub-v1 (0.4.0)
|
|
38
|
+
gapic-common (~> 0.3)
|
|
39
|
+
google-cloud-errors (~> 1.0)
|
|
40
|
+
grpc-google-iam-v1 (>= 0.6.10, < 2.0)
|
|
41
|
+
google-protobuf (3.15.8)
|
|
42
|
+
googleapis-common-protos (1.3.11)
|
|
43
|
+
google-protobuf (~> 3.14)
|
|
44
|
+
googleapis-common-protos-types (>= 1.0.6, < 2.0)
|
|
45
|
+
grpc (~> 1.27)
|
|
46
|
+
googleapis-common-protos-types (1.0.6)
|
|
47
|
+
google-protobuf (~> 3.14)
|
|
48
|
+
googleauth (0.16.1)
|
|
49
|
+
faraday (>= 0.17.3, < 2.0)
|
|
50
|
+
jwt (>= 1.4, < 3.0)
|
|
51
|
+
memoist (~> 0.16)
|
|
52
|
+
multi_json (~> 1.11)
|
|
53
|
+
os (>= 0.9, < 2.0)
|
|
54
|
+
signet (~> 0.14)
|
|
55
|
+
grpc (1.37.0)
|
|
56
|
+
google-protobuf (~> 3.15)
|
|
57
|
+
googleapis-common-protos-types (~> 1.0)
|
|
58
|
+
grpc-google-iam-v1 (0.6.11)
|
|
59
|
+
google-protobuf (~> 3.14)
|
|
60
|
+
googleapis-common-protos (>= 1.3.11, < 2.0)
|
|
61
|
+
grpc (~> 1.27)
|
|
62
|
+
jwt (2.2.2)
|
|
63
|
+
memoist (0.16.2)
|
|
64
|
+
multi_json (1.15.0)
|
|
65
|
+
multipart-post (2.1.1)
|
|
66
|
+
os (1.1.1)
|
|
67
|
+
public_suffix (4.0.6)
|
|
68
|
+
rake (12.3.3)
|
|
69
|
+
rspec (3.10.0)
|
|
70
|
+
rspec-core (~> 3.10.0)
|
|
71
|
+
rspec-expectations (~> 3.10.0)
|
|
72
|
+
rspec-mocks (~> 3.10.0)
|
|
73
|
+
rspec-core (3.10.1)
|
|
74
|
+
rspec-support (~> 3.10.0)
|
|
75
|
+
rspec-expectations (3.10.1)
|
|
76
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
77
|
+
rspec-support (~> 3.10.0)
|
|
78
|
+
rspec-mocks (3.10.2)
|
|
79
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
80
|
+
rspec-support (~> 3.10.0)
|
|
81
|
+
rspec-support (3.10.2)
|
|
82
|
+
ruby2_keywords (0.0.4)
|
|
83
|
+
signet (0.15.0)
|
|
84
|
+
addressable (~> 2.3)
|
|
85
|
+
faraday (>= 0.17.3, < 2.0)
|
|
86
|
+
jwt (>= 1.5, < 3.0)
|
|
87
|
+
multi_json (~> 1.10)
|
|
88
|
+
|
|
89
|
+
PLATFORMS
|
|
90
|
+
ruby
|
|
91
|
+
|
|
92
|
+
DEPENDENCIES
|
|
93
|
+
byebug
|
|
94
|
+
google-subscriber!
|
|
95
|
+
rake (~> 12.0)
|
|
96
|
+
rspec (~> 3.0)
|
|
97
|
+
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Perry Hertler
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# GoogleSubscriber
|
|
2
|
+
|
|
3
|
+
GoogleSubscribers discovers your ruby google pub/sub subscribers and provides a rake task for starting them
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'google-subscriber'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install google-subscriber
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Add a google_subscriber.rb file to in your `Rails.root/config/initializers/` directory
|
|
24
|
+
```ruby
|
|
25
|
+
require 'google_subscriber'
|
|
26
|
+
GoogleSubscriber.configure do |config|
|
|
27
|
+
config.subscriber_paths += %W( #{Rails.root}/app/subscribers )
|
|
28
|
+
config.logger = Rails.logger
|
|
29
|
+
# https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-ruby
|
|
30
|
+
config.google_credentials = '/path/to/cred/file.json' # or File.read('/path/to/cred/file.json')
|
|
31
|
+
config.google_project_id = 'my-gcp-project-id'
|
|
32
|
+
end
|
|
33
|
+
```
|
|
34
|
+
#### Subscribers
|
|
35
|
+
1. Add Subscribers to Rails.root/app/subscribers
|
|
36
|
+
1. Subscribers should subclass `GoogleSubscriber::BaseSubscriber`
|
|
37
|
+
|
|
38
|
+
Example:
|
|
39
|
+
```ruby
|
|
40
|
+
class FooSubscriber < GoogleSubscriber::BaseSubscriber
|
|
41
|
+
subscription_id 'my-subscription-id' # https://cloud.google.com/pubsub/docs/pull#ruby
|
|
42
|
+
subscription_listen_args({ threads: { callback: 16 } }) # https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub/Subscription.html#listen-instance_method
|
|
43
|
+
|
|
44
|
+
# optionally override config.google_credentials
|
|
45
|
+
# subscription_credentials '/path/to/cred/file.json' # or File.read('/path/to/cred/file.json')
|
|
46
|
+
|
|
47
|
+
# optionally override config.subscription_project_id
|
|
48
|
+
# subscription_project_id 'my-gcp-project-id'
|
|
49
|
+
|
|
50
|
+
# @param [Class: Google::Cloud::PubSub::ReceivedMessage] received_message The received_message
|
|
51
|
+
def on_received_message(received_message)
|
|
52
|
+
# do something exciting with received_message
|
|
53
|
+
message.acknowledge!
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
If the log-level is set to `DEBUG`, log messages such as "the service ws unable to fulfill your request" could show up. It seems
|
|
59
|
+
this is an internal transient error. Subsequent retries succeed.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## Development
|
|
63
|
+
|
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
65
|
+
|
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
67
|
+
|
|
68
|
+
## Contributing
|
|
69
|
+
|
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/perryqh/google-subscriber-ruby
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "google_subscriber/subscriber"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require_relative 'lib/google_subscriber/version'
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "google-subscriber"
|
|
5
|
+
spec.version = GoogleSubscriber::VERSION
|
|
6
|
+
spec.authors = ["Perry Hertler"]
|
|
7
|
+
spec.email = ["perry@hertler.org"]
|
|
8
|
+
|
|
9
|
+
spec.summary = %q{Provides rake task to startup google subscribers.}
|
|
10
|
+
spec.description = spec.description
|
|
11
|
+
spec.homepage = "https://github.com/perryqh/google-subscriber-ruby."
|
|
12
|
+
spec.license = "MIT"
|
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
|
14
|
+
|
|
15
|
+
# spec.metadata["allowed_push_host"] = 'http://rubygems.org'
|
|
16
|
+
|
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
18
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/perryqh/google-subscriber-ruby/blob/master/CHANGELOG.md"
|
|
20
|
+
|
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
25
|
+
end
|
|
26
|
+
spec.bindir = "exe"
|
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
28
|
+
spec.require_paths = ["lib"]
|
|
29
|
+
|
|
30
|
+
spec.add_runtime_dependency 'google-cloud-pubsub'
|
|
31
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'google_subscriber/version'
|
|
2
|
+
require 'google_subscriber/configuration'
|
|
3
|
+
require 'google_subscriber/life_cycle'
|
|
4
|
+
require 'google_subscriber/subscription_starter'
|
|
5
|
+
require 'google_subscriber/graceful_shutdown'
|
|
6
|
+
require 'google_subscriber/subscriber_registry'
|
|
7
|
+
require 'google_subscriber/base_subscriber'
|
|
8
|
+
require 'google_subscriber/pub_sub_factory'
|
|
9
|
+
require 'google_subscriber/railtie' if defined?(Rails)
|
|
10
|
+
|
|
11
|
+
module GoogleSubscriber
|
|
12
|
+
extend Configuration
|
|
13
|
+
extend SubscriberRegistry
|
|
14
|
+
extend LifeCycle
|
|
15
|
+
|
|
16
|
+
def self.configure
|
|
17
|
+
yield self if block_given?
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module GoogleSubscriber
|
|
2
|
+
class BaseSubscriber
|
|
3
|
+
extend SubscriptionStarter
|
|
4
|
+
|
|
5
|
+
# Subclasses must override this method or method(s) of the form `on_received_#{message.data_event}_message`.
|
|
6
|
+
# It is called as messages are received
|
|
7
|
+
# Remember to acknowledge the `received_message` https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub/ReceivedMessage.html
|
|
8
|
+
#
|
|
9
|
+
# @param [Class: Google::Cloud::PubSub::ReceivedMessage] _received_message The received_message
|
|
10
|
+
def on_received_message(_received_message)
|
|
11
|
+
raise 'must override!'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
attr_reader :g_subscription_id, :g_credentials, :g_project_id, :g_subscription_listen_args
|
|
16
|
+
|
|
17
|
+
# Macro for specifying the subscription_id
|
|
18
|
+
# @param [String] subscription_id The subscription-id
|
|
19
|
+
def subscription_id(subscription_id)
|
|
20
|
+
@g_subscription_id = subscription_id
|
|
21
|
+
GoogleSubscriber.register_subscriber(self)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def subscription_credentials(creds)
|
|
25
|
+
@g_credentials = creds
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def subscription_project_id(project_id)
|
|
29
|
+
@g_project_id = project_id
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# https://rubydoc.info/gems/google-cloud-pubsub/2.5.0/Google/Cloud/PubSub/Subscription#listen-instance_method
|
|
33
|
+
def subscription_listen_args(args)
|
|
34
|
+
@g_subscription_listen_args = args
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'logger'
|
|
2
|
+
|
|
3
|
+
module GoogleSubscriber
|
|
4
|
+
module Configuration
|
|
5
|
+
attr_writer :logger
|
|
6
|
+
attr_accessor :google_credentials, :google_project_id, :subscription_listen_args
|
|
7
|
+
|
|
8
|
+
# Returns the logger
|
|
9
|
+
# You can define the logger when you configure +GoogleSubscriber+ if you want to use a different logger than the default Ruby Logger.
|
|
10
|
+
#
|
|
11
|
+
# @see GoogleSubscriber.configure
|
|
12
|
+
def logger
|
|
13
|
+
@logger ||= ::Logger.new(STDOUT)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
module GRPC
|
|
19
|
+
extend GoogleSubscriber::Configuration
|
|
20
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module GoogleSubscriber
|
|
2
|
+
class GracefulShutdown
|
|
3
|
+
DEFAULT_SIGNALS = %w(INT TERM)
|
|
4
|
+
|
|
5
|
+
class << self
|
|
6
|
+
def handle_signals(*signals)
|
|
7
|
+
signals = DEFAULT_SIGNALS if signals.empty?
|
|
8
|
+
handlers = setup(signals)
|
|
9
|
+
yield if block_given?
|
|
10
|
+
teardown(handlers)
|
|
11
|
+
rescue Shutdown
|
|
12
|
+
exit
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def setup(signals)
|
|
18
|
+
signals.each_with_object({}) do |signal, handlers|
|
|
19
|
+
handlers[signal] = trap(signal) do
|
|
20
|
+
raise Shutdown
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def teardown(handlers)
|
|
26
|
+
handlers.each do |signal, handler|
|
|
27
|
+
trap(signal, handler)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Shutdown < RuntimeError
|
|
34
|
+
def continue
|
|
35
|
+
raise self
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def ignore
|
|
39
|
+
# No-op, provided for clarity.
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module GoogleSubscriber
|
|
2
|
+
module LifeCycle
|
|
3
|
+
def boot
|
|
4
|
+
GoogleSubscriber.logger.info('booting subscribers')
|
|
5
|
+
load_subscribers
|
|
6
|
+
active_subscribers = start_subscribers
|
|
7
|
+
|
|
8
|
+
GracefulShutdown.handle_signals do
|
|
9
|
+
begin
|
|
10
|
+
sleep
|
|
11
|
+
rescue Shutdown => e
|
|
12
|
+
GoogleSubscriber.shutdown(active_subscribers)
|
|
13
|
+
e.continue
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def shutdown(active_subscribers)
|
|
19
|
+
GoogleSubscriber.logger.info('shutting down subscribers')
|
|
20
|
+
return if active_subscribers.nil?
|
|
21
|
+
|
|
22
|
+
active_subscribers.each do |sub|
|
|
23
|
+
begin
|
|
24
|
+
sub.stop.wait!
|
|
25
|
+
rescue => e
|
|
26
|
+
# swallow error so the remaining active_subscribers can be shut down
|
|
27
|
+
GoogleSubscriber.logger.error("Error shutting down #{sub}: #{e.message}")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
GoogleSubscriber.logger.info('stopped subscriber(s)!')
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require "google/cloud/pubsub"
|
|
2
|
+
|
|
3
|
+
module GoogleSubscriber
|
|
4
|
+
module PubSubFactory
|
|
5
|
+
def self.new_pub_sub(project_id:, credentials:)
|
|
6
|
+
project_id = GoogleSubscriber.google_project_id if blank?(project_id)
|
|
7
|
+
credentials = GoogleSubscriber.google_credentials if blank?(credentials)
|
|
8
|
+
raise ArgumentError, 'subscription_project_id is required!' if blank?(project_id)
|
|
9
|
+
raise ArgumentError, 'subscription_credentials are required!' if blank?(credentials)
|
|
10
|
+
|
|
11
|
+
Google::Cloud::PubSub.new(project_id: project_id,
|
|
12
|
+
credentials: build_credentials(credentials))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.blank?(str)
|
|
16
|
+
str.nil? || str.empty?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# If creds are a file-path, just return them. If they're JSON, convert to a hash
|
|
20
|
+
#
|
|
21
|
+
# @param [String] creds The path to credentials JSON file or the actual JSON
|
|
22
|
+
def self.build_credentials(credentials)
|
|
23
|
+
JSON.parse(credentials)
|
|
24
|
+
rescue JSON::ParserError
|
|
25
|
+
credentials
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module GoogleSubscriber
|
|
4
|
+
module SubscriberRegistry
|
|
5
|
+
attr_writer :subscriber_paths
|
|
6
|
+
|
|
7
|
+
def subscriber_paths
|
|
8
|
+
@subscriber_paths ||= []
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def load_subscribers
|
|
12
|
+
subscriber_paths.each do |dir|
|
|
13
|
+
Dir[File.join(dir, '*.rb')].each { |f| require f }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def registry
|
|
18
|
+
@registry ||= []
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def register_subscriber(klass)
|
|
22
|
+
GoogleSubscriber.logger.info("[#{name}#register_subscriber] Registered subscriber '#{klass.name}'")
|
|
23
|
+
registry << klass unless registry.include?(klass)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def start_subscribers
|
|
27
|
+
registry.collect(&:start)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module GoogleSubscriber
|
|
2
|
+
module SubscriptionStarter
|
|
3
|
+
DEFAULT_SUBSCRIPTION_LISTEN_ARGS = {}.freeze
|
|
4
|
+
|
|
5
|
+
# Listens for new messages associated with the subscriber name
|
|
6
|
+
# As messages come in, `on_received_message` is called, which must be overridden on subclasses
|
|
7
|
+
def start
|
|
8
|
+
subscriber = new
|
|
9
|
+
pub_sub_subscriber = create_subscription.listen(build_listen_args) do |received_message|
|
|
10
|
+
on_received_wrapper do
|
|
11
|
+
subscriber.on_received_message(received_message)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
pub_sub_subscriber.start
|
|
16
|
+
GoogleSubscriber.logger.info("[#{name}.start] Started subscriber with subscription '#{g_subscription_id}'")
|
|
17
|
+
|
|
18
|
+
pub_sub_subscriber
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def on_received_wrapper
|
|
22
|
+
if defined?(Rails)
|
|
23
|
+
# https://guides.rubyonrails.org/threading_and_code_execution.html#reloader
|
|
24
|
+
Rails.application.reloader.wrap do
|
|
25
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
|
26
|
+
yield
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
else
|
|
30
|
+
yield
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def build_listen_args
|
|
35
|
+
DEFAULT_SUBSCRIPTION_LISTEN_ARGS
|
|
36
|
+
.merge(GoogleSubscriber.subscription_listen_args || {})
|
|
37
|
+
.merge(g_subscription_listen_args || {})
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def create_subscription
|
|
41
|
+
raise ArgumentError, 'subscription_id is required!' if g_subscription_id.nil?
|
|
42
|
+
|
|
43
|
+
GoogleSubscriber::PubSubFactory.new_pub_sub(project_id: g_project_id, credentials: g_credentials)
|
|
44
|
+
.subscription(g_subscription_id&.to_s)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: google-subscriber
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Perry Hertler
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-04-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: google-cloud-pubsub
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0'
|
|
27
|
+
description: ''
|
|
28
|
+
email:
|
|
29
|
+
- perry@hertler.org
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- ".circleci/config.yml"
|
|
35
|
+
- ".gitignore"
|
|
36
|
+
- ".rspec"
|
|
37
|
+
- ".travis.yml"
|
|
38
|
+
- CHANGELOG.md
|
|
39
|
+
- Gemfile
|
|
40
|
+
- Gemfile.lock
|
|
41
|
+
- LICENSE.txt
|
|
42
|
+
- README.md
|
|
43
|
+
- Rakefile
|
|
44
|
+
- bin/console
|
|
45
|
+
- bin/setup
|
|
46
|
+
- google-subscriber.gemspec
|
|
47
|
+
- lib/google-subscriber.rb
|
|
48
|
+
- lib/google_subscriber/base_subscriber.rb
|
|
49
|
+
- lib/google_subscriber/configuration.rb
|
|
50
|
+
- lib/google_subscriber/graceful_shutdown.rb
|
|
51
|
+
- lib/google_subscriber/life_cycle.rb
|
|
52
|
+
- lib/google_subscriber/pub_sub_factory.rb
|
|
53
|
+
- lib/google_subscriber/railtie.rb
|
|
54
|
+
- lib/google_subscriber/subscriber_registry.rb
|
|
55
|
+
- lib/google_subscriber/subscription_starter.rb
|
|
56
|
+
- lib/google_subscriber/version.rb
|
|
57
|
+
- lib/tasks/google_subscriber.rake
|
|
58
|
+
homepage: https://github.com/perryqh/google-subscriber-ruby.
|
|
59
|
+
licenses:
|
|
60
|
+
- MIT
|
|
61
|
+
metadata:
|
|
62
|
+
homepage_uri: https://github.com/perryqh/google-subscriber-ruby.
|
|
63
|
+
source_code_uri: https://github.com/perryqh/google-subscriber-ruby.
|
|
64
|
+
changelog_uri: https://github.com/perryqh/google-subscriber-ruby/blob/master/CHANGELOG.md
|
|
65
|
+
post_install_message:
|
|
66
|
+
rdoc_options: []
|
|
67
|
+
require_paths:
|
|
68
|
+
- lib
|
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: 2.3.0
|
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0'
|
|
79
|
+
requirements: []
|
|
80
|
+
rubygems_version: 3.1.4
|
|
81
|
+
signing_key:
|
|
82
|
+
specification_version: 4
|
|
83
|
+
summary: Provides rake task to startup google subscribers.
|
|
84
|
+
test_files: []
|