unleash 3.2.5 → 4.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/pull_request.yml +73 -0
- data/.gitignore +3 -0
- data/.rubocop.yml +114 -6
- data/README.md +247 -27
- data/bin/unleash-client +15 -5
- data/examples/bootstrap.rb +51 -0
- data/examples/default-toggles.json +42 -0
- data/examples/simple.rb +4 -3
- data/lib/unleash/bootstrap/configuration.rb +25 -0
- data/lib/unleash/bootstrap/handler.rb +22 -0
- data/lib/unleash/bootstrap/provider/base.rb +14 -0
- data/lib/unleash/bootstrap/provider/from_file.rb +14 -0
- data/lib/unleash/bootstrap/provider/from_url.rb +19 -0
- data/lib/unleash/client.rb +25 -15
- data/lib/unleash/configuration.rb +25 -10
- data/lib/unleash/constraint.rb +88 -10
- data/lib/unleash/context.rb +3 -2
- data/lib/unleash/feature_toggle.rb +21 -14
- data/lib/unleash/metrics_reporter.rb +18 -4
- data/lib/unleash/scheduled_executor.rb +5 -2
- data/lib/unleash/strategy/application_hostname.rb +1 -0
- data/lib/unleash/strategy/flexible_rollout.rb +5 -5
- data/lib/unleash/strategy/remote_address.rb +17 -1
- data/lib/unleash/toggle_fetcher.rb +33 -13
- data/lib/unleash/util/http.rb +7 -6
- data/lib/unleash/variant_definition.rb +5 -4
- data/lib/unleash/version.rb +1 -1
- data/lib/unleash.rb +0 -5
- data/unleash-client.gemspec +2 -1
- metadata +31 -10
- data/.travis.yml +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93e6312bc93458378b877b6e949274295a1e0037af2cf8206d94b9e2852727eb
|
4
|
+
data.tar.gz: ecb08a7c1a4e9b0b63fad299649632443a99f7b8e4752b486970d7656b862e46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eef06ea5a86751e6cfe68306a1d7f7e6c49fb0641c4916ddbdda30b4c1bec51a1349487dc156851229c4d496fe67cac96f0e5c875027998d0252bf154f2e02c9
|
7
|
+
data.tar.gz: 6fdf04c5e92b1de875ada2035b9fb495e80cf5138f4cd0171bd6686026a1280857c855ccdaa9bd15a915509f5bac4f9da50d08368f7de663c0a762b8654a3112
|
@@ -0,0 +1,73 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
pull_request:
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
|
10
|
+
runs-on: ${{ matrix.os }}-latest
|
11
|
+
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
os:
|
15
|
+
- ubuntu
|
16
|
+
- macos
|
17
|
+
ruby-version:
|
18
|
+
- jruby-9.2
|
19
|
+
- jruby-9.3
|
20
|
+
- 3.1
|
21
|
+
- 3.0
|
22
|
+
- 2.7
|
23
|
+
- 2.6
|
24
|
+
- 2.5
|
25
|
+
|
26
|
+
steps:
|
27
|
+
- uses: actions/checkout@v2
|
28
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
29
|
+
uses: ruby/setup-ruby@v1
|
30
|
+
with:
|
31
|
+
bundler-cache: true
|
32
|
+
ruby-version: ${{ matrix.ruby-version }}
|
33
|
+
- name: Install dependencies
|
34
|
+
run: bundle install
|
35
|
+
- name: Download test cases
|
36
|
+
run: git clone --depth 5 --branch v4.1.0 https://github.com/Unleash/client-specification.git client-specification
|
37
|
+
- name: rubocop
|
38
|
+
uses: reviewdog/action-rubocop@v2
|
39
|
+
with:
|
40
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
41
|
+
rubocop_version: gemfile
|
42
|
+
rubocop_extensions: rubocop-rspec:gemfile
|
43
|
+
reporter: github-pr-review # Default is github-pr-check
|
44
|
+
- name: Run tests
|
45
|
+
run: bundle exec rake
|
46
|
+
env:
|
47
|
+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
48
|
+
- name: Coveralls Parallel
|
49
|
+
uses: coverallsapp/github-action@master
|
50
|
+
with:
|
51
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
52
|
+
flag-name: run-${{ matrix.test_number }}
|
53
|
+
parallel: true
|
54
|
+
- name: Notify Slack of pipeline completion
|
55
|
+
uses: 8398a7/action-slack@v3
|
56
|
+
if: ${{ github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name }}
|
57
|
+
with:
|
58
|
+
status: ${{ job.status }}
|
59
|
+
text: Built on ${{ matrix.os }} - Ruby ${{ matrix.ruby-version }}
|
60
|
+
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
|
61
|
+
env:
|
62
|
+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|
63
|
+
|
64
|
+
finish:
|
65
|
+
needs: test
|
66
|
+
runs-on: ubuntu-latest
|
67
|
+
steps:
|
68
|
+
- name: Coveralls Finished
|
69
|
+
uses: coverallsapp/github-action@master
|
70
|
+
with:
|
71
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
72
|
+
parallel-finished: true
|
73
|
+
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -7,24 +7,24 @@ Naming/PredicateName:
|
|
7
7
|
AllowedMethods:
|
8
8
|
- is_enabled?
|
9
9
|
|
10
|
+
|
10
11
|
Metrics/ClassLength:
|
11
|
-
Max:
|
12
|
+
Max: 125
|
12
13
|
Layout/LineLength:
|
13
14
|
Max: 140
|
14
15
|
Metrics/MethodLength:
|
15
16
|
Max: 20
|
16
17
|
Metrics/BlockLength:
|
17
|
-
Max:
|
18
|
+
Max: 110
|
18
19
|
Exclude:
|
19
|
-
- 'spec
|
20
|
-
- 'spec/unleash/feature_toggle_spec.rb'
|
20
|
+
- 'spec/**/*.rb'
|
21
21
|
|
22
22
|
Metrics/AbcSize:
|
23
|
-
Max:
|
23
|
+
Max: 30
|
24
24
|
Metrics/CyclomaticComplexity:
|
25
25
|
Max: 9
|
26
26
|
Metrics/PerceivedComplexity:
|
27
|
-
Max:
|
27
|
+
Max: 10
|
28
28
|
|
29
29
|
Style/Documentation:
|
30
30
|
Enabled: false
|
@@ -51,6 +51,12 @@ Style/HashTransformKeys:
|
|
51
51
|
Enabled: true
|
52
52
|
Style/HashTransformValues:
|
53
53
|
Enabled: true
|
54
|
+
Style/EmptyElse:
|
55
|
+
Exclude:
|
56
|
+
- 'lib/unleash/strategy/flexible_rollout.rb'
|
57
|
+
|
58
|
+
Style/DoubleNegation:
|
59
|
+
Enabled: false
|
54
60
|
|
55
61
|
Style/IfInsideElse:
|
56
62
|
Exclude:
|
@@ -60,6 +66,61 @@ Style/Next:
|
|
60
66
|
Exclude:
|
61
67
|
- 'lib/unleash/scheduled_executor.rb'
|
62
68
|
|
69
|
+
|
70
|
+
Style/AccessorGrouping:
|
71
|
+
Enabled: true
|
72
|
+
Style/BisectedAttrAccessor:
|
73
|
+
Enabled: true
|
74
|
+
Style/CaseLikeIf:
|
75
|
+
Enabled: true
|
76
|
+
#Style/ClassEqualityComparison:
|
77
|
+
# Enabled: true
|
78
|
+
Style/CombinableLoops:
|
79
|
+
Enabled: true
|
80
|
+
Style/ExplicitBlockArgument:
|
81
|
+
Enabled: true
|
82
|
+
Style/ExponentialNotation:
|
83
|
+
Enabled: true
|
84
|
+
#Style/GlobalStdStream:
|
85
|
+
# Enabled: true
|
86
|
+
Style/HashAsLastArrayItem:
|
87
|
+
Enabled: true
|
88
|
+
Style/HashLikeCase:
|
89
|
+
Enabled: true
|
90
|
+
Style/KeywordParametersOrder:
|
91
|
+
Enabled: true
|
92
|
+
#Style/OptionalBooleanParameter:
|
93
|
+
# Enabled: false
|
94
|
+
Style/RedundantAssignment:
|
95
|
+
Enabled: true
|
96
|
+
Style/RedundantFetchBlock:
|
97
|
+
Enabled: true
|
98
|
+
Style/RedundantFileExtensionInRequire:
|
99
|
+
Enabled: true
|
100
|
+
Style/RedundantRegexpCharacterClass:
|
101
|
+
Enabled: true
|
102
|
+
Style/RedundantRegexpEscape:
|
103
|
+
Enabled: true
|
104
|
+
Style/RedundantSelfAssignment:
|
105
|
+
Enabled: true
|
106
|
+
Style/SingleArgumentDig:
|
107
|
+
Enabled: true
|
108
|
+
Style/SlicingWithRange:
|
109
|
+
Enabled: true
|
110
|
+
Style/SoleNestedConditional:
|
111
|
+
Enabled: true
|
112
|
+
Style/StringConcatenation:
|
113
|
+
Enabled: false
|
114
|
+
Style/TrailingCommaInHashLiteral:
|
115
|
+
Enabled: true
|
116
|
+
# EnforcedStyleForMultiline: consistent_comma
|
117
|
+
|
118
|
+
Layout/BeginEndAlignment:
|
119
|
+
Enabled: true
|
120
|
+
Layout/EmptyLinesAroundAttributeAccessor:
|
121
|
+
Enabled: true
|
122
|
+
Layout/SpaceAroundMethodCallOperator:
|
123
|
+
Enabled: true
|
63
124
|
Layout/MultilineMethodCallIndentation:
|
64
125
|
EnforcedStyle: indented
|
65
126
|
|
@@ -68,3 +129,50 @@ Layout/SpaceBeforeBlockBraces:
|
|
68
129
|
Exclude:
|
69
130
|
- 'unleash-client.gemspec'
|
70
131
|
- 'spec/**/*.rb'
|
132
|
+
|
133
|
+
Lint/BinaryOperatorWithIdenticalOperands:
|
134
|
+
Enabled: true
|
135
|
+
Lint/ConstantDefinitionInBlock:
|
136
|
+
Enabled: false
|
137
|
+
Lint/DeprecatedOpenSSLConstant:
|
138
|
+
Enabled: true
|
139
|
+
Lint/DuplicateElsifCondition:
|
140
|
+
Enabled: true
|
141
|
+
Lint/DuplicateRequire:
|
142
|
+
Enabled: true
|
143
|
+
Lint/DuplicateRescueException:
|
144
|
+
Enabled: true
|
145
|
+
Lint/EmptyConditionalBody:
|
146
|
+
Enabled: true
|
147
|
+
Lint/EmptyFile:
|
148
|
+
Enabled: true
|
149
|
+
Lint/FloatComparison:
|
150
|
+
Enabled: true
|
151
|
+
Lint/HashCompareByIdentity:
|
152
|
+
Enabled: true
|
153
|
+
Lint/IdentityComparison:
|
154
|
+
Enabled: true
|
155
|
+
Lint/MissingSuper:
|
156
|
+
Enabled: false
|
157
|
+
Lint/MixedRegexpCaptureTypes:
|
158
|
+
Enabled: true
|
159
|
+
Lint/OutOfRangeRegexpRef:
|
160
|
+
Enabled: true
|
161
|
+
Lint/RaiseException:
|
162
|
+
Enabled: true
|
163
|
+
Lint/RedundantSafeNavigation:
|
164
|
+
Enabled: true
|
165
|
+
Lint/SelfAssignment:
|
166
|
+
Enabled: true
|
167
|
+
Lint/StructNewOverride:
|
168
|
+
Enabled: true
|
169
|
+
Lint/TopLevelReturnWithArgument:
|
170
|
+
Enabled: true
|
171
|
+
Lint/TrailingCommaInAttributeDeclaration:
|
172
|
+
Enabled: true
|
173
|
+
Lint/UnreachableLoop:
|
174
|
+
Enabled: true
|
175
|
+
Lint/UselessMethodDefinition:
|
176
|
+
Enabled: true
|
177
|
+
Lint/UselessTimes:
|
178
|
+
Enabled: true
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# Unleash::Client
|
2
2
|
|
3
|
-
|
4
|
-
[![Coverage Status](https://coveralls.io/repos/github/Unleash/unleash-client-ruby/badge.svg?branch=
|
3
|
+
![Build Status](https://github.com/Unleash/unleash-client-ruby/actions/workflows/pull_request.yml/badge.svg?branch=main)
|
4
|
+
[![Coverage Status](https://coveralls.io/repos/github/Unleash/unleash-client-ruby/badge.svg?branch=main)](https://coveralls.io/github/Unleash/unleash-client-ruby?branch=main)
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/unleash.svg)](https://badge.fury.io/rb/unleash)
|
6
6
|
|
7
7
|
Unleash client so you can roll out your features with confidence.
|
@@ -10,18 +10,20 @@ Leverage the [Unleash Server](https://github.com/Unleash/unleash) for powerful f
|
|
10
10
|
|
11
11
|
## Supported Ruby Interpreters
|
12
12
|
|
13
|
+
* MRI 3.1
|
13
14
|
* MRI 3.0
|
14
15
|
* MRI 2.7
|
15
16
|
* MRI 2.6
|
16
17
|
* MRI 2.5
|
17
|
-
* jruby
|
18
|
+
* jruby 9.3
|
19
|
+
* jruby 9.2
|
18
20
|
|
19
21
|
## Installation
|
20
22
|
|
21
23
|
Add this line to your application's Gemfile:
|
22
24
|
|
23
25
|
```ruby
|
24
|
-
gem 'unleash', '~>
|
26
|
+
gem 'unleash', '~> 4.0.0'
|
25
27
|
```
|
26
28
|
|
27
29
|
And then execute:
|
@@ -34,22 +36,29 @@ Or install it yourself as:
|
|
34
36
|
|
35
37
|
## Configure
|
36
38
|
|
37
|
-
It is **required** to configure
|
39
|
+
It is **required** to configure:
|
40
|
+
- `url` of the unleash server
|
41
|
+
- `app_name` with the name of the runninng application.
|
42
|
+
- `custom_http_headers` with `{'Authorization': '<API token>'}` when using Unleash v4.0.0 and later.
|
38
43
|
|
39
|
-
|
44
|
+
Please substitute the example `'http://unleash.herokuapp.com/api'` for the url of your own instance.
|
45
|
+
|
46
|
+
It is **highly recommended** to configure:
|
47
|
+
- `instance_id` parameter with a unique identifier for the running instance.
|
40
48
|
|
41
49
|
|
42
50
|
```ruby
|
43
51
|
Unleash.configure do |config|
|
44
|
-
config.
|
45
|
-
config.
|
52
|
+
config.app_name = 'my_ruby_app'
|
53
|
+
config.url = 'http://unleash.herokuapp.com/api'
|
54
|
+
config.custom_http_headers = {'Authorization': '<API token>'}
|
46
55
|
end
|
47
56
|
```
|
48
57
|
|
49
58
|
or instantiate the client with the valid configuration:
|
50
59
|
|
51
60
|
```ruby
|
52
|
-
UNLEASH = Unleash::Client.new(url: 'http://unleash.herokuapp.com/api', app_name: 'my_ruby_app')
|
61
|
+
UNLEASH = Unleash::Client.new(url: 'http://unleash.herokuapp.com/api', app_name: 'my_ruby_app', custom_http_headers: {'Authorization': '<API token>'})
|
53
62
|
```
|
54
63
|
|
55
64
|
#### List of Arguments
|
@@ -60,19 +69,25 @@ Argument | Description | Required? | Type | Default Value|
|
|
60
69
|
`app_name` | Name of your program. | Y | String | N/A |
|
61
70
|
`instance_id` | Identifier for the running instance of program. Important so you can trace back to where metrics are being collected from. **Highly recommended be be set.** | N | String | random UUID |
|
62
71
|
`environment` | Environment the program is running on. Could be for example `prod` or `dev`. Not yet in use. | N | String | `default` |
|
72
|
+
`project_name` | Name of the project to retrieve features from. If not set, all feature flags will be retrieved. | N | String | nil |
|
63
73
|
`refresh_interval` | How often the unleash client should check with the server for configuration changes. | N | Integer | 15 |
|
64
|
-
`metrics_interval` | How often the unleash client should send metrics to server. | N | Integer |
|
74
|
+
`metrics_interval` | How often the unleash client should send metrics to server. | N | Integer | 60 |
|
65
75
|
`disable_client` | Disables all communication with the Unleash server, effectively taking it *offline*. If set, `is_enabled?` will always answer with the `default_value` and configuration validation is skipped. Defeats the entire purpose of using unleash, but can be useful in when running tests. | N | Boolean | `false` |
|
66
76
|
`disable_metrics` | Disables sending metrics to Unleash server. | N | Boolean | `false` |
|
67
|
-
`custom_http_headers` | Custom headers to send to Unleash. | N | Hash | {} |
|
77
|
+
`custom_http_headers` | Custom headers to send to Unleash. As of Unleash v4.0.0, the `Authorization` header is required. For example: `{'Authorization': '<API token>'}` | N | Hash | {} |
|
68
78
|
`timeout` | How long to wait for the connection to be established or wait in reading state (open_timeout/read_timeout) | N | Integer | 30 |
|
69
|
-
`retry_limit` | How many consecutive failures in connecting to the Unleash server are allowed before giving up.
|
79
|
+
`retry_limit` | How many consecutive failures in connecting to the Unleash server are allowed before giving up. The default is to retry indefinitely. | N | Float::INFINITY | 5 |
|
70
80
|
`backup_file` | Filename to store the last known state from the Unleash server. Best to not change this from the default. | N | String | `Dir.tmpdir + "/unleash-#{app_name}-repo.json` |
|
71
81
|
`logger` | Specify a custom `Logger` class to handle logs for the Unleash client. | N | Class | `Logger.new(STDOUT)` |
|
72
|
-
`log_level` | Change the log level for the `Logger` class. Constant from `Logger::Severity`. | N | Constant | `Logger::
|
82
|
+
`log_level` | Change the log level for the `Logger` class. Constant from `Logger::Severity`. | N | Constant | `Logger::WARN` |
|
83
|
+
`bootstrap_config` | Bootstrap config on how to loaded data on start-up. This is useful for loading large states on startup without (or before) hitting the network. | N | Unleash::Bootstrap::Configuration | `nil` |
|
73
84
|
|
74
|
-
For
|
85
|
+
For a more in-depth look, please see `lib/unleash/configuration.rb`.
|
75
86
|
|
87
|
+
Environment Variable | Description
|
88
|
+
---------|---------
|
89
|
+
`UNLEASH_BOOTSTRAP_FILE` | File to read bootstrap data from
|
90
|
+
`UNLEASH_BOOTSTRAP_URL` | URL to read bootstrap data from
|
76
91
|
|
77
92
|
## Usage in a plain Ruby Application
|
78
93
|
|
@@ -80,7 +95,7 @@ For in a more in depth look, please see `lib/unleash/configuration.rb`.
|
|
80
95
|
require 'unleash'
|
81
96
|
require 'unleash/context'
|
82
97
|
|
83
|
-
@unleash = Unleash::Client.new(url: 'http://unleash.herokuapp.com/api',
|
98
|
+
@unleash = Unleash::Client.new(app_name: 'my_ruby_app', url: 'http://unleash.herokuapp.com/api', custom_http_headers: { 'Authorization': '<API token>' })
|
84
99
|
|
85
100
|
feature_name = "AwesomeFeature"
|
86
101
|
unleash_context = Unleash::Context.new
|
@@ -101,37 +116,145 @@ Put in `config/initializers/unleash.rb`:
|
|
101
116
|
|
102
117
|
```ruby
|
103
118
|
Unleash.configure do |config|
|
104
|
-
config.url = 'http://unleash.herokuapp.com/api'
|
105
119
|
config.app_name = Rails.application.class.parent.to_s
|
120
|
+
config.url = 'http://unleash.herokuapp.com/api'
|
106
121
|
# config.instance_id = "#{Socket.gethostname}"
|
107
122
|
config.logger = Rails.logger
|
108
123
|
config.environment = Rails.env
|
109
124
|
end
|
110
125
|
|
111
126
|
UNLEASH = Unleash::Client.new
|
127
|
+
|
128
|
+
# Or if preferred:
|
129
|
+
# Rails.configuration.unleash = Unleash::Client.new
|
130
|
+
```
|
131
|
+
For `config.instance_id` use a string with a unique identification for the running instance.
|
132
|
+
For example: it could be the hostname, if you only run one App per host.
|
133
|
+
Or the docker container id, if you are running in docker.
|
134
|
+
If it is not set the client will generate an unique UUID for each execution.
|
135
|
+
|
136
|
+
To have it available in the `rails console` command as well, also add to the file above:
|
137
|
+
```ruby
|
138
|
+
Rails.application.console do
|
139
|
+
UNLEASH = Unleash::Client.new
|
140
|
+
# or
|
141
|
+
# Rails.configuration.unleash = Unleash::Client.new
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
#### Add Initializer if using [Puma in clustered mode](https://github.com/puma/puma#clustered-mode)
|
146
|
+
|
147
|
+
That is, multiple workers configured in `puma.rb`:
|
148
|
+
```ruby
|
149
|
+
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
|
150
|
+
```
|
151
|
+
|
152
|
+
##### with `preload_app!`
|
153
|
+
|
154
|
+
Then you may keep the client configuration still in `config/initializers/unleash.rb`:
|
155
|
+
```ruby
|
156
|
+
Unleash.configure do |config|
|
157
|
+
config.app_name = Rails.application.class.parent.to_s
|
158
|
+
config.environment = Rails.env
|
159
|
+
config.url = 'http://unleash.herokuapp.com/api'
|
160
|
+
config.custom_http_headers = {'Authorization': '<API token>'}
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
But you must ensure that the unleash client is instantiated only after the process is forked.
|
165
|
+
This is done by creating the client inside the `on_worker_boot` code block in `puma.rb` as below:
|
166
|
+
|
167
|
+
```ruby
|
168
|
+
#...
|
169
|
+
preload_app!
|
170
|
+
#...
|
171
|
+
|
172
|
+
on_worker_boot do
|
173
|
+
# ...
|
174
|
+
|
175
|
+
::UNLEASH = Unleash::Client.new
|
176
|
+
end
|
177
|
+
|
178
|
+
on_worker_shutdown do
|
179
|
+
::UNLEASH.shutdown
|
180
|
+
end
|
112
181
|
```
|
113
|
-
For `config.instance_id` use a string with a unique identification for the running instance. For example: it could be the hostname, if you only run one App per host. Or the docker container id, if you are running in docker. If it is not set the client will generate an unique UUID for each execution.
|
114
182
|
|
183
|
+
##### without `preload_app!`
|
115
184
|
|
116
|
-
|
185
|
+
By not using `preload_app!`:
|
186
|
+
- the `Rails` constant will NOT be available.
|
187
|
+
- but phased restarts will be possible.
|
117
188
|
|
118
|
-
|
189
|
+
You need to ensure that in `puma.rb`:
|
190
|
+
- loading unleash sdk with `require 'unleash'` explicitly, as it will not be pre-loaded.
|
191
|
+
- all parameters must be explicitly set in the `on_worker_boot` block, as `config/initializers/unleash.rb` is not read.
|
192
|
+
- there are no references to `Rails` constant, as that is not yet available.
|
119
193
|
|
194
|
+
Example for `puma.rb`:
|
120
195
|
```ruby
|
196
|
+
require 'unleash'
|
197
|
+
|
198
|
+
#...
|
199
|
+
# no preload_app!
|
200
|
+
|
121
201
|
on_worker_boot do
|
122
202
|
# ...
|
123
203
|
|
124
|
-
Unleash.
|
125
|
-
|
126
|
-
|
127
|
-
|
204
|
+
::UNLEASH = Unleash::Client.new(
|
205
|
+
app_name: 'my_rails_app',
|
206
|
+
environment: 'development',
|
207
|
+
url: 'http://unleash.herokuapp.com/api',
|
208
|
+
custom_http_headers: {'Authorization': '<API token>'},
|
209
|
+
)
|
210
|
+
end
|
211
|
+
|
212
|
+
on_worker_shutdown do
|
213
|
+
::UNLEASH.shutdown
|
214
|
+
end
|
215
|
+
```
|
216
|
+
|
217
|
+
Note that we also added shutdown hooks in `on_worker_shutdown`, to ensure a clean shutdown.
|
218
|
+
|
219
|
+
#### Add Initializer if using [Phusion Passenger](https://github.com/phusion/passenger)
|
220
|
+
|
221
|
+
The unleash client needs to be configured and instantiated inside the `PhusionPassenger.on_event(:starting_worker_process)` code block due to [smart spawning](https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/#smart-spawning-caveats):
|
222
|
+
|
223
|
+
The initializer in `config/initializers/unleash.rb` should look like:
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
227
|
+
if forked
|
228
|
+
Unleash.configure do |config|
|
229
|
+
config.app_name = Rails.application.class.parent.to_s
|
230
|
+
# config.instance_id = "#{Socket.gethostname}"
|
231
|
+
config.logger = Rails.logger
|
232
|
+
config.environment = Rails.env
|
233
|
+
config.url = 'http://unleash.herokuapp.com/api'
|
234
|
+
config.custom_http_headers = {'Authorization': '<API token>'}
|
235
|
+
end
|
236
|
+
|
237
|
+
UNLEASH = Unleash::Client.new
|
128
238
|
end
|
129
|
-
Rails.configuration.unleash = Unleash::Client.new
|
130
239
|
end
|
131
240
|
```
|
132
241
|
|
133
|
-
|
242
|
+
#### Add Initializer hooks when using within [Sidekiq](https://github.com/mperham/sidekiq)
|
243
|
+
|
244
|
+
Note that in this case we require that the code block for `Unleash.configure` is set beforehand.
|
245
|
+
For example in `config/initializers/unleash.rb`.
|
134
246
|
|
247
|
+
```ruby
|
248
|
+
Sidekiq.configure_server do |config|
|
249
|
+
config.on(:startup) do
|
250
|
+
UNLEASH = Unleash::Client.new
|
251
|
+
end
|
252
|
+
|
253
|
+
config.on(:shutdown) do
|
254
|
+
UNLEASH.shutdown
|
255
|
+
end
|
256
|
+
end
|
257
|
+
```
|
135
258
|
|
136
259
|
#### Set Unleash::Context
|
137
260
|
|
@@ -172,7 +295,8 @@ if Rails.configuration.unleash.is_enabled? "AwesomeFeature", @unleash_context
|
|
172
295
|
end
|
173
296
|
```
|
174
297
|
|
175
|
-
If the feature is not found in the server, it will by default return false.
|
298
|
+
If the feature is not found in the server, it will by default return false.
|
299
|
+
However you can override that by setting the default return value to `true`:
|
176
300
|
|
177
301
|
```ruby
|
178
302
|
if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context, true
|
@@ -180,6 +304,40 @@ if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context, true
|
|
180
304
|
end
|
181
305
|
```
|
182
306
|
|
307
|
+
Another possibility is to send a block, [Lambda](https://ruby-doc.org/core-3.0.1/Kernel.html#method-i-lambda) or [Proc](https://ruby-doc.org/core-3.0.1/Proc.html#method-i-yield)
|
308
|
+
to evaluate the default value:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
net_check_proc = proc do |feature_name, context|
|
312
|
+
context.remote_address.starts_with?("10.0.0.")
|
313
|
+
end
|
314
|
+
|
315
|
+
if UNLEASH.is_enabled?("AwesomeFeature", @unleash_context, &net_check_proc)
|
316
|
+
puts "AwesomeFeature is enabled by default if you are in the 10.0.0.* network."
|
317
|
+
end
|
318
|
+
```
|
319
|
+
|
320
|
+
or
|
321
|
+
|
322
|
+
```ruby
|
323
|
+
awesomeness = 10
|
324
|
+
@unleash_context.properties[:coolness] = 10
|
325
|
+
|
326
|
+
if UNLEASH.is_enabled?("AwesomeFeature", @unleash_context) { |feat, ctx| awesomeness >= 6 && ctx.properties[:coolness] >= 8 }
|
327
|
+
puts "AwesomeFeature is enabled by default if both the user has a high enought coolness and the application has a high enough awesomeness"
|
328
|
+
end
|
329
|
+
```
|
330
|
+
|
331
|
+
Note:
|
332
|
+
- The block/lambda/proc can use feature name and context as an arguments.
|
333
|
+
- The client will evaluate the fallback function once per call of `is_enabled()`.
|
334
|
+
Please keep this in mind when creating your fallback function!
|
335
|
+
- The returned value of the block should be a boolean.
|
336
|
+
However the client will coerce the result to boolean via `!!`.
|
337
|
+
- If both a `default_value` and `fallback_function` are supplied,
|
338
|
+
the client will define the default value by `OR`ing the default value and the output of the fallback function.
|
339
|
+
|
340
|
+
|
183
341
|
Alternatively by using `if_enabled` you can send a code block to be executed as a parameter:
|
184
342
|
|
185
343
|
```ruby
|
@@ -188,6 +346,8 @@ UNLEASH.if_enabled "AwesomeFeature", @unleash_context, true do
|
|
188
346
|
end
|
189
347
|
```
|
190
348
|
|
349
|
+
Note: `if_enabled` only supports `default_value`, but not `fallback_function`.
|
350
|
+
|
191
351
|
##### Variations
|
192
352
|
|
193
353
|
If no variant is found in the server, use the fallback variant.
|
@@ -199,6 +359,65 @@ variant = UNLEASH.get_variant "ColorVariants", @unleash_context, fallback_varian
|
|
199
359
|
puts "variant color is: #{variant.payload.fetch('color')}"
|
200
360
|
```
|
201
361
|
|
362
|
+
## Bootstrapping
|
363
|
+
|
364
|
+
Bootstrap configuration allows the client to be initialized with a predefined set of toggle states.
|
365
|
+
Bootstrapping can be configured by providing a bootstrap configuration when initializing the client.
|
366
|
+
```ruby
|
367
|
+
@unleash = Unleash::Client.new(
|
368
|
+
url: 'http://unleash.herokuapp.com/api',
|
369
|
+
app_name: 'my_ruby_app',
|
370
|
+
custom_http_headers: { 'Authorization': '<API token>' },
|
371
|
+
bootstrap_config: Unleash::Bootstrap::Configuration.new({
|
372
|
+
url: "http://unleash.herokuapp.com/api/client/features",
|
373
|
+
url_headers: {'Authorization': '<API token>'}
|
374
|
+
})
|
375
|
+
)
|
376
|
+
```
|
377
|
+
The `Bootstrap::Configuration` initializer takes a hash with one of the following options specified:
|
378
|
+
|
379
|
+
* `file_path` - An absolute or relative path to a file containing a JSON string of the response body from the Unleash server. This can also be set though the `UNLEASH_BOOTSTRAP_FILE` environment variable.
|
380
|
+
* `url` - A url pointing to an Unleash server's features endpoint, the code sample above is illustrative. This can also be set though the `UNLEASH_BOOTSTRAP_URL` environment variable.
|
381
|
+
* `url_headers` - Headers for the GET http request to the `url` above. Only used if the `url` parameter is also set. If this option isn't set then the bootstrapper will use the same url headers as the Unleash client.
|
382
|
+
* `data` - A raw JSON string as returned by the Unleash server.
|
383
|
+
* `block` - A lambda containing custom logic if you need it, an example is provided below.
|
384
|
+
|
385
|
+
You should only specify one type of bootstrapping since only one will be invoked and the others will be ignored.
|
386
|
+
The order of preference is as follows:
|
387
|
+
|
388
|
+
- Select a data bootstrapper if it exists.
|
389
|
+
- If no data bootstrapper exists, select the block bootstrapper.
|
390
|
+
- If no block bootstrapper exists, select the file bootstrapper from either parameters or the specified environment variable.
|
391
|
+
- If no file bootstrapper exists, then check for a URL bootstrapper from either the parameters or the specified environment variable.
|
392
|
+
|
393
|
+
|
394
|
+
Example usage:
|
395
|
+
|
396
|
+
First saving the toggles locally:
|
397
|
+
```shell
|
398
|
+
curl -H 'Authorization: <API token>' -XGET 'http://unleash.herokuapp.com/api' > ./default-toggles.json
|
399
|
+
```
|
400
|
+
|
401
|
+
Now using them on start up:
|
402
|
+
|
403
|
+
```ruby
|
404
|
+
|
405
|
+
custom_boostrapper = lambda {
|
406
|
+
File.read('./default-toggles.json')
|
407
|
+
}
|
408
|
+
|
409
|
+
@unleash = Unleash::Client.new(
|
410
|
+
app_name: 'my_ruby_app',
|
411
|
+
url: 'http://unleash.herokuapp.com/api',
|
412
|
+
custom_http_headers: { 'Authorization': '<API token>' },
|
413
|
+
bootstrap_config: Unleash::Bootstrap::Configuration.new({
|
414
|
+
block: custom_boostrapper
|
415
|
+
})
|
416
|
+
)
|
417
|
+
```
|
418
|
+
|
419
|
+
This example could be easily achieved with a file bootstrapper, this is just to illustrate the usage of custom bootstrapping.
|
420
|
+
Be aware that the client initializer will block until bootstrapping is complete.
|
202
421
|
|
203
422
|
#### Client methods
|
204
423
|
|
@@ -211,6 +430,7 @@ Method Name | Description | Return Type |
|
|
211
430
|
`shutdown` | Save metrics to disk, flush metrics to server, and then kill ToggleFetcher and MetricsReporter threads. A safe shutdown. Not really useful in long running applications, like web applications. | nil |
|
212
431
|
`shutdown!` | Kill ToggleFetcher and MetricsReporter threads immediately. | nil |
|
213
432
|
|
433
|
+
For the full method signatures, please see [client.rb](lib/unleash/client.rb)
|
214
434
|
|
215
435
|
## Local test client
|
216
436
|
|
@@ -228,6 +448,7 @@ This client comes with the all the required strategies out of the box:
|
|
228
448
|
|
229
449
|
* ApplicationHostnameStrategy
|
230
450
|
* DefaultStrategy
|
451
|
+
* FlexibleRolloutStrategy
|
231
452
|
* GradualRolloutRandomStrategy
|
232
453
|
* GradualRolloutSessionIdStrategy
|
233
454
|
* GradualRolloutUserIdStrategy
|
@@ -235,7 +456,6 @@ This client comes with the all the required strategies out of the box:
|
|
235
456
|
* UnknownStrategy
|
236
457
|
* UserWithIdStrategy
|
237
458
|
|
238
|
-
|
239
459
|
## Development
|
240
460
|
|
241
461
|
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.
|