Class: ActiveRecord::FutureResult

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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.



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

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



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

def lock_wait
  @lock_wait
end

Instance Method Details

#cancelObject



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

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

#canceled?Boolean

Returns:

  • (Boolean)


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

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

#execute!(connection) ⇒ Object



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

def execute!(connection)
  execute_query(connection)
end

#execute_or_skipObject



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

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)


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

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

#resultObject



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

def result
  execute_or_wait
  @event_buffer&.flush

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

#schedule!(session) ⇒ Object



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

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

#then(&block) ⇒ Object



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

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