Worker GenServer para ejecución de tareas.
Ciclo de vida
init/1— Inicializa estado y se registra enArrea.Monitor.handle_info(:execute_task, state)— Ejecuta la primera tarea de la cola.handle_cast({:message, msg}, state)— Procesa mensajes recibidos de otros workers.terminate/2— Notifica al Monitor si el worker terminó de forma inesperada. Si terminó por su propio flujo normal (todas las tareas completadas o error gestionado), la notificación ya se emitió antes de{:stop, ...}yterminateno la duplica.
Formatos de mensaje aceptados
Worker.send_message/2 acepta:
- Mapas con clave
:type— mensaje estructurado genérico:%{type: :my_event, ...} - Tupla de enrutamiento:
{:send_to_worker, target_worker_id, payload}— reenvía elpayloadal worker identificado portarget_worker_id.
Política de errores
Si no se especifica policy al iniciar el worker, se usan los valores de
Arrea.Config (:default_policy, :max_retries, :retry_delay).
Uso
Arrea.Worker.start_link(id: :worker_1, tasks: [fn -> :work end], parent: self())
Arrea.Worker.send_message(:worker_1, %{type: :ping})
Arrea.Worker.send_message(:worker_1, {:send_to_worker, :worker_2, %{type: :data, value: 42}})
{:ok, state} = Arrea.Worker.get_state(:worker_1)
Summary
Functions
Returns a specification to start this module under a supervisor.
Obtiene el estado actual del worker.
Envía un mensaje al worker identificado.
Inicia un worker con opciones configurables.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_state(atom()) :: {:ok, Arrea.WorkerState.t()} | {:error, :not_found}
Obtiene el estado actual del worker.
Ejemplos
iex> Worker.get_state(:worker_1)
{:ok, %WorkerState{id: :worker_1, ...}}
iex> Worker.get_state(:nonexistent)
{:error, :not_found}
Envía un mensaje al worker identificado.
Formatos aceptados:
%{type: atom(), ...}— mensaje estructurado{:send_to_worker, target_id, payload}— enrutapayloada otro worker
Ejemplos
iex> Worker.send_message(:worker_1, %{type: :ping})
:ok
iex> Worker.send_message(:worker_1, {:send_to_worker, :worker_2, %{type: :data, value: 1}})
:ok
@spec start_link(keyword()) :: GenServer.on_start()
Inicia un worker con opciones configurables.
Opciones
:id— Identificador único del worker (requerido):tasks— Lista de funciones a ejecutar:parent— PID del proceso padre (Leader):log— Habilitar logging (default: false):policy— Política de manejo de errores. Si esnil, se usan los valores deArrea.Config(:default_policy,:max_retries,:retry_delay).:telemetry— Habilitar telemetría (default: false)
Ejemplos
iex> Worker.start_link(id: :worker_1, tasks: [fn -> :ok end])
{:ok, pid}