Created
March 15, 2017 02:50
-
-
Save thephw/0f313c142fc595cb874218d351f75989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "redis" | |
class LockNKey | |
def call(env) | |
req = Rack::Request.new(env) | |
case req.path_info | |
when /bootstrap/ | |
redis = Redis.new | |
taken_environments = redis.get("environments").split(",").map(&:to_i) | |
available_environments = (1..24).to_a - taken_environments | |
chosen_environment = available_environments.shuffle.first | |
redis.set("environments", (taken_environments+[chosen_environment]).join(",")) | |
[200, {"Content-Type" => "text/plain"}, ["#{chosen_environment}\n"]] | |
when /teardown/ | |
redis = Redis.new | |
taken_environments = redis.get("environments").split(",").map(&:to_i) | |
to_remove = req.path_info.split('/').last.to_i | |
taken_environments = taken_environments - [to_remove] | |
redis.set("environments", taken_environments.join(",")) | |
[200, {"Content-Type" => "text/plain"}, ["#{to_remove}\n"]] | |
when /clear/ | |
redis = Redis.new | |
redis.set("environments", nil) | |
[200, {"Content-Type" => "text/plain"}, ["OK\n"]] | |
when /available/ | |
redis = Redis.new | |
taken_environments = redis.get("environments").split(",").map(&:to_i) | |
[200, {"Content-Type" => "text/plain"}, ["#{(1..24).to_a - taken_environments}\n"]] | |
else | |
[404, {"Content-Type" => "text/plain"}, ["Not Found\n"]] | |
end | |
end | |
end | |
run LockNKey.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment