hanami-events-cloud_pubsub 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c1e904f1c495c8ec63738657cffec7235f9ac37
4
- data.tar.gz: cc8260aba559598abff5cedeec6cc59e4514cf07
3
+ metadata.gz: 1fee1baafca2abf28345214b24f558961ca25a66
4
+ data.tar.gz: b090b51decb794b529f12786f92de729614d9dfd
5
5
  SHA512:
6
- metadata.gz: 90585ecfef0ea10e61674e7e3671633a5feaa1b5f1437b7952bf01b1a001314b30652d3df20cfb4eccafef5b8e6de26832b0114bfd47a7aafaad6b58bea777a7
7
- data.tar.gz: 69a520f5ed0fd98029bd6de9496f326d384ca2fb2717ee82635aacaca2113b70a5a9a83244077afbbd7f4d563e7f0a6f93a0dcec6fe937d86099f828161078e9
6
+ metadata.gz: 9991d0479c5b2f6fdbd0ad5ea5c1d12ae3e7cb3be8a2014ef25c9e712f0768b48b72c609a160d266d624eb6a5d1e2852d77687373c78313535660c55f8a6bd3a
7
+ data.tar.gz: dfaf278402868f15c16027e3a3d82cc78fc28b9070148926524137c7c3003be216997849811cceb9438bb93298787109904cb2ed39f9b137db359e52ba0c1acd
data/.rubocop_todo.yml CHANGED
@@ -1,20 +1,25 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2018-06-05 14:43:37 -0400 using RuboCop version 0.56.0.
3
+ # on 2018-06-11 14:37:44 -0400 using RuboCop version 0.54.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 4
9
+ # Offense count: 1
10
+ # Configuration parameters: CountKeywordArgs.
11
+ Metrics/ParameterLists:
12
+ Max: 6
13
+
14
+ # Offense count: 3
10
15
  # Configuration parameters: AllowedVariables.
11
16
  Style/GlobalVars:
12
17
  Exclude:
13
18
  - 'examples/server.rb'
14
19
  - 'lib/hanami/events/cloud_pubsub/cli.rb'
15
20
 
16
- # Offense count: 14
21
+ # Offense count: 22
17
22
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
18
23
  # URISchemes: http, https
19
24
  Metrics/LineLength:
20
- Max: 96
25
+ Max: 93
data/Gemfile CHANGED
@@ -10,6 +10,6 @@ gemspec
10
10
  group :test do
11
11
  gem 'hanami-events', github: 'ianks/events', branch: 'args-to-subscribe'
12
12
  gem 'pry'
13
- gem 'rubocop'
13
+ gem 'rubocop', '~> 0.54.0'
14
14
  gem 'simplecov', require: false
15
15
  end
data/Gemfile.lock CHANGED
@@ -9,7 +9,7 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- hanami-events-cloud_pubsub (0.2.2)
12
+ hanami-events-cloud_pubsub (0.2.3)
13
13
  dry-configurable (~> 0.7.0)
14
14
  google-cloud-pubsub (~> 0.30.2)
15
15
  hanami-cli (~> 0.2.0)
@@ -76,7 +76,6 @@ GEM
76
76
  hanami-utils (1.2.0)
77
77
  concurrent-ruby (~> 1.0)
78
78
  transproc (~> 1.0)
79
- jaro_winkler (1.5.1)
80
79
  json (2.1.0)
81
80
  jwt (2.1.0)
82
81
  little-plugger (1.1.4)
@@ -91,7 +90,7 @@ GEM
91
90
  parallel (1.12.1)
92
91
  parser (2.5.1.0)
93
92
  ast (~> 2.4.0)
94
- powerpack (0.1.1)
93
+ powerpack (0.1.2)
95
94
  pry (0.11.3)
96
95
  coderay (~> 1.1.0)
97
96
  method_source (~> 0.9.0)
@@ -113,8 +112,7 @@ GEM
113
112
  diff-lcs (>= 1.2.0, < 2.0)
114
113
  rspec-support (~> 3.7.0)
115
114
  rspec-support (3.7.1)
116
- rubocop (0.57.1)
117
- jaro_winkler (~> 1.5.1)
115
+ rubocop (0.54.0)
118
116
  parallel (~> 1.10)
119
117
  parser (>= 2.5)
120
118
  powerpack (~> 0.1)
@@ -145,7 +143,7 @@ DEPENDENCIES
145
143
  pry
146
144
  rake (~> 12.3)
147
145
  rspec (~> 3.0)
148
- rubocop
146
+ rubocop (~> 0.54.0)
149
147
  simplecov
150
148
 
151
149
  BUNDLED WITH
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- # Hanami::Events::CloudPubsub
2
- [![Build Status](https://travis-ci.org/adHawk/hanami-events-cloud_pubsub.svg?branch=master)](https://travis-ci.org/adHawk/hanami-events-cloud_pubsub) [![Maintainability](https://api.codeclimate.com/v1/badges/7341f70d4ed1d0bd7a5d/maintainability)](https://codeclimate.com/github/adHawk/hanami-events-cloud_pubsub/maintainability)
1
+ # Hanami Events for Google Cloud Pub/sub
2
+ [![Build Status](https://travis-ci.org/adHawk/hanami-events-cloud_pubsub.svg?branch=master)](https://travis-ci.org/adHawk/hanami-events-cloud_pubsub) [![Maintainability](https://api.codeclimate.com/v1/badges/7341f70d4ed1d0bd7a5d/maintainability)](https://codeclimate.com/github/adHawk/hanami-events-cloud_pubsub/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/7341f70d4ed1d0bd7a5d/test_coverage)](https://codeclimate.com/github/adHawk/hanami-events-cloud_pubsub/test_coverage)
3
3
 
4
4
  ## Installation
5
5
 
@@ -53,6 +53,14 @@ require 'config/environment'
53
53
 
54
54
  Hanami.boot
55
55
 
56
+ class CustomMiddleware
57
+ def call(message)
58
+ puts 'Middleware started!'
59
+ yield
60
+ puts 'Middleware ended!'
61
+ end
62
+ end
63
+
56
64
  Hanami::Events::CloudPubsub.configure do |config|
57
65
  # required
58
66
  config.subscriptions_loader = -> do
@@ -68,8 +76,9 @@ Hanami::Events::CloudPubsub.configure do |config|
68
76
  config.error_handlers << lambda do |err, message|
69
77
  Honeybadger.notify(err, context: message.attributes)
70
78
  end
71
- end
72
79
 
80
+ config.middleware << CustomMiddleware.new # must respond to #call
81
+ end
73
82
  ```
74
83
 
75
84
  Then, run the worker process:
@@ -2,6 +2,9 @@
2
2
 
3
3
  require 'hanami/events'
4
4
  require 'hanami/events/cloud_pubsub/version'
5
+ require 'hanami/events/cloud_pubsub/middleware/stack'
6
+ require 'hanami/events/cloud_pubsub/middleware/logging'
7
+ require 'hanami/events/cloud_pubsub/middleware/auto_acknowledge'
5
8
  require 'hanami/events/cloud_pubsub/runner'
6
9
  require 'hanami/events/cloud_pubsub/mixin'
7
10
  require 'google/cloud/pubsub'
@@ -46,6 +49,11 @@ module Hanami
46
49
  end
47
50
  ], reader: true
48
51
 
52
+ setting :middleware, Middleware::Stack.new(
53
+ Middleware::Logging.new,
54
+ Middleware::AutoAcknowledge.new
55
+ )
56
+
49
57
  def self.setup
50
58
  Hanami::Events::Adapter.register(:cloud_pubsub) do
51
59
  require_relative 'adapter/cloud_pubsub'
@@ -15,15 +15,22 @@ module Hanami
15
15
  :logger,
16
16
  :handler,
17
17
  :event_name,
18
- :subscriber_opts
19
-
20
- def initialize(topic:, logger:, handler:, event_name:, subscriber_id:)
18
+ :subscriber_opts,
19
+ :middleware
20
+
21
+ def initialize(topic:,
22
+ logger:,
23
+ handler:,
24
+ event_name:,
25
+ subscriber_id:,
26
+ middleware: CloudPubsub.config.middleware)
21
27
  @topic = topic
22
28
  @logger = logger
23
29
  @handler = handler
24
30
  @event_name = event_name
25
31
  @subscriber_id = subscriber_id
26
32
  @subscriber_opts = CloudPubsub.config.subscriber.to_h
33
+ @middleware = middleware
27
34
  end
28
35
 
29
36
  def register
@@ -72,29 +79,12 @@ module Hanami
72
79
  run_handler(msg)
73
80
  end
74
81
 
75
- #:reek:TooManyStatements
76
- #:reek:DuplicateMethodCall
77
- # rubocop:disable Metrics/MethodLength
78
82
  def run_handler(message)
79
- succeeded = false
80
- failed = false
81
- handler.call(message)
82
- succeeded = true
83
- rescue Exception => err # rubocop:disable all
84
- failed = true
83
+ middleware.invoke(message) { handler.call(message) }
84
+ rescue StandardError => err
85
85
  run_error_handlers(err, message)
86
- raise err
87
- ensure
88
- id = message.message_id
89
- if succeeded || failed
90
- message.acknowledge!
91
- logger.debug "Message(#{id}) was acknowledged"
92
- else
93
- message.reject!
94
- logger.warn "Message(#{id}) was terminated from outside, rescheduling"
95
- end
86
+ raise
96
87
  end
97
- # rubocop:enable Metrics/MethodLength
98
88
 
99
89
  def subscription_for(name)
100
90
  topic.create_subscription(name)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ module Events
5
+ module CloudPubsub
6
+ # Middleware
7
+ module Middleware
8
+ # Middleware used for automatically acknowledging messages
9
+ class AutoAcknowledge
10
+ def initialize(logger: nil)
11
+ @logger = logger
12
+ end
13
+
14
+ def call(message, *_args)
15
+ succeeded = false
16
+ failed = false
17
+ yield
18
+ succeeded = true
19
+ rescue StandardError => err
20
+ failed = true
21
+ raise err
22
+ ensure
23
+ ack_or_reject(message, succeeded, failed)
24
+ end
25
+
26
+ private
27
+
28
+ def ack_or_reject(message, succeeded, failed)
29
+ id = message.message_id
30
+
31
+ if succeeded || failed
32
+ message.acknowledge!
33
+ logger.debug "Message(#{id}) was acknowledged"
34
+ else
35
+ message.reject!
36
+ logger.warn "Message(#{id}) was terminated from outside, rescheduling"
37
+ end
38
+ end
39
+
40
+ def logger
41
+ @logger || CloudPubsub.logger
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ module Events
5
+ module CloudPubsub
6
+ module Middleware
7
+ # Middleware used for logging useful information about an event
8
+ class Logging
9
+ def initialize(logger: nil)
10
+ @logger = logger
11
+ end
12
+
13
+ def call(msg, *_args)
14
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
+ yield
16
+ ensure
17
+ ended_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
18
+ seconds = ended_at - started_at
19
+ logger.info <<~MSG
20
+ Processed message(id: #{msg.id}) took #{seconds} seconds to process
21
+ MSG
22
+ end
23
+
24
+ private
25
+
26
+ def logger
27
+ @logger || CloudPubsub.logger
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ module Events
5
+ module CloudPubsub
6
+ module Middleware
7
+ # Middleware which is invoked when a message is received
8
+ class Stack
9
+ # Error raised when middleware is not callable
10
+ class InvalidMiddlewareError < ArgumentError
11
+ def initialize(middleware)
12
+ super <<~MSG
13
+ Attempted to add middleware which is not callable: #{middleware.inspect}
14
+ Make sure that your middleware responds to the #call method
15
+ MSG
16
+ end
17
+ end
18
+
19
+ attr_reader :entries
20
+
21
+ def initialize(*entries)
22
+ entries.each(&method(:ensure_callable))
23
+ @entries = entries
24
+ yield self if block_given?
25
+ end
26
+
27
+ def <<(middleware)
28
+ ensure_callable(middleware)
29
+ entries << middleware
30
+ self
31
+ end
32
+
33
+ def shift
34
+ @entries.shift
35
+ end
36
+
37
+ def invoke(*args)
38
+ stack = entries.dup
39
+
40
+ traverse_stack = lambda do
41
+ if stack.empty?
42
+ yield(*args)
43
+ else
44
+ stack.shift.call(*args, &traverse_stack)
45
+ end
46
+ end
47
+
48
+ traverse_stack.call
49
+ end
50
+
51
+ private
52
+
53
+ def ensure_callable(middleware)
54
+ raise InvalidMiddlewareError, middleware unless middleware.respond_to?(:call)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -3,7 +3,7 @@
3
3
  module Hanami
4
4
  module Events
5
5
  module CloudPubsub
6
- VERSION = '0.2.2'
6
+ VERSION = '0.2.3'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-events-cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
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-08 00:00:00.000000000 Z
11
+ date: 2018-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -154,6 +154,9 @@ files:
154
154
  - lib/hanami/events/cloud_pubsub/handler.rb
155
155
  - lib/hanami/events/cloud_pubsub/health_check_server.rb
156
156
  - lib/hanami/events/cloud_pubsub/listener.rb
157
+ - lib/hanami/events/cloud_pubsub/middleware/auto_acknowledge.rb
158
+ - lib/hanami/events/cloud_pubsub/middleware/logging.rb
159
+ - lib/hanami/events/cloud_pubsub/middleware/stack.rb
157
160
  - lib/hanami/events/cloud_pubsub/mixin.rb
158
161
  - lib/hanami/events/cloud_pubsub/runner.rb
159
162
  - lib/hanami/events/cloud_pubsub/safe_error_handler.rb