rollout 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Golick
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.rdoc ADDED
@@ -0,0 +1,84 @@
1
+ = rollout
2
+
3
+ Conditionally rollout features to certain users with redis.
4
+
5
+ == How it works
6
+
7
+ Initialize a rollout object. I assign it to a global var.
8
+
9
+ $redis = Redis.new
10
+ $rollout = Rollout.new(redis)
11
+
12
+ Check whether a feature is active for a particular user:
13
+
14
+ $rollout.active?(:chat, User.first) # => true/false
15
+
16
+ You can activate features using a number of different mechanisms.
17
+
18
+ == Groups
19
+
20
+ Rollout ships with one group by default: "all", which does exactly what it sounds like.
21
+
22
+ You can activate the all group for the chat feature like this:
23
+
24
+ $rollout.activate_group(:chat, :all)
25
+
26
+ You might also want to define your own groups. We have one for our caretakers:
27
+
28
+ $rollout.define_group(:chat) do |user|
29
+ user.caretaker?
30
+ end
31
+
32
+ You can activate multiple groups per feature.
33
+
34
+ Deactivate groups like this:
35
+
36
+ $rollout.deactivate_group(:chat, :all)
37
+
38
+ == Specific Users
39
+
40
+ You might want to let a specific user into a beta test or something. If that user isn't part of an existing group, you can let them in specifically:
41
+
42
+ $rollout.activate_user(:chat, @user)
43
+
44
+ Deactivate them like this:
45
+
46
+ $rollout.deactivate_user(:chat, @user)
47
+
48
+ == User Percentages
49
+
50
+ If you're rolling out a new feature, you might want to test the waters by slowly letting in a percentage of your users.
51
+
52
+ $rollout.activate_percentage(:chat, 20)
53
+
54
+ The algorithm for determining which users get let in is this:
55
+
56
+ user.id % (100 / percentage) == 0
57
+
58
+ So, for 20%, it's every 5th user.
59
+
60
+ Deactivate all percentages like this:
61
+
62
+ $rollout.deactivate_percentage(:chat)
63
+
64
+ == Feature is broken
65
+
66
+ Deactivate everybody at once:
67
+
68
+ $rollout.deactivate_all
69
+
70
+ For some of our less stable features, we are actually measuring the error rate using redis, and deactivating them automatically when it raises above a certain threshold. It's pretty cool.
71
+
72
+ == Note on Patches/Pull Requests
73
+
74
+ * Fork the project.
75
+ * Make your feature addition or bug fix.
76
+ * Add tests for it. This is important so I don't break it in a
77
+ future version unintentionally.
78
+ * Commit, do not mess with rakefile, version, or history.
79
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
80
+ * Send me a pull request. Bonus points for topic branches.
81
+
82
+ == Copyright
83
+
84
+ Copyright (c) 2010 James Golick, Protose, Inc. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rollout"
8
+ gem.summary = %Q{Conditionally roll out features with redis.}
9
+ gem.description = %Q{Conditionally roll out features with redis.}
10
+ gem.email = "james@giraffesoft.ca"
11
+ gem.homepage = "http://github.com/giraffesoft/rollout"
12
+ gem.authors = ["James Golick"]
13
+ gem.add_development_dependency "rspec", "1.2.9"
14
+ gem.add_development_dependency "bourne", "1.0.0"
15
+ gem.add_development_dependency "redis", "0.1"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "rollout #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/rollout.rb ADDED
@@ -0,0 +1,78 @@
1
+ class Rollout
2
+ def initialize(redis)
3
+ @redis = redis
4
+ @groups = {"all" => lambda { |user| true }}
5
+ end
6
+
7
+ def activate_group(feature, group)
8
+ @redis.sadd(group_key(feature), group)
9
+ end
10
+
11
+ def deactivate_group(feature, group)
12
+ @redis.srem(group_key(feature), group)
13
+ end
14
+
15
+ def deactivate_all(feature)
16
+ @redis.del(group_key(feature))
17
+ @redis.del(user_key(feature))
18
+ @redis.del(percentage_key(feature))
19
+ end
20
+
21
+ def activate_user(feature, user)
22
+ @redis.sadd(user_key(feature), user.id)
23
+ end
24
+
25
+ def deactivate_user(feature, user)
26
+ @redis.srem(user_key(feature), user.id)
27
+ end
28
+
29
+ def define_group(group, &block)
30
+ @groups[group.to_s] = block
31
+ end
32
+
33
+ def active?(feature, user)
34
+ user_in_active_group?(feature, user) ||
35
+ user_active?(feature, user) ||
36
+ user_within_active_percentage?(feature, user)
37
+ end
38
+
39
+ def activate_percentage(feature, percentage)
40
+ @redis.set(percentage_key(feature), percentage)
41
+ end
42
+
43
+ def deactivate_percentage(feature)
44
+ @redis.del(percentage_key(feature))
45
+ end
46
+
47
+ private
48
+ def key(name)
49
+ "feature:#{name}"
50
+ end
51
+
52
+ def group_key(name)
53
+ "#{key(name)}:groups"
54
+ end
55
+
56
+ def user_key(name)
57
+ "#{key(name)}:users"
58
+ end
59
+
60
+ def percentage_key(name)
61
+ "#{key(name)}:percentage"
62
+ end
63
+
64
+ def user_in_active_group?(feature, user)
65
+ @redis.smembers(group_key(feature)).any? { |group| @groups[group].call(user) }
66
+ end
67
+
68
+ def user_active?(feature, user)
69
+ @redis.sismember(user_key(feature), user.id)
70
+ end
71
+
72
+ def user_within_active_percentage?(feature, user)
73
+ percentage = @redis.get(percentage_key(feature))
74
+ return false if percentage.nil?
75
+
76
+ user.id % (100 / percentage.to_i) == 0
77
+ end
78
+ end
@@ -0,0 +1,124 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Rollout" do
4
+ before do
5
+ @redis = Redis.new
6
+ @rollout = Rollout.new(@redis)
7
+ end
8
+
9
+ describe "when a group is activated" do
10
+ before do
11
+ @rollout.define_group(:fivesonly) { |user| user.id == 5 }
12
+ @rollout.activate_group(:chat, :fivesonly)
13
+ end
14
+
15
+ it "the feature is active for users for which the block evaluates to true" do
16
+ @rollout.should be_active(:chat, stub(:id => 5))
17
+ end
18
+
19
+ it "is not active for users for which the block evaluates to false" do
20
+ @rollout.should_not be_active(:chat, stub(:id => 1))
21
+ end
22
+ end
23
+
24
+ describe "the default all group" do
25
+ before do
26
+ @rollout.activate_group(:chat, :all)
27
+ end
28
+
29
+ it "evaluates to true no matter what" do
30
+ @rollout.should be_active(:chat, stub(:id => 0))
31
+ end
32
+ end
33
+
34
+ describe "deactivating a group" do
35
+ before do
36
+ @rollout.define_group(:fivesonly) { |user| user.id == 5 }
37
+ @rollout.activate_group(:chat, :all)
38
+ @rollout.activate_group(:chat, :fivesonly)
39
+ @rollout.deactivate_group(:chat, :all)
40
+ end
41
+
42
+ it "deactivates the rules for that group" do
43
+ @rollout.should_not be_active(:chat, stub(:id => 10))
44
+ end
45
+
46
+ it "leaves the other groups active" do
47
+ @rollout.should be_active(:chat, stub(:id => 5))
48
+ end
49
+ end
50
+
51
+ describe "deactivating a feature completely" do
52
+ before do
53
+ @rollout.define_group(:fivesonly) { |user| user.id == 5 }
54
+ @rollout.activate_group(:chat, :all)
55
+ @rollout.activate_group(:chat, :fivesonly)
56
+ @rollout.activate_user(:chat, stub(:id => 51))
57
+ @rollout.activate_percentage(:chat, 100)
58
+ @rollout.deactivate_all(:chat)
59
+ end
60
+
61
+ it "removes all of the groups" do
62
+ @rollout.should_not be_active(:chat, stub(:id => 0))
63
+ end
64
+
65
+ it "removes all of the users" do
66
+ @rollout.should_not be_active(:chat, stub(:id => 51))
67
+ end
68
+
69
+ it "removes the percentage" do
70
+ @rollout.should_not be_active(:chat, stub(:id => 24))
71
+ end
72
+ end
73
+
74
+ describe "activating a specific user" do
75
+ before do
76
+ @rollout.activate_user(:chat, stub(:id => 42))
77
+ end
78
+
79
+ it "is active for that user" do
80
+ @rollout.should be_active(:chat, stub(:id => 42))
81
+ end
82
+
83
+ it "remains inactive for other users" do
84
+ @rollout.should_not be_active(:chat, stub(:id => 24))
85
+ end
86
+ end
87
+
88
+ describe "deactivating a specific user" do
89
+ before do
90
+ @rollout.activate_user(:chat, stub(:id => 42))
91
+ @rollout.activate_user(:chat, stub(:id => 24))
92
+ @rollout.deactivate_user(:chat, stub(:id => 42))
93
+ end
94
+
95
+ it "that user should no longer be active" do
96
+ @rollout.should_not be_active(:chat, stub(:id => 42))
97
+ end
98
+
99
+ it "remains active for other active users" do
100
+ @rollout.should be_active(:chat, stub(:id => 24))
101
+ end
102
+ end
103
+
104
+ describe "activating a feature for a percentage of users" do
105
+ before do
106
+ @rollout.activate_percentage(:chat, 20)
107
+ end
108
+
109
+ it "activates the feature for that percentage of the users" do
110
+ (1..120).select { |id| @rollout.active?(:chat, stub(:id => id)) }.length.should == 24
111
+ end
112
+ end
113
+
114
+ describe "deactivating the percentage of users" do
115
+ before do
116
+ @rollout.activate_percentage(:chat, 100)
117
+ @rollout.deactivate_percentage(:chat)
118
+ end
119
+
120
+ it "becomes inactivate for all users" do
121
+ @rollout.should_not be_active(:chat, stub(:id => 24))
122
+ end
123
+ end
124
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rollout'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'bourne'
7
+ require 'redis'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with :mocha
11
+ config.before { Redis.new.flushdb }
12
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollout
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - James Golick
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-17 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - "="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bourne
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 0
50
+ version: 1.0.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: redis
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ hash: 9
62
+ segments:
63
+ - 0
64
+ - 1
65
+ version: "0.1"
66
+ type: :development
67
+ version_requirements: *id003
68
+ description: Conditionally roll out features with redis.
69
+ email: james@giraffesoft.ca
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - README.rdoc
77
+ files:
78
+ - .document
79
+ - .gitignore
80
+ - LICENSE
81
+ - README.rdoc
82
+ - Rakefile
83
+ - VERSION
84
+ - lib/rollout.rb
85
+ - spec/rollout_spec.rb
86
+ - spec/spec.opts
87
+ - spec/spec_helper.rb
88
+ has_rdoc: true
89
+ homepage: http://github.com/giraffesoft/rollout
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --charset=UTF-8
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.3.7
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: Conditionally roll out features with redis.
122
+ test_files:
123
+ - spec/rollout_spec.rb
124
+ - spec/spec_helper.rb