Module: RubynCode::Autonomous::TaskClaimer

Defined in:
lib/rubyn_code/autonomous/task_claimer.rb

Overview

Claims and prepares unclaimed tasks for agent execution. Uses optimistic locking to handle race conditions when multiple agents attempt to claim the same task concurrently.

Constant Summary collapse

MAX_RETRIES =
3

Class Method Summary collapse

Class Method Details

.call(task_manager:, agent_name:, max_retries: MAX_RETRIES) ⇒ Tasks::Task?

Finds the first ready (pending, unowned) task that hasn’t exceeded max retries, claims it for the given agent, and returns the updated Task. Returns nil if no work is available.

Parameters:

  • task_manager (#db, #update_task, #list_tasks)

    task persistence layer

  • agent_name (String)

    unique identifier of the claiming agent

  • max_retries (Integer) (defaults to: MAX_RETRIES)

    maximum retry count before skipping a task

Returns:

  • (Tasks::Task, nil)

    the claimed task, or nil if none available



19
20
21
22
23
24
25
26
# File 'lib/rubyn_code/autonomous/task_claimer.rb', line 19

def self.call(task_manager:, agent_name:, max_retries: MAX_RETRIES)
  db = task_manager.db
  claim_next_pending_task(db, agent_name, max_retries)
  fetch_claimed_task(db, agent_name)
rescue StandardError => e
  RubynCode.logger.warn("TaskClaimer: failed to claim task: #{e.message}") if RubynCode.respond_to?(:logger)
  nil
end