
    ՞i                         d Z ddlZddlZddlmZ  ej
                  e      ZdadZ	dZ
dZdefdZdd	d
dee   defdZd Zy)u  
Genesis Task Queue — Dramatiq broker configuration.

Uses Redis as the message broker, connecting to Elestio-managed Redis at:
  redis-genesis-u50607.vm.elestio.app:26379

Credentials are sourced in priority order:
  1. REDIS_URL environment variable (full URL, overrides everything)
  2. GENESIS_REDIS_PASSWORD + GENESIS_REDIS_HOST + GENESIS_REDIS_PORT env vars
  3. Hard-coded Elestio defaults (same pattern as core/db/connections.py)

Calling configure_broker() is idempotent — subsequent calls are no-ops unless
force=True is passed.

# VERIFICATION_STAMP
# Story: M6.01 — broker.py — Dramatiq broker configuration
# Verified By: parallel-builder
# Verified At: 2026-02-25T00:00:00Z
# Tests: 4/4
# Coverage: 100%
    N)Optionalz#redis-genesis-u50607.vm.elestio.app26379ze2ZyYYr4oWRdASI2CaLc-returnc                  6   t         j                  j                  d      } | r| S t         j                  j                  dt              }t         j                  j                  dt              }t         j                  j                  dt
              }d| d| d| S )z
    Build the Redis connection URL from environment or hard-coded Elestio defaults.

    Follows the same precedence as core/memory/redis_l1_schema.py so all Genesis
    components point to the same broker.
    	REDIS_URLGENESIS_REDIS_HOSTGENESIS_REDIS_PORTGENESIS_REDIS_PASSWORDzredis://default:@:)osenvironget_DEFAULT_REDIS_HOST_DEFAULT_REDIS_PORT_DEFAULT_REDIS_PASS)urlhostportpasswords       //mnt/e/genesis-system/core/task_queue/broker.py_build_redis_urlr   %   sy     **..
%C

::>>.0CDD::>>.0CDDzz~~68KLHhZqav66    F)force	redis_urlr   c                   t         |st         S | xs
 t               }	 ddl}ddlm}  ||      a |j                  t                d|v r|j                  d      d   n|}t        j                  d|       t         S # t        $ r t        j                  d       Y yt        $ r+ t        j                  d	|j                  d      d          Y yw xY w)
u  
    Initialize the Dramatiq Redis broker and set it as the global broker.

    This function is idempotent — if the broker is already configured and
    ``force=False`` (the default), the call is a no-op and the existing
    broker is returned.

    Parameters
    ----------
    redis_url : str, optional
        Full Redis URL (e.g. ``redis://user:pass@host:port``).
        When omitted the URL is built from environment variables or Elestio
        defaults via ``_build_redis_url()``.
    force : bool
        Re-initialize even if a broker is already configured.

    Returns
    -------
    dramatiq.brokers.redis.RedisBroker | None
        The configured broker, or ``None`` when dramatiq is not installed.
    Nr   )RedisBroker)r   r   z4Dramatiq broker configured: redis://<credentials>@%sud   dramatiq package not installed — task queue is disabled. Install with: pip install dramatiq[redis]z,Failed to configure Dramatiq broker (url=%s))_brokerr   dramatiqdramatiq.brokers.redisr   
set_brokersplitloggerinfoImportErrorwarning	Exception	exception)r   r   r   r    r   safe_urls         r   configure_brokerr+   8   s    0 5

)')C6#&G$ *-399S>"%JHU 8	
  GSVXZI[\s   AA< <C0CCc                      t         S )z
    Return the currently configured Dramatiq broker.

    Returns ``None`` if ``configure_broker()`` has not been called or if
    dramatiq is not installed.
    )r    r   r   
get_brokerr.   m   s	     Nr   )N)__doc__r   loggingtypingr   	getLogger__name__r$   r   r   r   r   strr   boolr+   r.   r-   r   r   <module>r6      sr   * 
  			8	$  <  - 7# 7&2u 2 2 2jr   