grpc-kit 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10cac29d383da9e2067ce2531663f0dc2dec7a7a
4
+ data.tar.gz: 3484d24ff86999515b2dad895bb6d9509ab22d4b
5
+ SHA512:
6
+ metadata.gz: 203f47dc248133a4711030718616186e176b2f7a65969932d1585351c167fd5719d2b1c8f93c0fe8a3ee0492cae6ca04f1d48b02c15184aba639c1cd09a66c83
7
+ data.tar.gz: 1f3701a9ddbdb7e11a8f381a8f4494951346cf804c68242ef4c3da2579dc1684cba7505c235a655bc17bdd85c0422673da3a8c68b64fd67bc69efd58481b089a
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ - 2.2.6
6
+ - 2.1.10
7
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grpc-kit.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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.
@@ -0,0 +1,102 @@
1
+ # GRPC::Kit
2
+
3
+ GRPC toolkit for microservices
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'grpc-kit'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install grpc-kit
20
+
21
+ ## Documentation
22
+
23
+ - [`GRPC::Kit::Communication::Resilient`](#grpckitcommunicationresilient)
24
+ - [`GRPC::Kit::Logger`](#grpckitlogger)
25
+ - [`GRPC::Kit::Queue::Publisher`](#grpckitqueuepublisher)
26
+ - [`GRPC::Kit::Queue::Worker`](#grpckitqueueworker)
27
+
28
+ ### GRPC::Kit::Communication::Resilient
29
+
30
+ Sometimes, GRPC and Google::Cloud raises _unavailable error_, but you can retry and all works fine. Based on [Datastore documentation](https://cloud.google.com/appengine/articles/handling_datastore_errors#timeouts-due-to-datastore-issues) we implemented a _helper_ for _resilient communication_.
31
+
32
+ Example:
33
+
34
+ ```ruby
35
+ pubsub = Google::Cloud::Pubsub.new
36
+ topic = pubsub.create_topic('topic_name')
37
+ topic.publish('message') # receives Google::Cloud::UnavailableError
38
+
39
+ # But you can uses resilient communication
40
+ include GRPC::Kit::Communication::Resilient
41
+ topic = resilient { topic.publish('message') }
42
+
43
+ # And you can sets the limit of repetition...
44
+ topic = resilient(limit: 5) { topic.publish('message') }
45
+ ```
46
+
47
+ ### GRPC::Kit::Logger
48
+
49
+ By default all logs sent to `GRPC.logger` are ignored. But `GRPC::Kit::Logger` automatically configures logs to use _STDOUT_.
50
+
51
+ ### GRPC::Kit::Queue::Publisher
52
+
53
+ To publish a message to topic (creating one if none exists) you only need to configure environment variables for `Google::Cloud::Pubsub` and use:
54
+
55
+ ```ruby
56
+ GRPC::Kit::Queue::Publisher.publish('topic_name', 'message')
57
+ ```
58
+
59
+ ### GRPC::Kit::Queue::Worker
60
+
61
+ Create a class in `lib/workers` including `GRPC::Kit::Queue::Worker`:
62
+
63
+ ```ruby
64
+ # lib/workers/my_worker.rb
65
+ class MyWorker
66
+ include GRPC::Kit::Queue::Worker
67
+
68
+ def initialize(msg)
69
+ @msg = msg
70
+ end
71
+
72
+ def call
73
+ puts @msg.data
74
+ @msg.ack!
75
+ end
76
+ end
77
+ ```
78
+
79
+ And you can use:
80
+
81
+ ```bash
82
+ # to list available workers
83
+ grpc-kit workers list
84
+ # to run a worker
85
+ grpc-kit workers runner MyWorker topic_name
86
+ ```
87
+
88
+ ## Development
89
+
90
+ 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.
91
+
92
+ 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).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/b2beauty/grpc-kit.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
102
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'grpc/kit'
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'grpc/kit'
5
+
6
+ GRPC::Kit::Cli.start(ARGV)
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grpc/kit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'grpc-kit'
8
+ spec.version = GRPC::Kit::VERSION
9
+ spec.authors = ['Leonardo Saraiva']
10
+ spec.email = ['vyper@maneh.org']
11
+
12
+ spec.summary = 'GRPC toolkit for microservices'
13
+ spec.description = 'GRPC toolkit for microservices'
14
+ spec.homepage = 'http://github.com/b2beauty/grpc-kit'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'google-cloud-pubsub', '~> 0.23.0'
25
+ spec.add_dependency 'thor', '~> 0.19.4'
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.14'
28
+ spec.add_development_dependency 'rake', '~> 12.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+ end
@@ -0,0 +1,10 @@
1
+ require 'grpc/kit/cli'
2
+ require 'grpc/kit/communication'
3
+ require 'grpc/kit/logger'
4
+ require 'grpc/kit/queue'
5
+ require 'grpc/kit/version'
6
+
7
+ module GRPC
8
+ module Kit
9
+ end
10
+ end
@@ -0,0 +1,38 @@
1
+ require 'thor'
2
+
3
+ workers_dir = File.join(Dir.pwd, 'lib', 'workers')
4
+ $LOAD_PATH.unshift(workers_dir) unless $LOAD_PATH.include?(workers_dir)
5
+
6
+ module GRPC
7
+ module Kit
8
+ class Workers < Thor
9
+ desc 'runner <worker> <topic>', 'Run <worker> for <topic>'
10
+ def runner(worker, topic)
11
+ require worker.gsub(/(.)([A-Z])/,'\1_\2').downcase
12
+
13
+ Queue::Worker::Runner.run! topic: topic, worker: worker
14
+ end
15
+
16
+ desc 'list', 'List available workers'
17
+ def list
18
+ Dir.glob('lib/workers/**/*') do |file|
19
+ require File.basename(file, File.extname(file))
20
+ end
21
+
22
+ workers = GRPC::Kit::Queue::Worker.list
23
+ if workers.empty?
24
+ puts 'No available workers'
25
+ else
26
+ workers.each do |worker|
27
+ puts " - #{worker}"
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ class Cli < Thor
34
+ desc 'workers SUBCOMMAND ...ARGS', 'run or manage workers'
35
+ subcommand 'workers', Workers
36
+ end
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ require_relative 'communication/resilient'
@@ -0,0 +1,41 @@
1
+ require 'grpc/errors'
2
+ require 'google/cloud/errors'
3
+
4
+ module GRPC
5
+ module Kit
6
+ module Communication
7
+ module Resilient
8
+ ERRORS = [
9
+ GRPC::BadStatus,
10
+ Google::Cloud::UnavailableError,
11
+ Google::Cloud::InternalError
12
+ ].freeze
13
+
14
+ def resilient(limit: 16)
15
+ tries ||= 0
16
+ yield
17
+ # From Datastore documentation:
18
+ # - UNAVAILABLE;
19
+ # - Server returned an error;
20
+ # - Retry using exponential backoff.
21
+ rescue *ERRORS => e
22
+ tries += 1
23
+ exponential_backoff(tries, limit: limit) && retry
24
+ raise
25
+ end
26
+
27
+ # See: https://en.wikipedia.org/wiki/Exponential_backoff
28
+ def exponential_backoff(tries, limit:)
29
+ # Retry few times before going exponential
30
+ return true if tries <= 3
31
+
32
+ # Check whether it's reached the ceiling
33
+ if tries < limit
34
+ retry_time = 0.1 * rand(1 << tries) # random number between 0 and 2**N − 1
35
+ sleep(retry_time)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ require 'logger'
2
+
3
+ module GRPC
4
+ module Kit
5
+ module Logger
6
+ # TODO Should we use config or env var?
7
+ LOGGER = ::Logger.new(STDOUT)
8
+ # TODO Should we use config or env var?
9
+ LOGGER.level = ::Logger::DEBUG
10
+
11
+ def logger
12
+ LOGGER
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ module GRPC
19
+ extend Kit::Logger
20
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'queue/publisher'
2
+ require_relative 'queue/worker'
@@ -0,0 +1,25 @@
1
+ require 'google/cloud/pubsub'
2
+
3
+ module GRPC
4
+ module Kit
5
+ module Queue
6
+ module Publisher
7
+ module_function
8
+
9
+ @@topics = {}
10
+
11
+ def publish(topic_name, message)
12
+ topic(topic_name).publish(message)
13
+ end
14
+
15
+ def topic(name)
16
+ @@topics[name] ||= pubsub.topic(name) || pubsub.create_topic(name)
17
+ end
18
+
19
+ def pubsub
20
+ @@pubsub ||= Google::Cloud::Pubsub.new
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'worker/runner'
2
+
3
+ module GRPC
4
+ module Kit
5
+ module Queue
6
+ module Worker
7
+ def self.included(klass)
8
+ @@workers ||= []
9
+ @@workers << klass
10
+ end
11
+
12
+ def self.list
13
+ @@workers
14
+ rescue NameError
15
+ []
16
+ end
17
+
18
+ def call
19
+ raise NotImplemented.new
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ module GRPC
2
+ module Kit
3
+ module Queue
4
+ module Worker
5
+ class Runner
6
+ def initialize(params)
7
+ @topic_name = params[:topic]
8
+ @subscription_name = "#{params[:topic]}.#{params[:worker]}"
9
+ @worker_class = params[:worker]
10
+ end
11
+
12
+ def self.run!(params)
13
+ new(params).run!
14
+ end
15
+
16
+ def run!
17
+ if worker.nil?
18
+ GRPC.logger.error("class #{@worker_class} does not exist")
19
+ exit
20
+ end
21
+
22
+ subscription.listen do |msg|
23
+ worker.new(msg).call
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def worker
30
+ Object.const_get(@worker_class)
31
+ rescue NameError
32
+ nil
33
+ end
34
+
35
+ def subscription
36
+ @subscription ||= topic.subscription(@subscription_name) || topic.subscribe(@subscription_name)
37
+ end
38
+
39
+ def topic
40
+ @topic ||= pubsub.topic(@topic_name) || pubsub.create_topic(@topic_name)
41
+ end
42
+
43
+ def pubsub
44
+ @pubsub ||= Google::Cloud::Pubsub.new
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module GRPC
2
+ module Kit
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'grpc/kit/queue/worker'
2
+
3
+ class MyOtherWorker
4
+ include GRPC::Kit::Queue::Worker
5
+
6
+ def call
7
+ puts 'ping'
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grpc-kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonardo Saraiva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-23 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.23.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.23.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.19.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.19.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: GRPC toolkit for microservices
84
+ email:
85
+ - vyper@maneh.org
86
+ executables:
87
+ - grpc-kit
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - exe/grpc-kit
101
+ - grpc-kit.gemspec
102
+ - lib/grpc/kit.rb
103
+ - lib/grpc/kit/cli.rb
104
+ - lib/grpc/kit/communication.rb
105
+ - lib/grpc/kit/communication/resilient.rb
106
+ - lib/grpc/kit/logger.rb
107
+ - lib/grpc/kit/queue.rb
108
+ - lib/grpc/kit/queue/publisher.rb
109
+ - lib/grpc/kit/queue/worker.rb
110
+ - lib/grpc/kit/queue/worker/runner.rb
111
+ - lib/grpc/kit/version.rb
112
+ - lib/workers/my_other_worker.rb
113
+ homepage: http://github.com/b2beauty/grpc-kit
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.6.10
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: GRPC toolkit for microservices
137
+ test_files: []