Class: Restate::AttemptFinishedEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/restate/context.rb

Overview

Signals when the current invocation attempt has finished — either the handler completed, the connection was lost, or a transient error occurred.

Use this to clean up attempt-scoped resources (open connections, temp files, etc.) that should not outlive the current attempt.

Available via ctx.request.attempt_finished_event.

Examples:

Cancel a long-running HTTP call when the attempt finishes

event = ctx.request.attempt_finished_event
ctx.run('call-api') do
  # poll event.set? periodically, or pass it to your HTTP client
end

Instance Method Summary collapse

Constructor Details

#initializeAttemptFinishedEvent

Returns a new instance of AttemptFinishedEvent.



20
21
22
23
24
# File 'lib/restate/context.rb', line 20

def initialize
  @mutex = Mutex.new
  @set = false
  @waiters = []
end

Instance Method Details

#set!Object

Marks the event as set and wakes all waiters. Called internally by the SDK when the attempt ends.



47
48
49
50
51
52
53
# File 'lib/restate/context.rb', line 47

def set!
  @mutex.synchronize do
    @set = true
    @waiters.each { |w| w.push(true) }
    @waiters.clear
  end
end

#set?Boolean

Returns true if the attempt has finished.

Returns:

  • (Boolean)


27
28
29
# File 'lib/restate/context.rb', line 27

def set?
  @set
end

#waitObject

Blocks the current fiber/thread until the attempt finishes.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/restate/context.rb', line 32

def wait
  return if @set

  waiter = nil
  @mutex.synchronize do
    unless @set
      waiter = Thread::Queue.new
      @waiters << waiter
    end
  end
  waiter&.pop
end