Skip to content

Instantly share code, notes, and snippets.

@thephw
Created March 15, 2017 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thephw/0f313c142fc595cb874218d351f75989 to your computer and use it in GitHub Desktop.
Save thephw/0f313c142fc595cb874218d351f75989 to your computer and use it in GitHub Desktop.
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