light_switch 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.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +209 -0
- data/Rakefile +7 -0
- data/app/assets/config/light_switch_manifest.js +2 -0
- data/app/assets/images/light_switch/light-switch.svg +1 -0
- data/app/assets/javascripts/light_switch/application.js +1 -0
- data/app/assets/stylesheets/light_switch/application.css +97 -0
- data/app/assets/stylesheets/light_switch/normalize.css +427 -0
- data/app/assets/stylesheets/light_switch/skeleton.css +418 -0
- data/app/controllers/light_switch/application_controller.rb +4 -0
- data/app/controllers/light_switch/switches_controller.rb +52 -0
- data/app/models/concerns/light_switch/switch/notifications_concern.rb +23 -0
- data/app/models/light_switch/application_record.rb +5 -0
- data/app/models/light_switch/switch.rb +29 -0
- data/app/views/layouts/light_switch/application.html.erb +30 -0
- data/app/views/light_switch/switches/_form.html.erb +6 -0
- data/app/views/light_switch/switches/_form_errors.html.erb +5 -0
- data/app/views/light_switch/switches/_switch.html.erb +23 -0
- data/app/views/light_switch/switches/index.html.erb +22 -0
- data/config/locales/light_switch.en.yml +13 -0
- data/config/routes.rb +5 -0
- data/db/migrate/20231116015256_create_light_switch_switches.rb +11 -0
- data/lib/light_switch/configuration.rb +5 -0
- data/lib/light_switch/engine.rb +27 -0
- data/lib/light_switch/null_cache.rb +10 -0
- data/lib/light_switch/version.rb +3 -0
- data/lib/light_switch.rb +41 -0
- data/lib/tasks/light_switch_tasks.rake +4 -0
- metadata +223 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 675a8f2ae1d5eb4e4beb9afdf243d5bec721eb4a7c3c520353ab74ea3dae8afc
|
|
4
|
+
data.tar.gz: 9df76f5c003d099f5b5bb186c8a931048bd1dcf484d906df252c9ca133820f97
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6ac3c7df6e5330247760c3b0ee8aafd96332553d4e0eb7d25ab8577f16b1f2fbcef9d8b9c5299d7fb0be1be37fa73464b6215e13e45511f238ad1acd06a8a73e
|
|
7
|
+
data.tar.gz: 74295e949fbdc54ec2c73a19137eecc853415fe66d347f65056f1462882cb6bcbc7ecdc069f6f2dc587601992342b644b0bcf514df459f387483c98fc3cadb64
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright 2023 Joel Lubrano
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
# LightSwitch
|
|
2
|
+
|
|
3
|
+
Simple circuit breakers on Rails
|
|
4
|
+
|
|
5
|
+
## Use Cases
|
|
6
|
+
|
|
7
|
+
`LightSwitch` aims to be a very simple circuit breaker implementation. You can
|
|
8
|
+
use a `LightSwitch` to turn things on and to turn things off. That's pretty much
|
|
9
|
+
it.
|
|
10
|
+
|
|
11
|
+
[Stoplight](https://github.com/bolshakov/stoplight) and
|
|
12
|
+
[Circuitbox](https://github.com/yammer/circuitbox) are excellent, full-featured
|
|
13
|
+
[circuit breaker](https://martinfowler.com/bliki/CircuitBreaker.html) libraries.
|
|
14
|
+
[Semian](https://github.com/Shopify/semian) is an even more sophisticated beast.
|
|
15
|
+
`LightSwitch` is much more basic. In particular, `LightSwitch` does not
|
|
16
|
+
dynamically respond and adjust to errors. As the operator of your application,
|
|
17
|
+
you are expected to manually turn switches off and on when you need to.
|
|
18
|
+
|
|
19
|
+
You may ask, "If I have turned off a particular piece of code, how do I know
|
|
20
|
+
when it's okay to turn it back on?" That's a great question but it's not one
|
|
21
|
+
that `LightSwitch` tries to answer for you. `Stoplight` and `Circuitbox` are
|
|
22
|
+
great at that sort of circuit breaking. Maybe you are monitoring a third-party's
|
|
23
|
+
status page. Maybe another team is fixing their service and will let you know
|
|
24
|
+
when the service is back up. Maybe you just turn your piece of code back on and
|
|
25
|
+
monitor things closely.
|
|
26
|
+
|
|
27
|
+
Some apps may not need the sophisticated, powerful behavior of `Stoplight` or
|
|
28
|
+
`Circuitbox`; some apps may not run their recommended infrastructure (e.g. Redis).
|
|
29
|
+
For simple cases where you are already running Rails, you may prefer the
|
|
30
|
+
simplicity of `LightSwitch`.
|
|
31
|
+
|
|
32
|
+
### LightSwitch vs. Flipper
|
|
33
|
+
|
|
34
|
+
`LightSwitch` imitates [Flipper](https://github.com/flippercloud/flipper) when
|
|
35
|
+
it comes to its API. If you want to use `Flipper` for your circuit breaking
|
|
36
|
+
needs, go for it. Some teams prefer to reserve `Flipper` strictly for feature
|
|
37
|
+
flags. Some organizations expose the Flipper UI to a wide range of internal
|
|
38
|
+
users especially if Product Managers enable and disable features on behalf of
|
|
39
|
+
customers. You probably want to limit the exposure of your `LightSwitches`. You
|
|
40
|
+
would not want someone to turn off a piece of code unless they really knew what
|
|
41
|
+
they were doing. `LightSwitch` can give you a little separation between feature
|
|
42
|
+
flags and circuit breakers. `Flipper` is well-suited to do the job if you want
|
|
43
|
+
to use it, though.
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
A `LightSwitch::Switch` has two states, `on` and `off`. This gem's convention
|
|
48
|
+
is that an `off` switch will short circuit a given piece of code. Therefore
|
|
49
|
+
the recommended usage of `LightSwitch` is something like this:
|
|
50
|
+
```ruby
|
|
51
|
+
def my_method
|
|
52
|
+
return if LightSwitch.off?(:my_switch)
|
|
53
|
+
|
|
54
|
+
do_stuff
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Or
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
def my_method
|
|
62
|
+
do_stuff if LightSwitch.on?(:my_switch)
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
You can interpret `off` and `on` however you want; it's up to you.
|
|
67
|
+
What few defaults `LightSwitch` implements assume the convention above, though.
|
|
68
|
+
|
|
69
|
+
### Operating Switches
|
|
70
|
+
|
|
71
|
+
If you have Rails console access, you can turn switches on and off:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
LightSwitch[:my_switch].on! # turns the switch on
|
|
75
|
+
LightSwitch[:my_switch].off! # turns the switch off
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### LightSwitch UI
|
|
79
|
+
|
|
80
|
+
LightSwitch comes with a web UI that can be mounted in your rails application.
|
|
81
|
+
|
|
82
|
+

|
|
83
|
+
|
|
84
|
+
Just add the following to `config/routes.rb`:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
Rails.application.routes.draw do
|
|
88
|
+
mount LightSwitch::Engine => "/light_switch"
|
|
89
|
+
end
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
You can limit access to the UI by using
|
|
93
|
+
[Rails routing constraints](https://guides.rubyonrails.org/routing.html#advanced-constraints).
|
|
94
|
+
|
|
95
|
+
### Configuration
|
|
96
|
+
|
|
97
|
+
You most likely want to initialize all of your switches when your app boots up.
|
|
98
|
+
This ensures that the switches are present in the LightSwitch UI so that you
|
|
99
|
+
can easily manage them. If you forget to initialize a switch, `LightSwitch` will
|
|
100
|
+
assume that the switch is `on` so that your code guarded by a switch will run
|
|
101
|
+
(assuming you follow the conventions demonstrated above). You can initialize
|
|
102
|
+
your switches in a Rails initializer.
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
# config/initializers/light_switch.rb
|
|
106
|
+
|
|
107
|
+
LightSwitch.configure do |config|
|
|
108
|
+
config.switches = %i[
|
|
109
|
+
one_switch
|
|
110
|
+
two_switch
|
|
111
|
+
red_switch
|
|
112
|
+
blue_switch
|
|
113
|
+
]
|
|
114
|
+
end
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Caching
|
|
118
|
+
|
|
119
|
+
By default, `LightSwitch` does not leverage caching, but you can configure
|
|
120
|
+
`LightSwitch` to use a cache if you have performance concerns. For example,
|
|
121
|
+
`Rails.cache` may be configured by setting `LightSwitch.config.cache` in a
|
|
122
|
+
Rails initializer:
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
# config/initializers/light_switch.rb
|
|
126
|
+
|
|
127
|
+
LightSwitch.configure do |config|
|
|
128
|
+
config.cache = Rails.cache
|
|
129
|
+
|
|
130
|
+
# You can also provide options that will be used in calls to cache.fetch.
|
|
131
|
+
# The default cache_fetch_options are:
|
|
132
|
+
config.cache_fetch_options = { expires_in: 1.day, race_condition_ttl: 5.minutes }
|
|
133
|
+
end
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
`LightSwitch` resets cached values whenever an underlying `LightSwitch::Switch`
|
|
137
|
+
model is changed. You should **not** use an in-memory cache for `LightSwitch`.
|
|
138
|
+
If you are using an in-memory cache, `LightSwitch` has no way to clear the caches
|
|
139
|
+
used by all of the various processes running your application (web workers, Sidekiq workers, etc.).
|
|
140
|
+
If you are using a centralized cache, like Redis or MemCached, feel free to use
|
|
141
|
+
it with `LightSwitch`. The queries to read a switch from the database are
|
|
142
|
+
indexed and will return very quickly, so most `LightSwitch` users will not
|
|
143
|
+
need caching.
|
|
144
|
+
|
|
145
|
+
You can also implement your own cache as long as it implements the interface
|
|
146
|
+
of [`LightSwitch::NullCache`](lib/light_switch/null_cache.rb).
|
|
147
|
+
|
|
148
|
+
### ActiveSupport::Notifications
|
|
149
|
+
|
|
150
|
+
If you wish to be notified of changes made to any switches, you can subscribe
|
|
151
|
+
to [`ActiveSupport::Notifications`](https://api.rubyonrails.org/classes/ActiveSupport/Notifications.html)
|
|
152
|
+
published by LightSwitch. There are three events available:
|
|
153
|
+
|
|
154
|
+
* `create_committed.switch.light_switch`
|
|
155
|
+
* `destroy_committed.switch.light_switch`
|
|
156
|
+
* `update_committed.switch.light_switch`
|
|
157
|
+
|
|
158
|
+
An example use case could be logging changes for visibility:
|
|
159
|
+
|
|
160
|
+
```ruby
|
|
161
|
+
# config/initializers/light_switch.rb
|
|
162
|
+
|
|
163
|
+
ActiveSupport::Notifications.subscribe("update_committed.switch.light_switch") do |*args|
|
|
164
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
|
165
|
+
switch = event.payload[:switch]
|
|
166
|
+
|
|
167
|
+
Rails.logger.info("#{switch.name} is now #{switch.state}")
|
|
168
|
+
end
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
The events captured do not capture any meaningful latency metrics. They are just
|
|
172
|
+
published events once changes to a `LightSwitch::Switch` are committed.
|
|
173
|
+
|
|
174
|
+
## Installation
|
|
175
|
+
Add this line to your application's Gemfile:
|
|
176
|
+
|
|
177
|
+
```ruby
|
|
178
|
+
gem "light_switch"
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
And then execute:
|
|
182
|
+
```bash
|
|
183
|
+
$ bundle
|
|
184
|
+
$ ./bin/rails light_switch:install:migrations db:migrate
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Or install it yourself as:
|
|
188
|
+
```bash
|
|
189
|
+
$ gem install light_switch
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Tests
|
|
193
|
+
You can run the tests across all supported Rails versions:
|
|
194
|
+
```
|
|
195
|
+
./bin/appraisal rails test
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Release process
|
|
199
|
+
```
|
|
200
|
+
./bin/release [major|minor|patch]
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Contributing
|
|
204
|
+
Fork this repo and submit a pull request.
|
|
205
|
+
|
|
206
|
+
If you find a bug or have a feature request, please open a GitHub issue.
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns:x="http://ns.adobe.com/Extensibility/1.0/" xmlns:i="http://ns.adobe.com/AdobeIllustrator/10.0/" xmlns:graph="http://ns.adobe.com/Graphs/1.0/" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 90 112.5" enable-background="new 0 0 90 90" xml:space="preserve"><switch><foreignObject requiredExtensions="http://ns.adobe.com/AdobeIllustrator/10.0/" x="0" y="0" width="1" height="1"/><g i:extraneous="self"><g><path fill="none" d="M60.6,13H29.4c-3.5,0-6.3,2.8-6.3,6.3v51.4c0,3.5,2.8,6.3,6.3,6.3h31.3c3.5,0,6.3-2.8,6.3-6.3V19.3 C67,15.8,64.1,13,60.6,13z M57.8,35.2c0,0.2-0.1,0.3-0.2,0.5l-5.4,8.7c0,0.1,0,0.1,0,0.2l0,12.4c0,0.3-0.1,0.6-0.3,0.8 c-0.2,0.2-0.5,0.3-0.8,0.3H39.8c-0.6,0-1.2-0.5-1.2-1.2V30.4c0-0.6,0.5-1.2,1.2-1.2h2.6l0.2-0.3c0-0.5,0.3-1,0.7-1.2v0h0 c0,0,0,0,0.1,0c0.2-0.1,11.5,0,11.5,0c0.7,0,1.3,0.2,1.4,0.9l1.4,5.8C57.9,34.7,57.9,34.9,57.8,35.2z"/><path d="M60.6,11H29.4c-4.6,0-8.3,3.7-8.3,8.3v51.4c0,4.6,3.7,8.3,8.3,8.3h31.3c4.6,0,8.3-3.7,8.3-8.3V19.3 C69,14.7,65.2,11,60.6,11z M67,70.7c0,3.5-2.8,6.3-6.3,6.3H29.4c-3.5,0-6.3-2.8-6.3-6.3V19.3c0-3.5,2.8-6.3,6.3-6.3h31.3 c3.5,0,6.3,2.8,6.3,6.3V70.7z"/><path d="M56.4,28.6c-0.2-0.6-0.7-0.9-1.4-0.9c0,0-11.3-0.1-11.5,0c0,0,0,0-0.1,0h0v0c-0.5,0.2-0.8,0.7-0.7,1.2l-0.2,0.3h-2.6 c-0.6,0-1.2,0.5-1.2,1.2V57c0,0.6,0.5,1.2,1.2,1.2h11.2c0.3,0,0.6-0.1,0.8-0.3c0.2-0.2,0.3-0.5,0.3-0.8l0-12.4c0-0.1,0-0.1,0-0.2 l5.4-8.7c0.1-0.1,0.1-0.3,0.2-0.5c0.1-0.2,0.2-0.5,0.1-0.8L56.4,28.6z M41,35.9l2.4-3.8l0.7,2.8L41,39.9V35.9z M41,55.8V44.9h8.9 l0,10.9H41z M50.6,42.6h-8.5l4.2-6.7h8.4L50.6,42.6z M46.2,33.6l-0.9-3.5h9.1l0.9,3.5H46.2z"/></g></g></switch></svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//= require turbo.min
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
|
3
|
+
* listed below.
|
|
4
|
+
*
|
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
|
7
|
+
*
|
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
|
11
|
+
* It is generally better to create a new file per style scope.
|
|
12
|
+
*
|
|
13
|
+
*= require_tree .
|
|
14
|
+
*= require_self
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
.site-header {
|
|
18
|
+
display: flex;
|
|
19
|
+
flex-flow: row;
|
|
20
|
+
align-items: center;
|
|
21
|
+
margin: 0 0 1em -0.75em;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.site-header h2 {
|
|
25
|
+
margin-bottom: 0.25em;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
.site-header .site-logo {
|
|
29
|
+
height: 4em;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.container {
|
|
33
|
+
min-height: 100vh;
|
|
34
|
+
padding-top: 1em;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#new-switch {
|
|
38
|
+
padding-bottom: 4em;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.site-footer {
|
|
42
|
+
position: absolute;
|
|
43
|
+
bottom: 0;
|
|
44
|
+
width: 100%;
|
|
45
|
+
min-height: 3em;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.form-inline {
|
|
49
|
+
display: flex;
|
|
50
|
+
flex-flow: row;
|
|
51
|
+
align-items: center;
|
|
52
|
+
margin-bottom: 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.form-inline label {
|
|
56
|
+
margin: 0.5em 1em 0.5em 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.form-inline input,
|
|
60
|
+
.form-inline button {
|
|
61
|
+
vertical-align: middle;
|
|
62
|
+
margin: 0.5em 1em 0.5em 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.alert {
|
|
66
|
+
padding: 1em;
|
|
67
|
+
background-color: rgba(255, 0, 0, 0.1);
|
|
68
|
+
border-radius: 0.5em;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.notice {
|
|
72
|
+
padding: 1em;
|
|
73
|
+
background-color: rgba(0, 255, 0, 0.1);
|
|
74
|
+
border-radius: 0.5em;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.mr-1 {
|
|
78
|
+
margin-right: 1em;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.switch-state {
|
|
82
|
+
min-width: 3em;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.switch-state-on {
|
|
86
|
+
font-weight: bold;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
input.turn-off {
|
|
90
|
+
min-width: 12em;
|
|
91
|
+
background: rgba(255, 0, 0, 0.25);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
input.turn-on {
|
|
95
|
+
background: rgba(0, 255, 0, 0.25);
|
|
96
|
+
min-width: 12em;
|
|
97
|
+
}
|