o nnh9'@sddlmZddlZddlZddlZddlmZddlmZddl m Z m Z ddl m Z ddlmZddlmZdd lmZd gZeZdaGd d d eZGd d d ejZdS)) annotationsN)TemplateNotFound)Queue)AnsibleConnectionFailure AnsibleError) TaskExecutor)to_text)Display)context WorkerProcesscs eZdZdZfddZZS) WorkerQueuez.Queue that raises AnsibleError items on get().cs(tt|j|i|}t|tr||SN)superr get isinstancer)selfargskwargsresult __class__J/usr/local/lib/python3.10/dist-packages/ansible/executor/process/worker.pyr*s zWorkerQueue.get)__name__ __module__ __qualname____doc__r __classcell__rrrrr (sr csTeZdZdZfddZddZfddZdd 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 s^tt|||_||_||_||_||_||_||_ ||_ t |j_ t td|_| |_dS)N)ctx)rr __init___final_q _task_vars_host_task _play_context_loader_variable_manager_shared_loader_objset _tempfilesr multiprocessing_context worker_queue worker_id) rfinal_q task_varshosttask play_contextloadervariable_managershared_loader_objr,rrrr8s   zWorkerProcess.__init__c Csd|_z&tjr(tjdur(ztttj|_Wn ty'YnwWn t t fy4Ynw|jdurBt tj |_dSdSr ) _new_stdinsysstdinisattyfilenoosfdopendupOSErrorAttributeError ValueErroropendevnullrrrr _save_stdinLs  zWorkerProcess._save_stdinc sZ|tjztt|W|jWdS|jw1s&wYdS)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)rCdisplay_lockrr startr5closerBrrrrF]s   zWorkerProcess.startcCs8z tdt|Wn tyYnwtddS)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)rDdebugr BaseExceptionr:_exitrerrr _hard_exitns  zWorkerProcess._hard_exitc Csz5z|WWttjdt_t_Sty(}z ||WYd}~nd}~wwWttjdt_t_dSttjdt_t_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) _runr@r:rAr6stdoutstderrrJrNrLrrrruns .zWorkerProcess.runc Csnt|j|aztd|j|jft|j|j|j|j |j |j |j |j|j }td|j|j|jjft|j_g|j_td|jjz|jj|jj|jj||jdWn/ty}z#td|d|j|jj|jjd|tdiWYd }~nd }~wwtd |jjWntyt|j_g|j_|jj|jj|jjtdd |jdYnrty/}zet|ttttfrt|tr%zJz%t|j_g|j_|jj|jj|jjtdt td d |jdWntytdt |tdt tYnwW|!n|!wWYd }~nd }~wwtdd 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) task_fieldszfailed to send task result (z), sending surrogate resultT)failedmsg exceptionNz$done sending task result for task %s) unreachable)rUrWrQzWORKER EXCEPTION: %szWORKER TRACEBACK: %szWORKER PROCESS EXITING)"rD set_queuer current_workerrIr"r#rr!r$r5r%r'r&rS_uuiddictvarsgroupssend_task_resultname dump_attrs Exception traceback format_excrrIOErrorEOFErrorKeyboardInterrupt SystemExitrr _clean_up)rexecutor_resultrMrrrrPs         zWorkerProcess._runcCs|jdSr )r%cleanup_all_tmp_filesrBrrrrjszWorkerProcess._clean_up) rrrrrrCrFrNrSrPrjrrrrrr 1s  b) __future__rr:r6rdjinja2.exceptionsrmultiprocessing.queuesransible.errorsrransible.executor.task_executorr+ansible.module_utils.common.text.convertersransible.utils.displayr ansible.utils.multiprocessingr r*__all__rDr[r Processr rrrrs