hanami-events-cloud_pubsub 0.2.1 → 0.2.2

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: 3e32a098f1de2fd09c4bc5c6426de01c1b1f2901
4
- data.tar.gz: ee797299d3b807a971ba55c26c56c2df39d42a71
3
+ metadata.gz: 5c1e904f1c495c8ec63738657cffec7235f9ac37
4
+ data.tar.gz: cc8260aba559598abff5cedeec6cc59e4514cf07
5
5
  SHA512:
6
- metadata.gz: 947616cdbd3ca4a37918db0071306f24411c06ebb351b0b5b8364c2b8660c4fab4e4959e10367d45d6e676f2ee2f6169bd86a5d7fdceb6539b085b77273c400b
7
- data.tar.gz: e47ed44cf294950d6c087443587e5f0b3c05e76f58c9f92aadbc167e728071fa016f4b742858cd680652f1021365a60315d5ebc9745950ad2111f8a7b1444466
6
+ metadata.gz: 90585ecfef0ea10e61674e7e3671633a5feaa1b5f1437b7952bf01b1a001314b30652d3df20cfb4eccafef5b8e6de26832b0114bfd47a7aafaad6b58bea777a7
7
+ data.tar.gz: 69a520f5ed0fd98029bd6de9496f326d384ca2fb2717ee82635aacaca2113b70a5a9a83244077afbbd7f4d563e7f0a6f93a0dcec6fe937d86099f828161078e9
data/Gemfile.lock CHANGED
@@ -9,11 +9,12 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- hanami-events-cloud_pubsub (0.2.1)
13
- dry-configurable
12
+ hanami-events-cloud_pubsub (0.2.2)
13
+ dry-configurable (~> 0.7.0)
14
14
  google-cloud-pubsub (~> 0.30.2)
15
- hanami-cli
16
- hanami-events
15
+ hanami-cli (~> 0.2.0)
16
+ hanami-events (~> 0)
17
+ rack (~> 2.0.5)
17
18
 
18
19
  GEM
19
20
  remote: https://rubygems.org/
@@ -75,7 +76,7 @@ GEM
75
76
  hanami-utils (1.2.0)
76
77
  concurrent-ruby (~> 1.0)
77
78
  transproc (~> 1.0)
78
- jaro_winkler (1.4.0)
79
+ jaro_winkler (1.5.1)
79
80
  json (2.1.0)
80
81
  jwt (2.1.0)
81
82
  little-plugger (1.1.4)
@@ -95,6 +96,7 @@ GEM
95
96
  coderay (~> 1.1.0)
96
97
  method_source (~> 0.9.0)
97
98
  public_suffix (3.0.2)
99
+ rack (2.0.5)
98
100
  rainbow (3.0.0)
99
101
  rake (12.3.1)
100
102
  rly (0.2.3)
@@ -111,8 +113,8 @@ GEM
111
113
  diff-lcs (>= 1.2.0, < 2.0)
112
114
  rspec-support (~> 3.7.0)
113
115
  rspec-support (3.7.1)
114
- rubocop (0.57.0)
115
- jaro_winkler (~> 1.4.0)
116
+ rubocop (0.57.1)
117
+ jaro_winkler (~> 1.5.1)
116
118
  parallel (~> 1.10)
117
119
  parser (>= 2.5)
118
120
  powerpack (~> 0.1)
data/examples/server.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Hanami::Events::CloudPubsub.configure do |config|
4
+ config.subscriber.streams = 2
5
+ config.subscriber.threads.push = 2
6
+ config.subscriber.threads.callback = 2
7
+
4
8
  config.subscriptions_loader = -> do
5
9
  $events.subscribe('user.deleted', id: 'testing-2') do |payload|
6
10
  puts "Deleted2 user: #{payload}"
@@ -22,10 +22,11 @@ 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
+ spec.add_dependency 'dry-configurable', '~> 0.7.0'
26
26
  spec.add_dependency 'google-cloud-pubsub', '~> 0.30.2'
27
- spec.add_dependency 'hanami-cli'
28
- spec.add_dependency 'hanami-events'
27
+ spec.add_dependency 'hanami-cli', '~> 0.2.0'
28
+ spec.add_dependency 'hanami-events', '~> 0'
29
+ spec.add_dependency 'rack', '~> 2.0.5'
29
30
 
30
31
  spec.add_development_dependency 'bundler', '~> 1.16'
31
32
  spec.add_development_dependency 'rake', '~> 12.3'
@@ -13,6 +13,13 @@ module Hanami
13
13
  module CloudPubsub
14
14
  extend Dry::Configurable
15
15
 
16
+ setting :subscriber, reader: true do
17
+ setting :streams, 4
18
+ setting :threads do
19
+ setting :callback, 8
20
+ setting :push, 4
21
+ end
22
+ end
16
23
  setting :project_id, reader: true
17
24
  setting :logger, Logger.new(STDOUT), reader: true
18
25
  setting :subscriptions_loader, proc {
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'hanami/cli'
4
4
  require 'hanami/events/cloud_pubsub'
5
+ require 'hanami/events/cloud_pubsub/health_check_server'
5
6
 
6
7
  module Hanami
7
8
  module Events
@@ -13,6 +14,13 @@ module Hanami
13
14
 
14
15
  # Command to run the worker
15
16
  class Run < Hanami::CLI::Command
17
+ attr_reader :runner
18
+
19
+ def initialize(*args)
20
+ super
21
+ @event_queue = Queue.new
22
+ end
23
+
16
24
  option :emulator,
17
25
  type: :boolean,
18
26
  default: false,
@@ -35,6 +43,7 @@ module Hanami
35
43
  load_subscriptions
36
44
  setup_signal_handlers
37
45
  start_runner
46
+ start_server
38
47
  sleep_forever
39
48
  end
40
49
 
@@ -49,18 +58,18 @@ module Hanami
49
58
  CloudPubsub.setup
50
59
  end
51
60
 
61
+ def start_server
62
+ server = HealthCheckServer.new(runner, logger)
63
+ on_shutdown = proc { @event_queue << proc { shutdown } }
64
+ server.run_in_background(on_shutdown: on_shutdown)
65
+ server
66
+ end
67
+
52
68
  def sleep_forever
53
- thread = Thread.new do
54
- loop do
55
- event = @queue.pop
56
- event.call
57
- end
69
+ until finished_shutting_down?
70
+ event = @event_queue.pop
71
+ event.call
58
72
  end
59
-
60
- sleep
61
- rescue Interrupt
62
- thread.kill
63
- shutdown
64
73
  end
65
74
 
66
75
  def load_config
@@ -72,7 +81,7 @@ module Hanami
72
81
  def start_runner
73
82
  logger.debug 'Running in emulator mode' if @emulator
74
83
  logger.info "Starting worker (pid: #{Process.pid})"
75
- @runner.start
84
+ runner.start
76
85
  end
77
86
 
78
87
  def parse_opts(opts)
@@ -100,24 +109,20 @@ module Hanami
100
109
  end
101
110
 
102
111
  def setup_signal_handlers
103
- @queue = Queue.new
104
-
105
- Signal.trap('TSTP') do
106
- @queue << proc { @runner.pause }
107
- end
108
-
109
- Signal.trap('CONT') do
110
- @queue << proc { @runner.start }
111
- end
112
-
113
- Signal.trap('TTIN') do
114
- @queue << proc { @runner.print_debug_info }
115
- end
112
+ Signal.trap('TSTP') { @event_queue << runner.method(:pause) }
113
+ Signal.trap('TTIN') { @event_queue << runner.method(:print_debug_info) }
114
+ Signal.trap('INT') { @event_queue << method(:shutdown) }
116
115
  end
117
116
 
118
117
  def shutdown
119
118
  STDOUT.flush
120
- @runner.gracefully_shutdown
119
+ runner.gracefully_shutdown
120
+ ensure
121
+ @finished_shutting_down = true
122
+ end
123
+
124
+ def finished_shutting_down?
125
+ @finished_shutting_down == true
121
126
  end
122
127
  end
123
128
 
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rack'
4
+
5
+ module Hanami
6
+ module Events
7
+ module CloudPubsub
8
+ # Simple server for health checks
9
+ class HealthCheckServer
10
+ def initialize(runner, logger)
11
+ @runner = runner
12
+ @logger = logger
13
+ end
14
+
15
+ def start
16
+ @logger.info 'Starting healthcheck server on port 0.0.0.0:8080'
17
+ server.start
18
+ end
19
+
20
+ def run_in_background(on_shutdown:)
21
+ log_error = proc { |err| logger.error(err.message) }
22
+
23
+ Concurrent::Promise.execute(on_reject: log_error) do
24
+ server.start
25
+ on_shutdown.call(server)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def server
32
+ @server ||= Rack::Server.new(
33
+ Port: 8080,
34
+ Host: '0.0.0.0',
35
+ quiet: true,
36
+ app: app
37
+ )
38
+ end
39
+
40
+ def app
41
+ health_endpoint = method(:health_endpoint)
42
+
43
+ Rack::Builder.app do
44
+ run health_endpoint
45
+ end
46
+ end
47
+
48
+ def health_endpoint(_env)
49
+ status = @runner.healthy? ? 200 : 503
50
+ headers = { 'Content-Type' => 'text/html' }
51
+ body = [status.to_s]
52
+
53
+ [status, headers, body]
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -14,7 +14,8 @@ module Hanami
14
14
  :subscriber_id,
15
15
  :logger,
16
16
  :handler,
17
- :event_name
17
+ :event_name,
18
+ :subscriber_opts
18
19
 
19
20
  def initialize(topic:, logger:, handler:, event_name:, subscriber_id:)
20
21
  @topic = topic
@@ -22,12 +23,13 @@ module Hanami
22
23
  @handler = handler
23
24
  @event_name = event_name
24
25
  @subscriber_id = subscriber_id
26
+ @subscriber_opts = CloudPubsub.config.subscriber.to_h
25
27
  end
26
28
 
27
29
  def register
28
30
  subscription = subscription_for(subscriber_id)
29
31
 
30
- listener = subscription.listen do |message|
32
+ listener = subscription.listen(subscriber_opts) do |message|
31
33
  handle_message(message)
32
34
  end
33
35
 
@@ -97,7 +99,7 @@ module Hanami
97
99
  def subscription_for(name)
98
100
  topic.create_subscription(name)
99
101
  rescue Google::Cloud::AlreadyExistsError
100
- # OK
102
+ topic.find_subscription(name)
101
103
  end
102
104
 
103
105
  def ensure_subscriber!
@@ -3,7 +3,7 @@
3
3
  module Hanami
4
4
  module Events
5
5
  module CloudPubsub
6
- VERSION = '0.2.1'
6
+ VERSION = '0.2.2'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-events-cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
@@ -14,16 +14,16 @@ dependencies:
14
14
  name: dry-configurable
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.7.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.7.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: google-cloud-pubsub
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -42,30 +42,44 @@ dependencies:
42
42
  name: hanami-cli
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 0.2.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 0.2.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: hanami-events
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.5
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.5
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -138,6 +152,7 @@ files:
138
152
  - lib/hanami/events/cloud_pubsub.rb
139
153
  - lib/hanami/events/cloud_pubsub/cli.rb
140
154
  - lib/hanami/events/cloud_pubsub/handler.rb
155
+ - lib/hanami/events/cloud_pubsub/health_check_server.rb
141
156
  - lib/hanami/events/cloud_pubsub/listener.rb
142
157
  - lib/hanami/events/cloud_pubsub/mixin.rb
143
158
  - lib/hanami/events/cloud_pubsub/runner.rb