Class: ActiveRecord::FutureResult

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/future_result.rb

Overview

:nodoc:

Direct Known Subclasses

SelectAll

Defined Under Namespace

Classes: Complete, EventBuffer, SelectAll

Constant Summary collapse

Canceled =
Class.new(ActiveRecordError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, *args, **kwargs) ⇒ FutureResult

Returns a new instance of FutureResult.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/future_result.rb', line 55

def initialize(pool, *args, **kwargs)
  @mutex = Mutex.new

  @session = nil
  @pool = pool
  @args = args
  @kwargs = kwargs

  @pending = true
  @error = nil
  @result = nil
  @instrumenter = ActiveSupport::Notifications.instrumenter
  @event_buffer = nil
end

Instance Attribute Details

#lock_waitObject (readonly)

Returns the value of attribute lock_wait.



53
54
55
# File 'lib/active_record/future_result.rb', line 53

def lock_wait
  @lock_wait
end

Instance Method Details

#cancelObject



83
84
85
86
87
# File 'lib/active_record/future_result.rb', line 83

def cancel
  @pending = false
  @error = Canceled
  self
end

#canceled?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/active_record/future_result.rb', line 124

def canceled?
  @session && !@session.active?
end

#execute!(connection) ⇒ Object



79
80
81
# File 'lib/active_record/future_result.rb', line 79

def execute!(connection)
  execute_query(connection)
end

#execute_or_skipObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_record/future_result.rb', line 89

def execute_or_skip
  return unless pending?

  @pool.with_connection do |connection|
    return unless @mutex.try_lock
    begin
      if pending?
        @event_buffer = EventBuffer.new(self, @instrumenter)
        connection.with_instrumenter(@event_buffer) do
          execute_query(connection, async: true)
        end
      end
    ensure
      @mutex.unlock
    end
  end
end

#pending?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/active_record/future_result.rb', line 120

def pending?
  @pending && (!@session || @session.active?)
end

#resultObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/active_record/future_result.rb', line 107

def result
  execute_or_wait
  @event_buffer&.flush

  if canceled?
    raise Canceled
  elsif @error
    raise @error
  else
    @result
  end
end

#schedule!(session) ⇒ Object



74
75
76
77
# File 'lib/active_record/future_result.rb', line 74

def schedule!(session)
  @session = session
  @pool.schedule_query(self)
end

#then(&block) ⇒ Object



70
71
72
# File 'lib/active_record/future_result.rb', line 70

def then(&block)
  Promise.new(self, block)
end