sc4ry 0.1.7 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sc4ry/store.rb CHANGED
@@ -1,42 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Sc4ry module
4
+ # @note namespace
1
5
  module Sc4ry
6
+ # Sc4ry::Store class
7
+ # Store Class Provider/manager singleton Forwarder on {Sc4ry::Backends::Memory} or {Sc4ry::Backends::Redis}
8
+ # @note must be accessed by {Sc4ry::Circuits.store}
2
9
  class Store
3
-
4
10
  @@current = :memory
5
11
 
6
12
  extend Forwardable
7
13
  include Singleton
8
14
 
9
- @@backends = {:memory => {:class => Sc4ry::Backends::Memory},
10
- :redis => {:class => Sc4ry::Backends::Redis, :config => {:host => 'localhost', :port => 6379, :db => 10 }}}
15
+ @@backends = { memory: { class: Sc4ry::Backends::Memory, config: {} },
16
+ redis: { class: Sc4ry::Backends::Redis, config: { host: 'localhost', port: 6379, db: 1 } } }
11
17
 
18
+ # accessor on current backend (default :memory)
12
19
  attr_reader :be
20
+
13
21
  def_delegators :@be, :put, :get, :flush, :exist?, :del, :list
14
22
 
23
+ # constructor pointing on :memory backend
15
24
  def initialize
16
- change_backend name: @@current
17
- end
25
+ change_backend name: @@current
26
+ end
18
27
 
28
+ # return the current backend
29
+ # @return [Object] in {Sc4ry::Backends::Memory} or {Sc4ry::Backends::Redis}
30
+ # @example usage
31
+ # include Sc4ry
32
+ # puts Circuits.store.current
19
33
  def current
20
- return @@current
34
+ @@current
21
35
  end
22
36
 
23
- def change_backend(options)
24
- @@current = options[:name]
25
- @be = @@backends[@@current][:class]::new(@@backends[@@current][:config])
37
+ # return the config of a specific backend
38
+ # @param [Symbol] backend the name the backend
39
+ # @return [Hash] the config of the backend
40
+ # @raise Sc4ry::Exceptions::Sc4ryBackendError if backend is not found
41
+ # @example usage
42
+ # include Sc4ry
43
+ # puts Circuits.store.get_config backend: :redis
44
+ def get_config(backend:)
45
+ raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{backend} not found" unless @@backends.include? backend
46
+
47
+ @@backends[backend][:config]
26
48
  end
27
49
 
28
- def register_backend(options)
29
- raise ":name is mandatory" unless options[:name]
30
- raise ":definition is mandatory" unless options[:definition]
31
- @@backends[options[:name]] = options[:definition]
50
+ # list backend available
51
+ # @return [Array] of Symbol the list of backend name
52
+ # @example usage
53
+ # include Sc4ry
54
+ # puts Circuits.store.list_backend
55
+ def list_backend
56
+ @@backends.keys
32
57
  end
33
58
 
34
- def config_backend(options)
35
- raise ":name is mandatory" unless options[:name]
36
- raise ":config is mandatory" unless options[:config]
37
- @@backends[options[:name]][:config] = options[:config]
59
+ # change the current backend
60
+ # @note if changing form :memory to :redis => all values and result are lost and circuits will be lost
61
+ # @note if changing to :redis, get all the define circuits with values and status (ideal)
62
+ # @note for distributed worker/instance/runner/services
63
+ # @param [Symbol] name the name of the target backend
64
+ # @return [Symbol] the name of the new current backend
65
+ # @raise Sc4ry::Exceptions::Sc4ryBackendError if backend is not found
66
+ def change_backend(name:)
67
+ raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} not found" unless @@backends.include? name
68
+
69
+ @@current = name
70
+ @be = @@backends[@@current][:class].new(@@backends[@@current][:config])
71
+ name
38
72
  end
39
73
 
74
+ # register a new backend
75
+ # @param [Symbol] name the name of the backend
76
+ # @param [Hash] config the config for this backend
77
+ # @param [Class] backend_class the class name of the new backend
78
+ # @raise Sc4ry::Exceptions::Sc4ryBackendError if backend already exist
79
+ # @return [Symbol] the name of the backend
80
+ def register_backend(name:, backend_class:, config: {})
81
+ raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} already exist" if @@backends.include? name
40
82
 
83
+ @@backends[name] = { config: config, class: backend_class }
84
+ name
85
+ end
86
+
87
+ # delete the specified backend reference
88
+ # @param [Symbol] name the name of the target backend
89
+ # @raise Sc4ry::Exceptions::Sc4ryBackendError if backend is not found, or name == :memory or :redis
90
+ # @return [Boolean]
91
+ def delete_backend(name:)
92
+ forbidden_mes = 'Delete forbidden for backend in [:redis,:memory]'
93
+ notfound_mes = "backend #{name} not found"
94
+ raise Sc4ry::Exceptions::Sc4ryBackendError, forbidden_mes if %i[memory redis].include? name
95
+ raise Sc4ry::Exceptions::Sc4ryBackendError, notfound_mes unless @@backends.include? name
96
+
97
+ @@backends.delete(name)
98
+ end
99
+
100
+ # change the specified backend config
101
+ # @param [Symbol] name the name of the target backend
102
+ # @param [Hash] config the config of the specified backend
103
+ # @raise Sc4ry::Exceptions::Sc4ryBackendError if backend is not found, or name == :memory
104
+ def config_backend(name:, config:)
105
+ raise Sc4ry::Exceptions::Sc4ryBackendError, "backend #{name} not found" unless @@backends.include? name
106
+ raise Sc4ry::Exceptions::Sc4ryBackendError, 'backend :memory not need config' if name == :memory
107
+
108
+ @@backends[name][:config] = config
109
+ end
41
110
  end
42
- end
111
+ end
data/lib/sc4ry/version.rb CHANGED
@@ -1,3 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'version'
4
+
5
+ # Sc4ry module
6
+ # @note namespace
1
7
  module Sc4ry
2
- VERSION = "0.1.7"
8
+ # the version of Sc4ry
9
+ VERSION = Version.current
3
10
  end
data/lib/sc4ry.rb CHANGED
@@ -1,10 +1,4 @@
1
- require "sc4ry/version"
2
- require "sc4ry/dependencies"
3
-
4
-
5
-
6
-
7
-
8
-
9
-
1
+ # frozen_string_literal: true
10
2
 
3
+ require 'sc4ry/version'
4
+ require 'sc4ry/dependencies'
data/samples/test.rb ADDED
@@ -0,0 +1,159 @@
1
+ require 'rubygems'
2
+ require 'sc4ry'
3
+
4
+ include Sc4ry
5
+
6
+ # display of default Sc4ry config
7
+ puts '1.1/ CONFIG : Initial default config'
8
+ Circuits.default_config.each do |item, value|
9
+ puts " * #{item} : #{value}"
10
+ end
11
+ puts ''
12
+
13
+ # testing the two ways to configure Sc4ry default config
14
+ puts '1.2/ CONFIG : modifying default config activate timout and set max_time to 12'
15
+ Circuits.merge_default_config diff: { timeout: true }
16
+ Circuits.configure do |spec|
17
+ spec.max_time = 12
18
+ end
19
+ puts ''
20
+
21
+ # display default config, must be override with a nested hash by calling default_config= method
22
+ puts '1.3/ CONFIG : Default config updated:'
23
+ Circuits.default_config.each do |item, value|
24
+ puts " * #{item} : #{value}"
25
+ end
26
+ puts ''
27
+
28
+ # display current data Store
29
+ print '2.1/ STORE : Current datastore backend : '
30
+ puts Circuits.store.current
31
+ puts ''
32
+
33
+ # display available backend
34
+ puts '2.2/ STORE : List of existing backends : '
35
+ Circuits.store.list_backend.each do |backend|
36
+ puts " - #{backend}"
37
+ end
38
+ puts ''
39
+
40
+ # display Redis backend config in store before change
41
+ puts '2.3/ STORE : display default config of redis backend'
42
+ Circuits.store.get_config(backend: :redis).each do |item, value|
43
+ puts " * #{item} : #{value}"
44
+ end
45
+ puts ''
46
+
47
+ # reconfigure a backend
48
+ puts '2.4/ STORE : reconfigure Redis backend'
49
+ Circuits.store.config_backend name: :redis, config: { host: 'localhost', port: 6379, db: 10 }
50
+ puts
51
+
52
+ # display after
53
+ puts '2.5/ STORE : display altered config of redis backend'
54
+ Circuits.store.get_config(backend: :redis).each do |item, value|
55
+ puts " * #{item} : #{value}"
56
+ end
57
+ puts ''
58
+
59
+ # change backend
60
+
61
+ puts '2.6/ STORE : change to redis backend (NEED a Redis installed) '
62
+ puts ' $ docker pull redis:latest'
63
+ puts ' $ docker run --rm -d -p 6379:6379/tcp redis:latest'
64
+ Circuits.store.change_backend name: :redis
65
+ puts ''
66
+
67
+ puts '2.7/ STORE : flush redis backend, just for test, and for idempotency (NEED a Redis installed) '
68
+ Circuits.store.flush
69
+ puts ''
70
+
71
+ # defining a circuit, config must be empty or override from default
72
+ puts '3.1/ CIRCUIT : registering a circuit by merge :'
73
+ Circuits.register circuit: :test, config: { notifiers: %i[prometheus mattermost], exceptions: [Errno::ECONNREFUSED, URI::InvalidURIError] }
74
+ puts ''
75
+
76
+ puts '3.2/ CIRCUIT : registering a circuit by block :'
77
+ Circuits.register circuit: :test2 do |spec|
78
+ spec.exceptions = [Errno::ECONNREFUSED]
79
+ end
80
+ puts ''
81
+
82
+ puts '3.3/ CIRCUIT : registering a circuit by default :'
83
+ Circuits.register circuit: :test3
84
+ puts ''
85
+
86
+ puts '3.4/ CIRCUITS : Circuits list'
87
+ Circuits.list.each do |circuit|
88
+ puts " * #{circuit}"
89
+ end
90
+ puts ''
91
+
92
+ puts '3.5/ CIRCUIT : display a circuit config :test3 :'
93
+ Circuits.get(circuit: :test3).each do |item, value|
94
+ puts " * #{item} : #{value}"
95
+ end
96
+ puts ''
97
+
98
+ puts '3.6/ CIRCUIT : update config of :test3 => :raise_on_opening == true :'
99
+ Circuits.update_config circuit: :test3, config: { raise_on_opening: true }
100
+ puts ''
101
+
102
+ puts '3.7/ CIRCUIT : display a circuit config :test3 after change :'
103
+ Circuits.get(circuit: :test3).each do |item, value|
104
+ puts " * #{item} : #{value}"
105
+ end
106
+ puts ''
107
+
108
+ puts '3.8/ unregister a circuit : :test2 :'
109
+ Circuits.unregister circuit: :test2
110
+ puts ''
111
+
112
+ puts '3.9/ CIRCUITS : Circuits list after unregister'
113
+ Circuits.list.each do |circuit|
114
+ puts " * #{circuit}"
115
+ end
116
+ puts ''
117
+
118
+ # Config an alternate logger
119
+
120
+ puts '4.1/ LOGGER : register a logger on file '
121
+ Circuits.loggers.register name: :perso, instance: ::Logger.new('/tmp/logfile.log')
122
+ puts ''
123
+
124
+ puts '4.2/ LOGGER : get the list of available loggers'
125
+ Circuits.loggers.list_available.each do |logger|
126
+ puts " * #{logger}"
127
+ end
128
+ puts ''
129
+
130
+ puts '4.3/ LOGGER : change logger to :perso'
131
+ Circuits.loggers.current = :perso
132
+ puts ''
133
+
134
+ # sample Mattermost notification
135
+ puts '5/ set notifier mattermost on dummy url, change with your slack or mattermost server'
136
+ Sc4ry::Notifiers.config name: :mattermost, config: { url: 'https://mattermost.mycorp.com', token: '<TOKEN>' }
137
+ puts ''
138
+
139
+ # sample loop
140
+ puts '6/ running circuits test, please wait ... (see /tmp/logfile.log for result)'
141
+ puts ' check endoint status for different result, you cloud use http://github.com/Ultragreen/MockWS for testing endpoint, on an other tty'
142
+ puts ' $ git clone https://github.com/Ultragreen/MockWS.git'
143
+ puts ' $ cd MockWS'
144
+ puts ' $ rackup'
145
+ begin
146
+ 10.times do
147
+ sleep 1
148
+ Circuits.run circuit: :test do
149
+ # for the test choose or build an endpoint you must shutdown
150
+ puts RestClient.get('http://localhost:9292/test2/data')
151
+ end
152
+ end
153
+ rescue Interrupt
154
+ puts 'Interrputed'
155
+ ensure
156
+ Circuits.store.flush
157
+ end
158
+
159
+ puts 'end'
data/sc4ry.gemspec CHANGED
@@ -1,27 +1,31 @@
1
- require_relative 'lib/sc4ry/version'
1
+ # frozen_string_literal: true
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.name = "sc4ry"
5
- spec.version = Sc4ry::VERSION
6
- spec.authors = ["Romain GEORGES"]
7
- spec.email = ["romain.georges@orange.com"]
8
-
9
- spec.summary = %q{Sc4Ry is Simple Circuitbreaker 4 RubY}
10
- spec.description = %q{Sc4ry provide the design pattern Circuit breaker for your application.}
11
- spec.homepage = "https://github.com/Ultragreen/sc4ry"
12
- spec.license = "MIT"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
4
+ spec.name = 'sc4ry'
5
+ spec.version = `cat VERSION`.chomp
6
+ spec.authors = ['Romain GEORGES']
7
+ spec.email = ['romain.georges@orange.com']
14
8
 
9
+ spec.summary = 'Sc4Ry is Simple Circuitbreaker 4 RubY'
10
+ spec.description = 'Sc4ry provide the design pattern Circuit breaker for your application.'
11
+ spec.homepage = 'https://github.com/Ultragreen/sc4ry'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
15
14
 
16
15
  # Specify which files should be added to the gem when it is released.
17
16
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
18
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
19
  end
21
- spec.require_paths = ["lib"]
22
-
23
- spec.add_dependency "prometheus-client", "~> 3.0"
24
- spec.add_dependency "rest-client", "~> 2.1"
20
+ spec.require_paths = ['lib']
25
21
 
22
+ spec.add_dependency 'prometheus-client', '~> 3.0'
23
+ spec.add_dependency 'redis', '~> 4.6'
24
+ spec.add_dependency 'rest-client', '~> 2.1'
26
25
 
26
+ spec.add_development_dependency 'code_statistics', '~> 0.2.13'
27
+ spec.add_development_dependency 'rubocop', '~> 1.32'
28
+ spec.add_development_dependency 'yard', '~> 0.9.27'
29
+ spec.add_development_dependency 'yard-rspec', '~> 0.1'
30
+ spec.add_dependency 'version', '~> 1.1'
27
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sc4ry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain GEORGES
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-08 00:00:00.000000000 Z
11
+ date: 2022-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prometheus-client
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.6'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rest-client
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,76 @@ dependencies:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: code_statistics
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.13
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.13
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.32'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.32'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.9.27
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.9.27
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: version
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.1'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.1'
41
125
  description: Sc4ry provide the design pattern Circuit breaker for your application.
42
126
  email:
43
127
  - romain.georges@orange.com
@@ -45,14 +129,17 @@ executables: []
45
129
  extensions: []
46
130
  extra_rdoc_files: []
47
131
  files:
132
+ - ".github/workflows/main.yml"
48
133
  - ".gitignore"
49
134
  - ".rspec"
135
+ - ".rubocop.yml"
50
136
  - Gemfile
51
137
  - LICENSE.txt
52
138
  - README.md
53
139
  - Rakefile
140
+ - VERSION
54
141
  - assets/images/logo_sc4ry.png
55
- - assets/logo_sc4ry.png
142
+ - assets/images/sc4ry_workflow.png
56
143
  - bin/console
57
144
  - bin/setup
58
145
  - lib/sc4ry.rb
@@ -60,6 +147,8 @@ files:
60
147
  - lib/sc4ry/backends/memory.rb
61
148
  - lib/sc4ry/backends/redis.rb
62
149
  - lib/sc4ry/circuits.rb
150
+ - lib/sc4ry/config.rb
151
+ - lib/sc4ry/constants.rb
63
152
  - lib/sc4ry/dependencies.rb
64
153
  - lib/sc4ry/exceptions.rb
65
154
  - lib/sc4ry/exporters/init.rb
@@ -71,12 +160,13 @@ files:
71
160
  - lib/sc4ry/run_controller.rb
72
161
  - lib/sc4ry/store.rb
73
162
  - lib/sc4ry/version.rb
163
+ - samples/test.rb
74
164
  - sc4ry.gemspec
75
165
  homepage: https://github.com/Ultragreen/sc4ry
76
166
  licenses:
77
167
  - MIT
78
168
  metadata: {}
79
- post_install_message:
169
+ post_install_message:
80
170
  rdoc_options: []
81
171
  require_paths:
82
172
  - lib
@@ -84,15 +174,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
174
  requirements:
85
175
  - - ">="
86
176
  - !ruby/object:Gem::Version
87
- version: 2.3.0
177
+ version: 2.6.0
88
178
  required_rubygems_version: !ruby/object:Gem::Requirement
89
179
  requirements:
90
180
  - - ">="
91
181
  - !ruby/object:Gem::Version
92
182
  version: '0'
93
183
  requirements: []
94
- rubygems_version: 3.1.2
95
- signing_key:
184
+ rubygems_version: 3.2.3
185
+ signing_key:
96
186
  specification_version: 4
97
187
  summary: Sc4Ry is Simple Circuitbreaker 4 RubY
98
188
  test_files: []
Binary file