Class: LittleGhost::AgentInterruptions::Ticket

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/agent_interruptions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, id:, batch_key:, metadata:) ⇒ Ticket

Returns a new instance of Ticket.



32
33
34
35
36
37
38
39
40
# File 'lib/little_ghost/agent_interruptions.rb', line 32

def initialize(message, id:, batch_key:, metadata:)
  @id = id
  @message = message
  @batch_key = batch_key
  @metadata = 
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @resolved = false
end

Instance Attribute Details

#batch_keyObject (readonly)

Returns the value of attribute batch_key.



30
31
32
# File 'lib/little_ghost/agent_interruptions.rb', line 30

def batch_key
  @batch_key
end

#idObject (readonly)

Returns the value of attribute id.



30
31
32
# File 'lib/little_ghost/agent_interruptions.rb', line 30

def id
  @id
end

#messageObject (readonly)

Returns the value of attribute message.



30
31
32
# File 'lib/little_ghost/agent_interruptions.rb', line 30

def message
  @message
end

#metadataObject (readonly)

Returns the value of attribute metadata.



30
31
32
# File 'lib/little_ghost/agent_interruptions.rb', line 30

def 
  @metadata
end

Instance Method Details

#reject(error) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/little_ghost/agent_interruptions.rb', line 52

def reject(error)
  @mutex.synchronize do
    return if @resolved

    @error = error
    @resolved = true
    @condition.broadcast
  end
end

#resolve(value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/little_ghost/agent_interruptions.rb', line 42

def resolve(value)
  @mutex.synchronize do
    return if @resolved

    @value = value
    @resolved = true
    @condition.broadcast
  end
end

#value(cancellation_token:, deadline:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/little_ghost/agent_interruptions.rb', line 62

def value(cancellation_token:, deadline:)
  @mutex.synchronize do
    until @resolved
      cancellation_token.raise_if_cancelled!
      raise DeadlineExceededError, "The run deadline was reached" if deadline && Time.now >= deadline

      timeout = deadline ? [deadline - Time.now, 0.05].min : 0.05
      @condition.wait(@mutex, [timeout, 0].max)
    end
    cancellation_token.raise_if_cancelled!
    raise DeadlineExceededError, "The run deadline was reached" if deadline && Time.now >= deadline
    raise @error if @error

    @value
  end
end