Class: Madeleine::DefaultSnapshotMadeleine
- Inherits:
-
Object
- Object
- Madeleine::DefaultSnapshotMadeleine
- Defined in:
- lib/madeleine.rb
Instance Attribute Summary collapse
-
#system ⇒ Object
readonly
The prevalent system.
Instance Method Summary collapse
-
#close ⇒ Object
Close the system.
-
#execute_command(command) ⇒ Object
Execute a command on the prevalent system.
-
#execute_query(query) ⇒ Object
Execute a query on the prevalent system.
-
#initialize(system, logger, snapshotter, executer) ⇒ DefaultSnapshotMadeleine
constructor
A new instance of DefaultSnapshotMadeleine.
-
#take_snapshot ⇒ Object
Take a snapshot of the current system.
Constructor Details
#initialize(system, logger, snapshotter, executer) ⇒ DefaultSnapshotMadeleine
Returns a new instance of DefaultSnapshotMadeleine.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/madeleine.rb', line 79 def initialize(system, logger, snapshotter, executer) SanityCheck.instance.run_once @system = system @logger = logger @snapshotter = snapshotter @lock = Sync.new @executer = executer @closed = false end |
Instance Attribute Details
#system ⇒ Object (readonly)
The prevalent system
77 78 79 |
# File 'lib/madeleine.rb', line 77 def system @system end |
Instance Method Details
#close ⇒ Object
Close the system.
The log file is closed and no new commands can be received by this Madeleine.
149 150 151 152 153 154 |
# File 'lib/madeleine.rb', line 149 def close @lock.synchronize do @logger.close @closed = true end end |
#execute_command(command) ⇒ Object
Execute a command on the prevalent system.
Commands must have a method execute(aSystem)
. Otherwise an error, Madeleine::InvalidCommandException
, will be raised.
The return value from the command’s execute()
method is returned.
-
command
- The command to execute on the system.
100 101 102 103 104 105 106 107 |
# File 'lib/madeleine.rb', line 100 def execute_command(command) verify_command_sane(command) @lock.synchronize do raise MadeleineClosedException if @closed @logger.store(command) @executer.execute(command) end end |
#execute_query(query) ⇒ Object
Execute a query on the prevalent system.
Only differs from execute_command
in that the command/query isn’t logged, and therefore isn’t allowed to modify the system. A shared lock is held, preventing others from modifying the system while the query is running.
-
query
- The query command to execute
116 117 118 119 120 |
# File 'lib/madeleine.rb', line 116 def execute_query(query) @lock.synchronize(:SH) do @executer.execute(query) end end |
#take_snapshot ⇒ Object
Take a snapshot of the current system.
You need to regularly take a snapshot of a running system, otherwise the logs will grow big and restarting the system will take a long time. Your backups must also be done from the snapshot files, since you can’t make a consistent backup of a live log.
A practical way of doing snapshots is a timer thread:
Thread.new(madeleine) {|madeleine|
while true
sleep(60 * 60 * 24) # 24 hours
madeleine.take_snapshot
end
}
137 138 139 140 141 142 143 |
# File 'lib/madeleine.rb', line 137 def take_snapshot @lock.synchronize do @logger.close @snapshotter.take(@system) @logger.reset end end |