Sha256: dd592c923c43e044bbbef238d6c7520e2a2b5f2b007255685fdab0d4a8ff000b

Contents?: true

Size: 962 Bytes

Versions: 1

Compression:

Stored size: 962 Bytes

Contents

require 'ruboty/brains/base'
require 'ruboty/brains/memory'
require 'pstore'

module Ruboty
  module Brains
    class LocalPStore < Base
      KEY = 'brain'

      attr_reader :thread

      env :PSTORE_SAVE_INTERVAL, 'Interval sec to save data to PStore-db (default: 5)', optional: true

      def initialize
        super
        @thread = Thread.new { sync }
        @thread.abort_on_exception = true
      end

      def data
        @data ||= pull || {}
      end

      private

      def push
        db.transaction do
          db[KEY] = data
        end
      end

      def pull
        db.transaction do
          db[KEY]
        end
      end

      def sync
        loop do
          wait
          push
        end
      end

      def wait
        sleep(interval)
      end

      def db
        @db ||= PStore.new('/tmp/__ruboty_pstore__')
      end

      def interval
        (ENV['PSTORE_SAVE_INTERVAL'] || 5).to_i
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruboty-local_pstore-0.0.0 lib/ruboty/brains/local_pstore.rb