hanami-events-cloud_pubsub 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +10 -1
- data/README.md +24 -3
- data/examples/server.rb +6 -9
- data/hanami-events-cloud_pubsub.gemspec +1 -0
- data/lib/hanami/events/cloud_pubsub.rb +28 -0
- data/lib/hanami/events/cloud_pubsub/cli.rb +12 -26
- data/lib/hanami/events/cloud_pubsub/listener.rb +11 -5
- data/lib/hanami/events/cloud_pubsub/safe_error_handler.rb +18 -0
- data/lib/hanami/events/cloud_pubsub/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ba6ce6fe08440e7ac49969a63b2b6f4e2412c24a3fa45effbf11563f8c6f5cd
|
4
|
+
data.tar.gz: c072c528e2379cb1a32a1e7aef86858315e3638c856488e4967cdbe62696312f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b76340517e3a21af94fc94deb6a2dd0cf93ca46c86a285e64b7ed516b3501ab355fc22cfae7b9a8ef9085909fc454583ced8ae16e666f8a2cd5200d931cad362
|
7
|
+
data.tar.gz: acdce37077630472a14d0a2b4c7179a4535e72a5f7c0a890752cc525909d4d91b6ce3d56a705459d57d4bd6ee48571b2c25cfe177e5b819c7219bb1010d2f619
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -4,10 +4,20 @@ cache: bundler
|
|
4
4
|
services:
|
5
5
|
- docker
|
6
6
|
language: ruby
|
7
|
+
env:
|
8
|
+
global:
|
9
|
+
- CC_TEST_REPORTER_ID=7ab310950456b913cd7e947f6671e3e9c16822f33bd8c572dadcc14593fe4fb5
|
10
|
+
- COVERAGE=true
|
7
11
|
rvm:
|
8
12
|
- 2.4
|
9
13
|
before_install:
|
10
14
|
- gem install bundler -v 1.16.1
|
15
|
+
before_script:
|
16
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
17
|
+
- chmod +x ./cc-test-reporter
|
18
|
+
- ./cc-test-reporter before-build
|
19
|
+
after_script:
|
20
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
11
21
|
after_failure:
|
12
22
|
- cat log/test.log
|
13
23
|
deploy:
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -9,7 +9,8 @@ GIT
|
|
9
9
|
PATH
|
10
10
|
remote: .
|
11
11
|
specs:
|
12
|
-
hanami-events-cloud_pubsub (0.
|
12
|
+
hanami-events-cloud_pubsub (0.2.0)
|
13
|
+
dry-configurable
|
13
14
|
google-cloud-pubsub (~> 0.30.2)
|
14
15
|
hanami-cli
|
15
16
|
hanami-events
|
@@ -23,6 +24,7 @@ GEM
|
|
23
24
|
coderay (1.1.2)
|
24
25
|
concurrent-ruby (1.0.5)
|
25
26
|
diff-lcs (1.3)
|
27
|
+
docile (1.3.1)
|
26
28
|
dry-configurable (0.7.0)
|
27
29
|
concurrent-ruby (~> 1.0)
|
28
30
|
dry-container (0.6.0)
|
@@ -74,6 +76,7 @@ GEM
|
|
74
76
|
concurrent-ruby (~> 1.0)
|
75
77
|
transproc (~> 1.0)
|
76
78
|
jaro_winkler (1.4.0)
|
79
|
+
json (2.1.0)
|
77
80
|
jwt (2.1.0)
|
78
81
|
little-plugger (1.1.4)
|
79
82
|
logging (2.2.2)
|
@@ -122,6 +125,11 @@ GEM
|
|
122
125
|
faraday (~> 0.9)
|
123
126
|
jwt (>= 1.5, < 3.0)
|
124
127
|
multi_json (~> 1.10)
|
128
|
+
simplecov (0.16.1)
|
129
|
+
docile (~> 1.1)
|
130
|
+
json (>= 1.8, < 3)
|
131
|
+
simplecov-html (~> 0.10.0)
|
132
|
+
simplecov-html (0.10.2)
|
125
133
|
transproc (1.0.2)
|
126
134
|
unicode-display_width (1.4.0)
|
127
135
|
|
@@ -136,6 +144,7 @@ DEPENDENCIES
|
|
136
144
|
rake (~> 12.3)
|
137
145
|
rspec (~> 3.0)
|
138
146
|
rubocop
|
147
|
+
simplecov
|
139
148
|
|
140
149
|
BUNDLED WITH
|
141
150
|
1.16.1
|
data/README.md
CHANGED
@@ -52,19 +52,40 @@ First, create a config file:
|
|
52
52
|
require 'config/environment'
|
53
53
|
|
54
54
|
Hanami.boot
|
55
|
+
|
56
|
+
Hanami::Events::CloudPubsub.configure do |config|
|
57
|
+
# required
|
58
|
+
config.subscriptions_loader = -> do
|
59
|
+
# Ensure these files are not loaded until *after* the subscriptions are
|
60
|
+
# setup, or else you will have an undefined reference to `$events`
|
61
|
+
Hanami::Utils.require! 'apps/web/subscriptions'
|
62
|
+
end
|
63
|
+
|
64
|
+
# (optional)
|
65
|
+
config.logger = Hanami.logger
|
66
|
+
|
67
|
+
# (optional)
|
68
|
+
config.error_handlers << lambda do |err, message|
|
69
|
+
Honeybadger.notify(err, context: message.attributes)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
55
73
|
```
|
56
74
|
|
57
75
|
Then, run the worker process:
|
58
76
|
|
59
77
|
```sh
|
60
|
-
bundle exec
|
78
|
+
bundle exec cloudpubsub run
|
61
79
|
```
|
62
80
|
|
81
|
+
# Testing
|
82
|
+
|
63
83
|
If you would like to use an emulator process for testing:
|
64
84
|
|
65
85
|
```sh
|
66
|
-
|
67
|
-
|
86
|
+
$(gcloud beta emulators pubsub env-init)
|
87
|
+
gcloud beta emulators pubsub start
|
88
|
+
bundle exec cloudpubsub run --emulator
|
68
89
|
```
|
69
90
|
|
70
91
|
## Development
|
data/examples/server.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
$events.subscribe('user.deleted', id: 'testing-2') do |payload|
|
11
|
-
puts "Deleted2 user: #{payload}"
|
3
|
+
Hanami::Events::CloudPubsub.configure do |config|
|
4
|
+
config.subscriptions_loader = -> do
|
5
|
+
$events.subscribe('user.deleted', id: 'testing-2') do |payload|
|
6
|
+
puts "Deleted2 user: #{payload}"
|
7
|
+
end
|
8
|
+
end
|
12
9
|
end
|
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
+
spec.add_dependency 'dry-configurable'
|
25
26
|
spec.add_dependency 'google-cloud-pubsub', '~> 0.30.2'
|
26
27
|
spec.add_dependency 'hanami-cli'
|
27
28
|
spec.add_dependency 'hanami-events'
|
@@ -5,11 +5,39 @@ require 'hanami/events/cloud_pubsub/version'
|
|
5
5
|
require 'hanami/events/cloud_pubsub/runner'
|
6
6
|
require 'hanami/events/cloud_pubsub/mixin'
|
7
7
|
require 'google/cloud/pubsub'
|
8
|
+
require 'dry-configurable'
|
8
9
|
|
9
10
|
module Hanami
|
10
11
|
module Events
|
11
12
|
# CloudPubsub
|
12
13
|
module CloudPubsub
|
14
|
+
extend Dry::Configurable
|
15
|
+
|
16
|
+
setting :logger, Logger.new(STDOUT), reader: true
|
17
|
+
setting :subscriptions_loader, proc {
|
18
|
+
abort <<~MSG
|
19
|
+
┌────────────────────────────────────────────────────────────────────────────────┐
|
20
|
+
│ You must configure subscriptions_loader param in order to be able to subscribe │
|
21
|
+
│ to events. When the worker is setup and ready to subscribe to events, this │
|
22
|
+
│ loader will be called. It must respond to `#call`. │
|
23
|
+
│ │
|
24
|
+
│ Example │
|
25
|
+
│ ═══════ │
|
26
|
+
│ │
|
27
|
+
│ Hanami::Events::CloudPubsub.configure do │config│ │
|
28
|
+
│ config.subscriptions_loader = proc do │
|
29
|
+
│ Hanami::Utils.require! 'apps/web/subscriptions' │
|
30
|
+
│ end │
|
31
|
+
│ end │
|
32
|
+
└────────────────────────────────────────────────────────────────────────────────┘
|
33
|
+
MSG
|
34
|
+
}, reader: true
|
35
|
+
setting :error_handlers, [
|
36
|
+
->(err, msg) do
|
37
|
+
logger.error "Message(#{msg.message_id}) failed with exception #{err.inspect}"
|
38
|
+
end
|
39
|
+
], reader: true
|
40
|
+
|
13
41
|
def self.setup
|
14
42
|
Hanami::Events::Adapter.register(:cloud_pubsub) do
|
15
43
|
require_relative 'adapter/cloud_pubsub'
|
@@ -26,36 +26,25 @@ module Hanami
|
|
26
26
|
def call(opts)
|
27
27
|
setup_env(opts)
|
28
28
|
parse_opts(opts)
|
29
|
+
load_config
|
29
30
|
build_runner
|
31
|
+
load_subscriptions
|
30
32
|
setup_signal_handlers
|
31
|
-
load_config
|
32
33
|
start_runner
|
33
34
|
sleep_forever
|
34
35
|
end
|
35
36
|
|
36
37
|
private
|
37
38
|
|
39
|
+
def load_subscriptions
|
40
|
+
CloudPubsub.subscriptions_loader.call
|
41
|
+
end
|
42
|
+
|
38
43
|
def setup_env(opts)
|
39
44
|
ENV['PUBSUB_EMULATOR_HOST'] ||= 'localhost:8085' if opts[:emulator]
|
40
|
-
try_load_environment
|
41
|
-
resolve_components
|
42
45
|
CloudPubsub.setup
|
43
46
|
end
|
44
47
|
|
45
|
-
def try_load_environment
|
46
|
-
boot_file = Bundler.default_gemfile.parent.join('config', 'environment.rb')
|
47
|
-
load boot_file.to_s
|
48
|
-
rescue LoadError
|
49
|
-
logger.warn <<~MSG
|
50
|
-
Could not load config/environment.rb, assuming we are not in a Hanami project
|
51
|
-
MSG
|
52
|
-
end
|
53
|
-
|
54
|
-
def resolve_components
|
55
|
-
return unless defined?(Hanami::Components)
|
56
|
-
Hanami::Components.resolve('logger')
|
57
|
-
end
|
58
|
-
|
59
48
|
def sleep_forever
|
60
49
|
thread = Thread.new do
|
61
50
|
loop do
|
@@ -71,19 +60,20 @@ module Hanami
|
|
71
60
|
end
|
72
61
|
|
73
62
|
def load_config
|
74
|
-
load @config
|
63
|
+
load String(@config)
|
64
|
+
rescue LoadError
|
65
|
+
logger.warn "No config file found (tried #{@config}), using default"
|
75
66
|
end
|
76
67
|
|
77
68
|
def start_runner
|
78
|
-
logger.
|
69
|
+
logger.debug 'Running in emulator mode' if @emulator
|
70
|
+
logger.info "Starting worker (pid: #{Process.pid})"
|
79
71
|
@runner.start
|
80
72
|
end
|
81
73
|
|
82
74
|
def parse_opts(opts)
|
83
75
|
@emulator = opts[:emulator]
|
84
|
-
logger.info 'Running in emulator mode' if @emulator
|
85
76
|
@config = opts[:config]
|
86
|
-
logger.debug "Using config file: #{@config}"
|
87
77
|
end
|
88
78
|
|
89
79
|
def build_runner
|
@@ -100,11 +90,7 @@ module Hanami
|
|
100
90
|
end
|
101
91
|
|
102
92
|
def logger
|
103
|
-
|
104
|
-
Hanami.logger
|
105
|
-
else
|
106
|
-
Logger.new(STDOUT)
|
107
|
-
end
|
93
|
+
CloudPubsub.logger
|
108
94
|
end
|
109
95
|
|
110
96
|
def setup_signal_handlers
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'hanami/events/cloud_pubsub/safe_error_handler'
|
4
|
+
|
3
5
|
module Hanami
|
4
6
|
module Events
|
5
7
|
module CloudPubsub
|
@@ -70,17 +72,15 @@ module Hanami
|
|
70
72
|
|
71
73
|
#:reek:TooManyStatements
|
72
74
|
#:reek:DuplicateMethodCall
|
73
|
-
# rubocop:disable Metrics/MethodLength
|
75
|
+
# rubocop:disable Metrics/MethodLength
|
74
76
|
def run_handler(message)
|
75
|
-
id = message.message_id
|
76
77
|
succeeded = false
|
77
78
|
failed = false
|
78
|
-
|
79
79
|
handler.call(message)
|
80
80
|
succeeded = true
|
81
81
|
rescue Exception => err # rubocop:disable all
|
82
82
|
failed = true
|
83
|
-
|
83
|
+
run_error_handlers(err, message)
|
84
84
|
raise err
|
85
85
|
ensure
|
86
86
|
id = message.message_id
|
@@ -92,7 +92,7 @@ module Hanami
|
|
92
92
|
logger.warn "Message(#{id}) was terminated from outside, rescheduling"
|
93
93
|
end
|
94
94
|
end
|
95
|
-
# rubocop:enable Metrics/MethodLength
|
95
|
+
# rubocop:enable Metrics/MethodLength
|
96
96
|
|
97
97
|
def subscription_for(name)
|
98
98
|
topic.find_subscription(name) || topic.create_subscription(name)
|
@@ -101,6 +101,12 @@ module Hanami
|
|
101
101
|
def ensure_subscriber!
|
102
102
|
raise NoSubscriberError, 'No subsriber has been registered' unless @subscriber
|
103
103
|
end
|
104
|
+
|
105
|
+
def run_error_handlers(err, message)
|
106
|
+
CloudPubsub.error_handlers.each do |handler|
|
107
|
+
SafeErrorHandler.call(handler, err, message)
|
108
|
+
end
|
109
|
+
end
|
104
110
|
end
|
105
111
|
end
|
106
112
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Hanami
|
4
|
+
module Events
|
5
|
+
module CloudPubsub
|
6
|
+
# Safely run error_handlers
|
7
|
+
module SafeErrorHandler
|
8
|
+
def self.call(handler, err, message)
|
9
|
+
handler.call(err, message)
|
10
|
+
rescue StandardError => ex
|
11
|
+
CloudPubsub.logger.error '!!! ERROR HANDLER THREW AN ERROR !!!'
|
12
|
+
CloudPubsub.logger.error ex
|
13
|
+
CloudPubsub.logger.error ex.backtrace.join("\n") unless ex.backtrace.nil?
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanami-events-cloud_pubsub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ian Ker-Seymer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-configurable
|
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'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: google-cloud-pubsub
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -127,6 +141,7 @@ files:
|
|
127
141
|
- lib/hanami/events/cloud_pubsub/listener.rb
|
128
142
|
- lib/hanami/events/cloud_pubsub/mixin.rb
|
129
143
|
- lib/hanami/events/cloud_pubsub/runner.rb
|
144
|
+
- lib/hanami/events/cloud_pubsub/safe_error_handler.rb
|
130
145
|
- lib/hanami/events/cloud_pubsub/thread_inspector.rb
|
131
146
|
- lib/hanami/events/cloud_pubsub/version.rb
|
132
147
|
- log/.keep
|