module BigBench # The store encapsulates the communication with the redis key-value store. To the outside it looks like normal module methods would be # called. It allows the simple starting of a test on multiple bots with the latest test reciept. module Store TEST_RECEIPT_KEY = "bigbench_test_receipt" RUNNING_KEY = "bigbench_running" BOTS_KEY = "bigbench_bots" TRACKINGS_KEY = "bigbench_trackings" @redis = nil # Setup the redis storage. Default values are localhost:6379 and no password. # This needs to be called for any bot test def self.setup!(url = nil, password = nil) uri = URI(url || "localhost:6379") @redis = ::Redis.new :host => uri.host, :port => uri.port, :password => password true end # Deletes all BigBench related keys on the redis store def self.reset! return if @redis.nil? @redis.del(TEST_RECEIPT_KEY) @redis.del(RUNNING_KEY) @redis.del(BOTS_KEY) @redis.del(TRACKINGS_KEY) end # Stores the current test receipt for all bots on the redis instance def self.test=(test) if test @redis.set(TEST_RECEIPT_KEY, test) else @redis.del(TEST_RECEIPT_KEY) end end # Gets the current test from the redis def self.test @redis.get(TEST_RECEIPT_KEY) end # Start the tests for all bots by setting the running key def self.start @redis.set(RUNNING_KEY, "1") end # Stops the test for all bots by unsetting the running key def self.stop @redis.set(RUNNING_KEY, "0") end # Returns true if a bot test has been started def self.running? @redis.get(RUNNING_KEY) == "1" end # Adds a bot to the currently working bots list def self.bot_is_working(id) @redis.sadd(BOTS_KEY, id) end # Removes a bot to the currently working bots list def self.bot_stopped_working(id) @redis.srem(BOTS_KEY, id) end # Shows all currently working bots def self.bots @redis.smembers(BOTS_KEY) end # Add a tracking to the trackings def self.add_tracking(tracking) @redis.rpush(TRACKINGS_KEY, tracking) end # Pop a tracking from the beginning of the list def self.pop_tracking @redis.lpop(TRACKINGS_KEY) end # How many trackings are in the list def self.count_trackings @redis.llen(TRACKINGS_KEY) end end end