Sha256: c045b16ace2ade3b0ac2da4e248cc5b398442b326ff324cac98706551b2fecd9

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'madeleine/automatic'
require 'madeleine'
require 'singleton'
require 'yaml'

class MadeleineService
  include Madeleine::Automatic::Interceptor

  @@storage_path = self.name.downcase + "_storage"

  class << self
    def storage_path
      @@storage_path
    end
  
    def storage_path=(storage_path)
      @@storage_path = storage_path
    end

    def clean_storage
      MadeleineServer.clean_storage(self)
    end

    def instance
      @system = MadeleineServer.new(self).system if @system.nil?
      @system
    end
    
    def restart
      clean_storage
      @system = MadeleineServer.new(self).system
    end
  end
end

class MadeleineServer
  SNAPSHOT_INTERVAL   = 60 * 60 * 24 # seconds * minutes * hours => Each day
  AUTOMATIC_SNAPSHOTS = true

  # Clears all the command_log and snapshot files located in the storage directory, so the
  # database is essentially dropped and recreated as blank
  def self.clean_storage(service)
    begin 
      Dir.foreach(service.storage_path) do |file|
        File.delete(service.storage_path + File::SEPARATOR + file) if file =~ /(command_log|snapshot)$/
      end
    rescue
      Dir.mkdir(service.storage_path)
    end
  end

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

  def system
    @server.system
  end

  def start_snapshot_thread
    Thread.new(@server) {
      while true
        sleep(SNAPSHOT_INTERVAL)
        @server.take_snapshot
      end
    }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
Pimki-1.1.092 libraries/madeleine_service.rb
Pimki-1.2.092 libraries/madeleine_service.rb
Pimki-1.3.092 libraries/madeleine_service.rb