a )g"@sddlmZmZmZeZddlZddlZddlZddl m Z ddl m Z ddl mZddlmZddlmZddlmZd gZeZGd d d ejZdS) )absolute_importdivisionprint_functionN)TemplateNotFound)AnsibleConnectionFailure) TaskExecutor)to_text)Display)context WorkerProcesscsTeZdZdZfddZddZfddZdd Zd d Zd d Z ddZ 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 sLtt|||_||_||_||_||_||_||_ ||_ t |j_ dSN) superr __init___final_q _task_vars_host_task _play_context_loaderZ_variable_manager_shared_loader_objsetZ _tempfiles) selfZfinal_qZ task_varshostZtaskZ play_contextloaderZvariable_managerZshared_loader_obj __class__C/usr/lib/python3.9/site-packages/ansible/executor/process/worker.pyr.szWorkerProcess.__init__c Csd|_zLtjrPtjdurPztttj|_WntyNYn0Wnt t fyhYn0|jdurt tj |_dSr ) _new_stdinsysstdinisattyfilenoosfdopendupOSErrorAttributeError ValueErroropendevnullrrrr _save_stdin?s   zWorkerProcess._save_stdinc sh|tjFz(tt|W|jWdS|j0Wdn1sZ0YdS)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)r,display_lockr r startrcloser+rrrr/Ps  zWorkerProcess.startcCs8ztdt|Wnty(Yn0tddS)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. zWORKER HARD EXIT: %sN)r-debugr BaseExceptionr#_exitrerrr _hard_exitas  zWorkerProcess._hard_exitc Cszhz |WWttjdt_t_StyP}z||WYd}~n d}~00Wttjdt_t_nttjdt_t_0dS)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) _runr)r#r*rstdoutstderrr3r7r5rrrrunss  "zWorkerProcess.runc Cst|jztd|j|jft|j|j|j|j|j |j |j |j }td|j|j|jj ft|j_g|j_td|jj |jj|jj|jj ||jdtd|jj Wn2tyt|j_g|j_|jj|jj|jj tdd|jdYnty}zt|ttttfrDt|trzzJt|j_g|j_|jj|jj|jj tdttdd |jdWn<tytd t|td ttYn0W|n |0WYd }~n d }~00td d S)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 %s/%sz*done running TaskExecutor() for %s/%s [%s]zsending task result for task %s)Z task_fieldsz$done sending task result for task %sT)Z unreachable)Zfailed exceptionr:zWORKER EXCEPTION: %szWORKER TRACEBACK: %sNzWORKER PROCESS EXITING) r-Z set_queuerr2rrrrrrrrr<Z_uuiddictvarsgroupsZsend_task_resultnameZ dump_attrsr Exception isinstanceIOErrorEOFErrorKeyboardInterrupt SystemExitrr traceback format_exc _clean_up)rZexecutor_resultr6rrrr9sb        *zWorkerProcess._runcCs|jdSr )rZcleanup_all_tmp_filesr+rrrrKszWorkerProcess._clean_up) __name__ __module__ __qualname____doc__rr,r/r7r<r9rK __classcell__rrrrr 's  O)Z __future__rrrtypeZ __metaclass__r#rrIZjinja2.exceptionsrZansible.errorsrZansible.executor.task_executorrZansible.module_utils._textrZansible.utils.displayr Zansible.utils.multiprocessingr Zmultiprocessing_context__all__r-ZProcessr rrrrs