Class: AgentsControl::Pending

Inherits:
Object
  • Object
show all
Defined in:
lib/agents_control/pending.rb

Overview

Questions currently waiting for an answer.

The thread serving the hook parks here until a human presses a button in Telegram, or time runs out. This wait is exactly what holds the agent blocked — that's the whole point of it.

Held only in memory, and that's deliberate. If the daemon crashes, the HTTP connection to the agent drops too: the agent gets a network error, treats it as "no decision," and continues on its own. There's nothing to restore after a restart — nobody's left waiting.

Defined Under Namespace

Classes: Question

Instance Method Summary collapse

Constructor Details

#initializePending

Returns a new instance of Pending.



17
18
19
20
# File 'lib/agents_control/pending.rb', line 17

def initialize
  @questions = {}
  @mutex = Mutex.new
end

Instance Method Details

#allObject



50
# File 'lib/agents_control/pending.rb', line 50

def all = @mutex.synchronize { @questions.values.dup }

#answer(id, reply) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/agents_control/pending.rb', line 40

def answer(id, reply)
  question = @mutex.synchronize { @questions[id] }
  return false unless question

  question.queue.push(reply)
  true
end

#ask(event, timeout:) ⇒ Object

Register a question and wait for an answer. Returns Reply.none if nobody answered in time.

The block runs between registration and waiting: the message has to be sent once the question's identifier is already known, but before the thread goes to sleep. Otherwise the answer could arrive before we start waiting for it.



29
30
31
32
33
34
35
36
37
38
# File 'lib/agents_control/pending.rb', line 29

def ask(event, timeout:)
  question = register(event)

  begin
    yield(question.id) if block_given?
    question.queue.pop(timeout: timeout) || Reply.none
  ensure
    forget(question.id)
  end
end

#find(id) ⇒ Object



48
# File 'lib/agents_control/pending.rb', line 48

def find(id) = @mutex.synchronize { @questions[id] }

#sizeObject



52
# File 'lib/agents_control/pending.rb', line 52

def size = @mutex.synchronize { @questions.size }