
    9ci
                         d Z ddlZddlZddlmZmZmZ  ej                  e      Z		 	 	 	 d
de
dedee
ef   dee
   dee   f
d	Zy)a  
Celery Task Dispatch Helper
===========================

Platform-aware Celery task dispatching utility.
Uses apply_async on Windows (solo pool) and send_task on Linux (prefork pool).

Usage:
    from src.smart_inventory.utils.celery_dispatch import dispatch_task
    
    # Instead of: compute_daily_sales.delay(target_date)
    result = dispatch_task(
        "src.smart_inventory.tasks.daily_sales_task.compute_daily_sales",
        kwargs={"target_date": target_date}
    )
    N)DictAnyOptional	task_nameargskwargstask_id	countdownc                    ddl m} |d}|i }	 t        j                  dk(  ra|j                  j                  |       }|r|j                  ||||      S t        j                  d|  d       |j                  | ||||      S |j                  | ||||      S # t        $ r!}t        j                  d	|  d
|         d}~ww xY w)aN  
    Dispatch a Celery task with platform-specific handling.
    
    Windows (solo pool): Uses direct task import and apply_async
    Linux (prefork pool): Uses send_task with task name string
    
    Args:
        task_name: Full Celery task name (e.g., "src.smart_inventory.tasks.daily_sales_task.compute_daily_sales")
        args: Positional arguments for the task
        kwargs: Keyword arguments for the task
        task_id: Optional task ID to use
        countdown: Optional delay in seconds before task execution
        
    Returns:
        AsyncResult or task ID string
    r   )
celery_appN win32)r   r   r	   r
   zTask z! not in registry, using send_taskzFailed to dispatch task z: )src.utils.celery_workerr   sysplatformtasksgetapply_asyncloggerdebug	send_task	Exceptionerror)r   r   r   r	   r
   r   taskes           H/var/www/html/hubwallet-dev/src/smart_inventory/utils/celery_dispatch.pydispatch_taskr      s    . 3|~ <<7"##''	2D''!#'	 (   uYK/PQR!++!#' ,   ''# (    /	{"QC@As$   AB .B B 	C#B??C)NNNN)__doc__r   loggingtypingr   r   r   	getLogger__name__r   strtupleintr   r       r   <module>r'      s{   "   & &			8	$
 !!#>>
> cN> c]	>
 }>r&   