Class: RubynCode::Autonomous::IdlePoller
- Inherits:
-
Object
- Object
- RubynCode::Autonomous::IdlePoller
- 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
-
#initialize(mailbox:, task_manager:, agent_name:, poll_interval: 5, idle_timeout: 60) ⇒ IdlePoller
constructor
A new instance of IdlePoller.
-
#interrupt! ⇒ void
Signals the poller to stop at the next iteration.
-
#poll! ⇒ :resume, ...
Blocks the caller, polling for new work at the configured interval.
Constructor Details
#initialize(mailbox:, task_manager:, agent_name:, poll_interval: 5, idle_timeout: 60) ⇒ IdlePoller
Returns a new instance of IdlePoller.
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.
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 return :resume if claimable_task? remaining = deadline - monotonic_now return :shutdown if remaining <= 0 sleep [remaining, @poll_interval].min end end |