Sha256: ccbe3848c914642c42651bf6350f99d02a2a32372e4fb628aec0981a3a2bc17a

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require 'fileutils'
module NewRelic
  class DataSerialization
    module ClassMethods
      def should_send_data?
        # TODO get configuration from main control
        (File.size(file_path) >= max_size)
      end

      def load_from_file
        create_file_if_needed
        with_locked_store('r+') do |f|
          get_data_from_file(f)
        end
      rescue(EOFError) => e
        nil
      end

      def dump_to_file
        create_file_if_needed
        with_locked_store('r+') do |f|
          result = (yield get_data_from_file(f))
          f.rewind
          f.write(dump(result))
        end
      end

      private

      def with_locked_store(mode)
        File.open(file_path, mode) do |f|
          f.flock(File::LOCK_EX)
          begin
            yield(f)
          ensure
            f.flock(File::LOCK_UN)
          end
        end
      rescue Exception => e
        puts e.inspect
      end

      def get_data_from_file(f)
        data = f.read
        result = load(data)
        f.truncate(0)
        result
      end
      
      def max_size
        100_000
      end

      def create_file_if_needed
        FileUtils.touch(file_path) unless File.exists?(file_path)
      end

      def dump(object)
        Marshal.dump(object)
      end

      def load(dump)
        Marshal.load(dump)
      rescue ArgumentError => e
        nil
      end

      def truncate_file
        create_file_if_needed
        File.truncate(file_path, 0)
      end

      def file_path
        # TODO get configuration from main control
        './log/newrelic_agent_store.db'
      end
    end
    extend ClassMethods
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
newrelic_rpm-3.1.0.beta2 lib/new_relic/data_serialization.rb
newrelic_rpm-3.1.0.beta1 lib/new_relic/data_serialization.rb