gracefully 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f736eec744523457533b32c6df2f79cc26c4c154
4
+ data.tar.gz: a02c22d1d338f3120bf7a712d6df7345552e7e88
5
+ SHA512:
6
+ metadata.gz: 548dcba4a7645ce353bac284e57ee5b3d250f75ed40d4818bb514f71db0bd0e78af12b79979b9dfcf9bc53072f531fa8b54cd9d774cda568a5049adf35ffb291
7
+ data.tar.gz: b88c02168bcf7a204ae38188245cad7d655723c61a67a96e128b32815f2bb1d6dc92d95dd8d004e5a06b874153e51e630daeb6d695f4e023d236d224439dbe9e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *~
16
+ .idea/
17
+ *.iml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gracefully.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec', '~> 3.1.0'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Yusuke KUOKA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Gracefully
2
+
3
+ ensures features gracefully degrade based on error rate or turnaround time.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gracefully'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gracefully
20
+
21
+ ## Usage
22
+
23
+ See `spec/gracefully_spec.rb` for basic usage.
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/gracefully/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gracefully/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gracefully"
8
+ spec.version = Gracefully::VERSION
9
+ spec.authors = ["Yusuke KUOKA"]
10
+ spec.email = ["yusuke.kuoka@crowdworks.co.jp"]
11
+ spec.summary = %q{ensures features gracefully degrade based on error rate or turnaround time.}
12
+ spec.homepage = "https://github.com/crowdworks/gracefully"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
@@ -0,0 +1,13 @@
1
+ module Gracefully
2
+ class Feature
3
+ def initialize(args)
4
+ @name = args[:name]
5
+ @usually = args[:usually]
6
+ @fallback_to = args[:fallback_to]
7
+ end
8
+
9
+ def call(*args)
10
+ Try.to { @usually.call *args }.or_else(Try.to { @fallback_to.call *args }).get
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module Gracefully
2
+ class FeatureBuilder
3
+ def initialize(feature_name)
4
+ @feature_name = feature_name
5
+ end
6
+
7
+ def usually(&block)
8
+ @usually = block
9
+ self
10
+ end
11
+
12
+ def fallback_to(&block)
13
+ @fallback_to = block
14
+
15
+ build
16
+ end
17
+
18
+ private
19
+
20
+ def build
21
+ Feature.new(name: @feature_name, usually: @usually, fallback_to: @fallback_to)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ module Gracefully
2
+ class FeatureRepository
3
+ def self.instance
4
+ @instance
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,90 @@
1
+ module Gracefully
2
+ class HealthMeter
3
+ def initialize(args)
4
+ @healthy_count = 0
5
+ @unhealthy_count = 0
6
+ @state = Healthy.new configuration: args[:configuration]
7
+ end
8
+
9
+ def mark_healthy
10
+ @state = @state.mark_healthy
11
+ end
12
+
13
+ def mark_unhealthy
14
+ @state = @state.mark_unhealthy
15
+ end
16
+
17
+ def healthy?
18
+ @state.healthy?
19
+ end
20
+
21
+ def unhealthy?
22
+ @state.unhealthy?
23
+ end
24
+
25
+ class State
26
+ def unhealthy?
27
+ !healthy?
28
+ end
29
+ end
30
+
31
+ class Configuration
32
+ attr_reader :healthy_threshold, :unhealthy_threshold
33
+
34
+ def initialize(args)
35
+ @healthy_threshold = args[:healthy_threshold]
36
+ @unhealthy_threshold = args[:unhealthy_threshold]
37
+ end
38
+ end
39
+
40
+ class Healthy < State
41
+ def initialize(args)
42
+ @unhealthy_count = 0
43
+ @configuration = args[:configuration]
44
+ end
45
+
46
+ def mark_healthy
47
+ self
48
+ end
49
+
50
+ def mark_unhealthy
51
+ @unhealthy_count += 1
52
+
53
+ if @unhealthy_count <= @configuration.unhealthy_threshold
54
+ self
55
+ else
56
+ Unhealthy.new configuration: @configuration
57
+ end
58
+ end
59
+
60
+ def healthy?
61
+ true
62
+ end
63
+ end
64
+
65
+ class Unhealthy < State
66
+ def initialize(args)
67
+ @healthy_count = 0
68
+ @configuration = args[:configuration]
69
+ end
70
+
71
+ def mark_healthy
72
+ @healthy_count += 1
73
+
74
+ if @healthy_count <= @configuration.healthy_threshold
75
+ self
76
+ else
77
+ Healthy.new configuration: @configuration
78
+ end
79
+ end
80
+
81
+ def mark_unhealthy
82
+ self
83
+ end
84
+
85
+ def healthy?
86
+ false
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,97 @@
1
+ module Gracefully
2
+ class Try
3
+ def self.to(&block)
4
+ Unresolved.new(&block)
5
+ end
6
+ end
7
+
8
+ class Unresolved
9
+ def initialize(&block)
10
+ @block = block
11
+ end
12
+
13
+ def resolve
14
+ @resolved = begin
15
+ Success.with @block.call
16
+ rescue => e
17
+ Failure.with Error.new('Nested error', nested: e)
18
+ end
19
+ end
20
+
21
+ def or_else(other)
22
+ resolve.or_else other
23
+ end
24
+
25
+ def get
26
+ resolve.get
27
+ end
28
+ end
29
+
30
+ class Success
31
+ def self.with(result)
32
+ new result
33
+ end
34
+
35
+ def initialize(result)
36
+ @result = result
37
+ end
38
+
39
+ def or_else(other)
40
+ self
41
+ end
42
+
43
+ def get
44
+ @result
45
+ end
46
+ end
47
+
48
+ class Failure
49
+ def self.with(error)
50
+ new error
51
+ end
52
+
53
+ def initialize(error)
54
+ @error = error
55
+ end
56
+
57
+ def or_else(other)
58
+ other
59
+ end
60
+
61
+ def get
62
+ raise @error
63
+ end
64
+ end
65
+
66
+ # Thanks to [nested](https://github.com/skorks/nesty) for the original code
67
+ module NestedError
68
+ def initialize(message, args)
69
+ @nested = args[:nested]
70
+ super(message)
71
+ end
72
+
73
+ def set_backtrace(backtrace)
74
+ @raw_backtrace = backtrace
75
+ if nested
76
+ backtrace = backtrace - nested_raw_backtrace
77
+ backtrace += ["#{nested.backtrace.first}: #{nested.message} (#{nested.class.name})"]
78
+ backtrace += nested.backtrace[1..-1] || []
79
+ end
80
+ super(backtrace)
81
+ end
82
+
83
+ private
84
+
85
+ def nested_raw_backtrace
86
+ nested.respond_to?(:raw_backtrace) ? nested.raw_backtrace : nested.backtrace
87
+ end
88
+
89
+ def nested
90
+ @nested
91
+ end
92
+ end
93
+
94
+ class Error < StandardError
95
+ include NestedError
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module Gracefully
2
+ VERSION = "0.0.1"
3
+ end
data/lib/gracefully.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "gracefully/version"
2
+ require "gracefully/feature"
3
+ require "gracefully/feature_builder"
4
+ require "gracefully/try"
5
+
6
+ module Gracefully
7
+ def self.degrade(feature_name)
8
+ FeatureBuilder.new(feature_name)
9
+ end
10
+ end
@@ -0,0 +1,43 @@
1
+ require 'gracefully'
2
+
3
+ RSpec.describe Gracefully do
4
+ describe "feature call result" do
5
+ subject {
6
+ Gracefully.
7
+ degrade(feature_name).
8
+ usually(&usually).
9
+ fallback_to(&fallback_to).
10
+ call input1
11
+ }
12
+
13
+ let(:input1) {
14
+ 'input1'
15
+ }
16
+
17
+ let(:fallback_to) {
18
+ -> arg1 { 'fallback:arg1:' + arg1 }
19
+ }
20
+
21
+ context 'when the feature is defined for the name' do
22
+ let(:feature_name) {
23
+ :the_feature
24
+ }
25
+
26
+ context 'when the usually block succeeds without any error' do
27
+ let(:usually) {
28
+ -> arg1 { 'usually:arg1:' + arg1 }
29
+ }
30
+
31
+ it { is_expected.to eq('usually:arg1:input1') }
32
+ end
33
+
34
+ context 'when the usually block fails with an error' do
35
+ let(:usually) {
36
+ -> arg1 { raise 'simulated error' }
37
+ }
38
+
39
+ it { is_expected.to eq('fallback:arg1:input1') }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,23 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+
10
+ config.disable_monkey_patching!
11
+
12
+ config.warnings = true
13
+
14
+ if config.files_to_run.one?
15
+ config.default_formatter = 'doc'
16
+ end
17
+
18
+ config.profile_examples = 10
19
+
20
+ config.order = :random
21
+
22
+ Kernel.srand config.seed
23
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gracefully
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke KUOKA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - yusuke.kuoka@crowdworks.co.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - gracefully.gemspec
55
+ - lib/gracefully.rb
56
+ - lib/gracefully/feature.rb
57
+ - lib/gracefully/feature_builder.rb
58
+ - lib/gracefully/feature_repository.rb
59
+ - lib/gracefully/health_meter.rb
60
+ - lib/gracefully/try.rb
61
+ - lib/gracefully/version.rb
62
+ - spec/gracefully_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/crowdworks/gracefully
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.2.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: ensures features gracefully degrade based on error rate or turnaround time.
88
+ test_files:
89
+ - spec/gracefully_spec.rb
90
+ - spec/spec_helper.rb