Sha256: 7d97ed6cca9fe1138dbca30c1fd74583fbef2ed6d3343bbb0aaf3cfd04455611

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

module Journaled
  module Connection
    class << self
      def available?
        Journaled.transactional_batching_enabled && transaction_open?
      end

      def stage!(event)
        raise TransactionSafetyError, <<~MSG unless transaction_open?
          Transaction not available! By default, journaled event batching requires an open database transaction.
        MSG

        connection.current_transaction._journaled_staged_events << event
      end

      private

      def transaction_open?
        connection.transaction_open?
      end

      def connection
        if Journaled.queue_adapter.in? %w(delayed delayed_job)
          Delayed::Job.connection
        elsif Journaled.queue_adapter == 'good_job'
          GoodJob::BaseRecord.connection
        elsif Journaled.queue_adapter == 'que'
          Que::ActiveRecord::Model.connection
        elsif Journaled.queue_adapter == 'test' && Rails.env.test?
          ActiveRecord::Base.connection
        else
          raise "Unsupported adapter: #{Journaled.queue_adapter}"
        end
      end
    end

    module TestOnlyBehaviors
      def transaction_open?
        # Transactional fixtures wrap all tests in an outer, non-joinable transaction:
        super && (connection.open_transactions > 1 || connection.current_transaction.joinable?)
      end
    end

    class << self
      prepend TestOnlyBehaviors if Rails.env.test?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
journaled-5.2.0 lib/journaled/connection.rb
journaled-5.1.1 lib/journaled/connection.rb
journaled-5.1.0 lib/journaled/connection.rb
journaled-5.0.0 lib/journaled/connection.rb