Sha256: 8f65a7a632f9eae721c2a8d82adafb68b88ab31fe5d8488322365def7328a85d

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require_relative 'auto_create'

module Hyperstack
  module ConnectionAdapter
    module ActiveRecord
      class Connection < ::ActiveRecord::Base
        extend AutoCreate

        self.table_name = 'hyperstack_connections'

        do_not_synchronize

        has_many :messages,
                foreign_key: 'connection_id',
                class_name: 'Hyperstack::ConnectionAdapter::ActiveRecord::QueuedMessage',
                dependent: :destroy

        scope :expired,
              -> { where('expires_at IS NOT NULL AND expires_at < ?', Time.current) }
        scope :pending_for,
              ->(channel) { where(channel: channel).where('session IS NOT NULL') }
        scope :inactive,
              -> { where('session IS NULL AND refresh_at < ?', Time.current) }

        before_create do
          if session
            self.expires_at = Time.current + transport.expire_new_connection_in
          elsif transport.refresh_channels_every != :never
            self.refresh_at = Time.current + transport.refresh_channels_every
          end
        end

        class << self
          def needs_refresh?
            exists?(['refresh_at IS NOT NULL AND refresh_at < ?', Time.current])
          end
        end

        def transport
          Hyperstack::Connection.transport
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hyper-operation-1.0.alpha1.8 lib/hyper-operation/transport/connection_adapter/active_record/connection.rb
hyper-operation-1.0.alpha1.7 lib/hyper-operation/transport/connection_adapter/active_record/connection.rb
hyper-operation-1.0.alpha1.6 lib/hyper-operation/transport/connection_adapter/active_record/connection.rb