Class: RubynCode::Autonomous::IdlePoller

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/autonomous/idle_poller.rb

Overview

Polls for new work when an agent is idle. Checks the mailbox (messages always take priority) and the task board on a regular interval. Blocks the calling thread until work is found, the idle timeout expires, or the poller is interrupted.

Instance Method Summary collapse

Constructor Details

#initialize(mailbox:, task_manager:, agent_name:, poll_interval: 5, idle_timeout: 60) ⇒ IdlePoller

Returns a new instance of IdlePoller.

Parameters:

  • mailbox (#pending_for)

    message mailbox

  • task_manager (#db)

    task persistence layer

  • agent_name (String)

    the polling agent’s identifier

  • poll_interval (Numeric) (defaults to: 5)

    seconds between polls (default 5)

  • idle_timeout (Numeric) (defaults to: 60)

    max seconds to wait before shutdown (default 60)



15
16
17
18
19
20
21
22
# File 'lib/rubyn_code/autonomous/idle_poller.rb', line 15

def initialize(mailbox:, task_manager:, agent_name:, poll_interval: 5, idle_timeout: 60)
  @mailbox = mailbox
  @task_manager = task_manager
  @agent_name = agent_name
  @poll_interval = poll_interval
  @idle_timeout = idle_timeout
  @interrupted = false
end

Instance Method Details

#interrupt!void

This method returns an undefined value.

Signals the poller to stop at the next iteration.



52
53
54
# File 'lib/rubyn_code/autonomous/idle_poller.rb', line 52

def interrupt!
  @interrupted = true
end

#poll!:resume, ...

Blocks the caller, polling for new work at the configured interval.

Returns:

  • (:resume, :shutdown, :interrupted)
    • :resume - found work (message or task)

    • :shutdown - idle timeout elapsed with no work

    • :interrupted - #interrupt! was called externally



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubyn_code/autonomous/idle_poller.rb', line 30

def poll!
  deadline = monotonic_now + @idle_timeout

  loop do
    return :interrupted if @interrupted
    return :shutdown if monotonic_now >= deadline

    # Messages always take priority over tasks.
    return :resume if pending_messages?

    return :resume if claimable_task?

    remaining = deadline - monotonic_now
    return :shutdown if remaining <= 0

    sleep [remaining, @poll_interval].min
  end
end