Module: Offshore::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/offshore/server/database.rb

Constant Summary collapse

LOCK_KEY =
"database:lock"
SHUTDOWN_KEY =
"database:shutdown"
SUITES_LIST_KEY =
"suites:list"

Instance Method Summary collapse

Instance Method Details

#initObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/offshore/server/database.rb', line 45

def init
  # TODO: how to let these finish and also stall startup.
  # should it be per test
  # what about db reconnections?
  Logger.info(" Database.init")
  
  Offshore::Database.unlock unless ENV['MULTI_OFFSHORE']  # no reason to keep everyone waiting if I'm the only one
  
  if redis.get(SHUTDOWN_KEY)
    Logger.info("   .... database shutting down. Exiting.")
    raise "Database shutting down. No new connections, please."
  end
end

#lockObject



13
14
15
16
17
18
19
20
21
# File 'lib/offshore/server/database.rb', line 13

def lock
  Logger.info(" Database.lock")
  if Offshore::Mutex.lock(LOCK_KEY)
    Logger.info("    .... lock acquired")
  else
    Logger.info("    .... locked")
    raise Offshore::CheckBackLater.new("Database in use")
  end
end

#redisObject



9
10
11
# File 'lib/offshore/server/database.rb', line 9

def redis
  Offshore.redis
end

#resetObject



28
29
30
31
32
33
# File 'lib/offshore/server/database.rb', line 28

def reset
  Logger.info(" Database.reset")
  redis.del(LOCK_KEY)
  redis.del(SHUTDOWN_KEY)
  redis.del(SUITES_LIST_KEY)
end

#rollbackObject



64
65
66
67
# File 'lib/offshore/server/database.rb', line 64

def rollback
  Logger.info(" Database.rollback")
  snapshot.rollback
end

#schema_snapshotObject



59
60
61
62
# File 'lib/offshore/server/database.rb', line 59

def schema_snapshot
  Logger.info(" Database.schema_snapshot")
  snapshot.create
end

#shutdownObject



40
41
42
43
# File 'lib/offshore/server/database.rb', line 40

def shutdown
  Logger.info(" Database.shutdown")
  redis.incr(SHUTDOWN_KEY, 1)
end

#startupObject



35
36
37
38
# File 'lib/offshore/server/database.rb', line 35

def startup
  Logger.info(" Database.startup")
  reset
end

#unlockObject



23
24
25
26
# File 'lib/offshore/server/database.rb', line 23

def unlock
  Logger.info(" Database.unlock")
  Offshore::Mutex.unlock!(LOCK_KEY)
end