
    Vh9'                        d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ d d	lmZ d
gZ e       Zda G d de      Z G d d
ej2                        Zy)    )annotationsN)TemplateNotFound)Queue)AnsibleConnectionFailureAnsibleError)TaskExecutor)to_text)Display)contextWorkerProcessc                  "     e Zd ZdZ fdZ xZS )WorkerQueuez.Queue that raises AnsibleError items on get().c                R    t        t        | 
  |i |}t        |t              r||S N)superr   get
isinstancer   )selfargskwargsresult	__class__s       O/home/dcms/DCMS/lib/python3.12/site-packages/ansible/executor/process/worker.pyr   zWorkerQueue.get*   s-    {D-t>v>fl+L    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r   (   s    8 r   r   c                  J     e Zd ZdZ fdZd Z fdZd Zd Zd Z	d Z
 xZS )	r   z
    The worker thread class, which uses TaskExecutor to run tasks
    read from a job queue and pushes results into a results queue
    for reading later.
    c
                   t         t        |           || _        || _        || _        || _        || _        || _        || _	        || _
        t               | j                  _        t        t              | _        |	| _        y )N)ctx)r   r   __init___final_q
_task_vars_host_task_play_context_loader_variable_manager_shared_loader_objset
_tempfilesr   multiprocessing_contextworker_queue	worker_id)r   final_q	task_varshosttaskplay_contextloadervariable_managershared_loader_objr1   r   s             r   r$   zWorkerProcess.__init__8   sp    mT+-#

)!1"3 #&%',CD"r   c                   d | _         	 t        j                  j                         rht        j                  j	                         J	 t        j                  t        j                  t        j                  j	                                     | _         | j                   t        t
        j                        | _         y y # t        $ r Y 7w xY w# t        t        f$ r Y Lw xY wr   )
_new_stdinsysstdinisattyfilenoosfdopendupOSErrorAttributeError
ValueErroropendevnullr   s    r   _save_stdinzWorkerProcess._save_stdinL   s    
	yy!cii&6&6&8&D&(iisyy7G7G7I0J&KDO ??""2::.DO #    
+ 		s0   <C
 A	B; ;	CC
 CC
 
CCc                   | j                          t        j                  5  	 t        t        |          | j                  j                          cddd       S # | j                  j                          w xY w# 1 sw Y   yxY w)al  
        multiprocessing.Process replaces the worker's stdin with a new file
        but we wish to preserve it if it is connected to a terminal.
        Therefore dup a copy prior to calling the real start(),
        ensuring the descriptor is preserved somewhere in the new child, and
        make sure it is closed in the parent when start() completes.
        N)rI   display_lockr   r   startr;   close)r   r   s    r   rM   zWorkerProcess.start]   sh     	]] 	((]D79%%'		( 	( %%'		( 	(s!   A8AA8A55A88Bc                    	 t         j                  dt        |      z         t	        j
                  d       y# t        $ r Y !w xY w)a  
        There is no safe exception to return to higher level code that does not
        risk an innocent try/except finding itself executing in the wrong
        process. All code executing above WorkerProcess.run() on the stack
        conceptually belongs to another program.
        zWORKER HARD EXIT: %s   N)rK   debugr	   BaseExceptionr@   _exitr   es     r   
_hard_exitzWorkerProcess._hard_exitn   s@    	MM1GAJ>? 	  	 		s   !9 	AAc                   	 | j                         t        t        j                  d      xt        _        t        _        S # t        $ r}| j                  |       Y d}~nd}~ww xY w	 t        t        j                  d      xt        _        t        _        y# t        t        j                  d      xt        _        t        _        w xY w)a  
        Wrap _run() to ensure no possibility an errant exception can cause
        control to return to the StrategyBase task loop, or any other code
        higher in the stack.

        As multiprocessing in Python 2.x provides no protection, it is possible
        a try/except added in far-away code can cause a crashed child process
        to suddenly assume the role and prior state of its parent.
        wN)	_runrF   r@   rG   r<   stdoutstderrrR   rV   rT   s     r   runzWorkerProcess.run   s    	<99;  '+2::s&;;CJ  	OOA	 '+2::s&;;CJd2::s&;;CJs)   A   	A$	AB A$$B 0Cc                t	   t         j                  | j                         | a	 t         j	                  d| j
                  d| j                         t        | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  	      j                         }t         j	                  d| j
                  d| j                  d| j                  j                  d       t!               | j
                  _        g | j
                  _        t         j	                  d| j                  j                  z         	 | j                  j'                  | j
                  j(                  | j                  j                  || j                  j+                                t         j	                  d| j                  j                  z         t         j	                  d       y# t,        $ r}t         j	                  d| d	       | j                  j'                  | j
                  j(                  | j                  j                  d
| t/        j0                         di        Y d}~d}~ww xY w# t2        $ r t!               | j
                  _        g | j
                  _        | j                  j'                  | j
                  j(                  | j                  j                  t!        d
      | j                  j+                                Y At,        $ rw}t5        |t6        t8        t:        t<        f      rt5        |t>              r;	 t!               | j
                  _        g | j
                  _        | j                  j'                  | j
                  j(                  | j                  j                  t!        d
tA        t/        j0                               d      | j                  j+                                nc# t,        $ rW t         j	                  dtA        |      z         t         j	                  dtA        t/        j0                               z         Y nw xY w| jC                          n# | jC                          w xY wY d}~d}~ww xY w)z
        Called when the process is started.  Pushes the result onto the
        results queue. We also remove the host from the blocked hosts list, to
        signify that they are ready for their next task.
        zrunning TaskExecutor() for /z done running TaskExecutor() for z []zsending task result for task %s)task_fieldszfailed to send task result (z), sending surrogate resultT)failedmsg	exceptionNz$done sending task result for task %s)unreachable )ra   rc   rZ   zWORKER EXCEPTION: %szWORKER TRACEBACK: %szWORKER PROCESS EXITING)"rK   	set_queuer%   current_workerrQ   r'   r(   r   r&   r)   r;   r*   r,   r+   r\   _uuiddictvarsgroupssend_task_resultname
dump_attrs	Exception	traceback
format_excr   r   IOErrorEOFErrorKeyboardInterrupt
SystemExitr   r	   	_clean_up)r   executor_resultrU   s      r   rY   zWorkerProcess._run   s[    	$--( E	%MM

DJJWX*



""''&&
 ce  MM$**VZV`V`bfblblbrbrst"fDJJO "DJJ MM;djj>N>NNO..JJOOJJ$$# $

 5 5 7	 / ( MM@4::CSCSST: 	./W   <QC?Z[\..JJOOJJ$$ #'"#%.%9%9%;    ( 	"fDJJO "DJJMM**



  & JJ113	 +   	%a'85F
!STXbcdfvXw%&*fDJJO(*DJJ%MM22



((DGI<P<P<R4S\^_$(JJ$9$9$;	 3  ! ]MM"9GAJ"FGMM"9GIDXDXDZ<["[\] NN$DNN$	%s   EJ +AH 
,J 	JA6JJ JJ BR77	R7 1R22B0P#"R#AR RRRR2R))R22R7c                8    | j                   j                          y r   )r*   cleanup_all_tmp_filesrH   s    r   rv   zWorkerProcess._clean_up   s     	**,r   )r   r   r   r   r$   rI   rM   rV   r\   rY   rv   r   r    s   @r   r   r   1   s-    #(/"("$<:X0D-r   )
__future__r   r@   r<   rp   jinja2.exceptionsr   multiprocessing.queuesr   ansible.errorsr   r   ansible.executor.task_executorr   +ansible.module_utils.common.text.convertersr	   ansible.utils.displayr
   ansible.utils.multiprocessingr   r/   __all__rK   rg   r   Processr    r   r   <module>r      s]   $ # 	 
  . ( A 7 ? ) L

)% Q-+33 Q-r   