Class: MadeleineServer

Inherits:
Object
  • Object
show all
Defined in:
app/models/wiki_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ MadeleineServer

Returns a new instance of MadeleineServer.



182
183
184
185
186
187
188
189
# File 'app/models/wiki_service.rb', line 182

def initialize(service)
  @storage_path = service.storage_path
  @server = Madeleine::Automatic::AutomaticSnapshotMadeleine.new(service.storage_path, 
    Madeleine::ZMarshal.new) {
    service.new
  }
  start_snapshot_thread
end

Instance Attribute Details

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



166
167
168
# File 'app/models/wiki_service.rb', line 166

def storage_path
  @storage_path
end

Class Method Details

.clean_storage(service) ⇒ Object

Clears all the command_log and snapshot files located in the storage directory, so the database is essentially dropped and recreated as blank



170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/wiki_service.rb', line 170

def self.clean_storage(service)
  begin 
    Dir.foreach(service.storage_path) do |file|
      if file =~ /(command_log|snapshot)$/
        File.delete(File.join(service.storage_path, file))
      end
    end
  rescue
    Dir.mkdir(service.storage_path)
  end
end

Instance Method Details

#command_log_present?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'app/models/wiki_service.rb', line 191

def command_log_present?
  not Dir[storage_path + '/*.command_log'].empty?
end

#snapshotObject



195
196
197
# File 'app/models/wiki_service.rb', line 195

def snapshot
  @server.take_snapshot
end

#start_snapshot_threadObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/models/wiki_service.rb', line 199

def start_snapshot_thread
  Thread.new(@server) {
    hours_since_last_snapshot = 0
    while true
      begin
        hours_since_last_snapshot += 1
        # Take a snapshot if there is a command log, or 24 hours 
        # have passed since the last snapshot
        if command_log_present? or hours_since_last_snapshot >= 24 
          ActionController::Base.logger.info "[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] " +
            'Taking a Madeleine snapshot'
          snapshot
          hours_since_last_snapshot = 0
        end
        sleep(1.hour)
      rescue => e
        ActionController::Base.logger.error(e)
        # wait for a minute (not to spoof the log with the same error)
        # and go back into the loop, to keep trying
        sleep(1.minute)
        ActionController::Base.logger.info("Retrying to save a snapshot")
      end
    end
  }
end

#systemObject



225
226
227
# File 'app/models/wiki_service.rb', line 225

def system
  @server.system
end