Module: BigBench::Store

Defined in:
lib/bigbench/store.rb

Overview

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.

Constant Summary collapse

TEST_RECEIPT_KEY =
"bigbench_test_receipt"
RUNNING_KEY =
"bigbench_running"
BOTS_KEY =
"bigbench_bots"
TRACKINGS_KEY =
"bigbench_trackings"

Class Method Summary collapse

Class Method Details

.add_tracking(tracking) ⇒ Object

Add a tracking to the trackings



77
78
79
# File 'lib/bigbench/store.rb', line 77

def self.add_tracking(tracking)
  @redis.rpush(TRACKINGS_KEY, tracking)
end

.bot_is_working(id) ⇒ Object

Adds a bot to the currently working bots list



62
63
64
# File 'lib/bigbench/store.rb', line 62

def self.bot_is_working(id)
  @redis.sadd(BOTS_KEY, id)
end

.bot_stopped_working(id) ⇒ Object

Removes a bot to the currently working bots list



67
68
69
# File 'lib/bigbench/store.rb', line 67

def self.bot_stopped_working(id)
  @redis.srem(BOTS_KEY, id)
end

.botsObject

Shows all currently working bots



72
73
74
# File 'lib/bigbench/store.rb', line 72

def self.bots
  @redis.smembers(BOTS_KEY)
end

.count_trackingsObject

How many trackings are in the list



87
88
89
# File 'lib/bigbench/store.rb', line 87

def self.count_trackings
  @redis.llen(TRACKINGS_KEY)
end

.pop_trackingObject

Pop a tracking from the beginning of the list



82
83
84
# File 'lib/bigbench/store.rb', line 82

def self.pop_tracking
  @redis.lpop(TRACKINGS_KEY)
end

.reset!Object

Deletes all BigBench related keys on the redis store



23
24
25
26
27
28
29
30
# File 'lib/bigbench/store.rb', line 23

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

.running?Boolean

Returns true if a bot test has been started

Returns:

  • (Boolean)


57
58
59
# File 'lib/bigbench/store.rb', line 57

def self.running?
  @redis.get(RUNNING_KEY) == "1"
end

.setup!(url = nil, password = nil) ⇒ Object

Setup the redis storage. Default values are localhost:6379 and no password. This needs to be called for any bot test



16
17
18
19
20
# File 'lib/bigbench/store.rb', line 16

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

.startObject

Start the tests for all bots by setting the running key



47
48
49
# File 'lib/bigbench/store.rb', line 47

def self.start
  @redis.set(RUNNING_KEY, "1")
end

.stopObject

Stops the test for all bots by unsetting the running key



52
53
54
# File 'lib/bigbench/store.rb', line 52

def self.stop
  @redis.set(RUNNING_KEY, "0")
end

.testObject

Gets the current test from the redis



42
43
44
# File 'lib/bigbench/store.rb', line 42

def self.test
  @redis.get(TEST_RECEIPT_KEY)
end

.test=(test) ⇒ Object

Stores the current test receipt for all bots on the redis instance



33
34
35
36
37
38
39
# File 'lib/bigbench/store.rb', line 33

def self.test=(test)
  if test
    @redis.set(TEST_RECEIPT_KEY, test)
  else
    @redis.del(TEST_RECEIPT_KEY)
  end
end