
    /hIO                        d Z ddl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
 ddlmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZ ddlmZmZmZmZ ddlmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z& ddl'm(Z( ddl)m*Z* dZ+ e$e,          Z-ej        .                    d          Z/ e0ed          Z1g dZ2dZ3dZ4d Z5ej6        fdZ7 G d de*          Z8dddedfde8fd Z9de8fd!Z:de8fd"Z;de8fd#Z<e
d$k    r ee9dd%e<e&          Z=n ee9dd'          Z=e/s ee9d$d%e<e&          Z>nd( xZ=Z>d) Z?e/s ee9d*e?e+          Z@nd, Z@d- ZAd6d/ZBd7d1ZC	 	 d8d4ZDd6d5ZEdS )9zWorker command-line program.

This module is the 'program-version' of :mod:`celery.worker`.

It does everything necessary to run that module
as an actual application, like installing signal handlers,
platform tweaks, and so on.
    N)datetime)partial)REMAP_SIGTERM)current_process)safe_str)VERSION_BANNER	platformssignals)trace)	AppLoader)
EX_FAILUREEX_OKcheck_privilegesisatty)staticterm)cry)qualname)
get_loggerin_sighandlerset_in_sighandler)	pluralize)WorkController)Workerjavapypy_version_info) ----------------- ***** ------- ******* ----- *** --- * ---- ** ----------r!   r!   r!   r    r   r   r   z{hostname} v{version}

{platform} {timestamp}

[config]
.> app:         {app}
.> transport:   {conninfo}
.> results:     {results}
.> concurrency: {concurrency}
.> task events: {events}

[queues]
{queues}
z
[tasks]
{tasks}
c                  P    ddl m}  t          d  |             D                       S )Nr   	enumeratec              3   N   K   | ] }|j                             d           dV  !dS )zDummy-   N)name
startswith).0ts     V/var/www/html/e360mart/e360mart_env/lib/python3.11/site-packages/celery/apps/worker.py	<genexpr>z&active_thread_count.<locals>.<genexpr>L   sJ       3 3Qf''113q 3 3 3 3 3 3    )	threadingr$   sumr#   s    r+   active_thread_countr0   J   sG    ###### 3 3))++ 3 3 3 3 3 3r-   c                     t          |d          rS|                                At          j        |                                d|  d                                           d S d S d S )Nfileno
)hasattrr2   oswriteencode)msgfs     r+   safe_sayr:   P   se    q( 4

 6
\#\\\0022333334 4 6 6r-   c                        e Zd ZdZddZ	 	 d fd	Zd Z fdZd Zd	 Z	dd
Z
d ZddZd ZddZd Zd Zd Z xZS )r   zWorker as a program.Fc                     || _         t          j        | j        | j                   t
          j                            | j        | | j        j        |           t          | j        j        j
                   d S )N)senderinstanceconfoptions)quietr   setup_worker_optimizationsapphostnamer
   celeryd_initsendr?   r   accept_content)selfrA   kwargss      r+   on_before_initzWorker.on_before_initX   so    
(4=AAA 	!!=4 	" 	
 	
 	
 	566666r-   Nc                 r   | j                             d|          | _        | j                             d|          | _         t	                      j        di | || _        || _        t          t          j
                  | _        | j         j                            | j        || n|          | _        d S )Nworker_redirect_stdoutsworker_redirect_stdouts_level)enabled )rC   eitherredirect_stdoutsredirect_stdouts_levelsupersetup_defaultspurgeno_colorr   sysstdout_isattylogcoloredlogfile)rH   rU   rV   rQ   rR   rI   	__class__s         r+   on_after_initzWorker.on_after_initd   s     !%%'7!9 !9&*hoo+-C'E 'E#(((((
 cj))x|++L$,$8LLh , 
 
r-   c                 v    |                                  | _        t          j        | j        | j                   d S N)setup_logging_custom_loggingr   rB   rC   rD   rH   s    r+   on_init_blueprintzWorker.on_init_blueprintt   s5    #1133 	(4=AAAAAr-   c                    | j         }t                                                       t          j                            | j        | |j                   | j        r| 	                                 | j
        s|                                  |                     d           |                     |            | j        s&| j        r|j                            | j                   d}|j        }t'          |t(                    r|                                dv}|r5|j                                        rt.                              d           d S d S d S )N)r=   r>   r?   z-active-T)zdjango.conf:settingszPlease run `celery upgrade settings path/to/settings.py` to avoid these warnings and to allow a smoother upgrade to Celery 6.0.)rC   rS   on_startr
   celeryd_after_setuprF   rD   r?   rU   purge_messagesrA   emit_bannerset_process_statusinstall_platform_tweaksrb   rQ   rZ   rR   _config_source
isinstancestrlowermaybe_warn_deprecated_settingsloggerwarning)rH   rC   warn_deprecatedconfig_sourcer]   s       r+   rf   zWorker.on_startz   sv   h 	#((=4ch 	) 	
 	
 	
 : 	"!!!z 	
+++$$T***# 	B(= 	BG$$T%@AAA *mS)) 	 ,1133 < O  	x6688 %    	 	 r-   c                    t          j                    }|r2t          t          j        t	          j                                         t          t          d                    t          | j	        
                    d|                     |                               t          | j	                            |                                 pd                    g                    t          j        d           d S )N z 
)artlinesT)fileflush)r   supports_imagesprintimgcatr   logor   joinrn   r[   cyanstartup_inforeset
extra_inforW   
__stdout__)rH   	use_images     r+   ri   zWorker.emit_banner   s     (**	 	.$+fkmm,,---hrww!!t((i-(@@B B C C""4??#4#4#:;;<< 
     .		. 	. 	. 	. 	. 	.r-   c                     t           j                            |           t                              dt          | j                             d S )N)r=   z	%s ready.)r
   worker_readyrF   rq   infor   rD   )rH   consumers     r+   on_consumer_readyzWorker.on_consumer_ready   s@    !!!222K$-!8!899999r-   c                     || j         | j          }| j        j                            | j        | j        d|| j                  S )NF)rQ   colorizerD   )rV   rC   rZ   setuploglevelr\   rD   )rH   r   s     r+   ra   zWorker.setup_logging   sL     9=(Hx|!!M4<"X " 
 
 	
r-   c           
          | j                                         5 }| j         j                            |          }|r&t	          d| dt          |d           dd           d d d            d S # 1 swxY w Y   d S )N)
connectionzpurge: Erased  messagez from the queue.
T)ry   )rC   connection_for_writecontrolrU   r{   r   )rH   r   counts      r+   rh   zWorker.purge_messages   s    X**,, 	l
H$**j*AAE l^u^^y	/J/J^^^fjkkkk	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	l 	ls   A	A00A47A4Tr3   celery.c                 x    |                     fdt          | j        j                  D                       S )Nc              3   V   K   | ]#}s|                               n|d | V  $dS )z  . N)r(   )r)   taskinclude_builtinsint_s     r+   r,   z"Worker.tasklist.<locals>.<genexpr>   sd       
 
"1A
OOD))
 
GK
4MM
 
 
 
 
 
r-   )r~   sortedrC   tasks)rH   r   sepr   s    ` `r+   tasklistzWorker.tasklist   sQ    xx 
 
 
 
 
&,TX^&<&<
 
 
 
 
 	
r-   c                     | j         d S | j         t          j        k    rF| j         t          j        k    }|                     |          }t
                              |          S d S )N)r   )r   )r   loggingINFODEBUGr   EXTRA_INFO_FMTformat)rH   r   r   s      r+   r   zWorker.extra_info   s`    = F=GL((#}=}}6F}GGH!((x(888 )(r-   c                    | j         }t          | j                  }d                    |j        pdt          |                    }t          |j        t                    s<t          |j                  }|
                    d          r
|dd          }|d| dz  }| j        r| j        \  }}d| d| d	}| j        }t          |t                    s|j        }|d|                    d
          d          dz  }d}	| j        sd}	t                               |t#          | j                  t'          j                                        d          t,          | j                                                                         | j         j                                        |t#          t5          j                              |	|j        j                            dd          
  
                                        }
|r_t?          |
          D ]O\  }}	 d                     tB          |         |
|         g          |
|<   2# tD          $ r d|
|         z   |
|<   Y Lw xY wd                     |
          dz   S )Nz{}:{:#x}__main__zcelery.loaders    ()z{min=z, max=}.ONz/OFF (enable -E to monitor tasks in this worker)r   )microsecondF)indentindent_first)
rC   rD   	timestampversionconninforesultsconcurrencyplatformeventsqueuesr   z                r3   )#rC   rn   r   r   mainidrm   loaderr   r   r(   	autoscalepool_cls
__module__splittask_eventsBANNERr   rD   r   nowreplacer   r   as_uribackend	_platformr   amqpr   
splitlinesr$   r~   ARTLINES
IndexError)rH   rw   rC   r   apprr   maxminpoolr   banneri_s                r+   r   zWorker.startup_info   sj   h$*++  !7ZCAA#*i00 	#cj))F  !122 %NNNN"D> 	6~HC5355c555K}$$$ 	#?D2DJJsOOB/2222 	GFFdm,,lnn,,,;;"X((**1133H$++--#i022338?)))GG  
 
 *,, 	  	5!&)) 5 515 #(1+vay)A B BF1II! 5 5 5 (6!9 4F1III5yy  4''s   +H==IIc                 <   | j         j        r|                                  | j        s+| j         j        rt	          |           nt          |           t          |           t          |           t          |           t                       t                       dS )z1Install platform specific tweaks and workarounds.N)rC   IS_macOS macOS_proxy_detection_workaroundrY   !install_HUP_not_supported_handlerinstall_worker_restart_handlerinstall_worker_term_handler install_worker_term_hard_handlerinstall_worker_int_handlerinstall_cry_handlerinstall_rdb_handler)rH   workers     r+   rk   zWorker.install_platform_tweaks   s    8 	411333 | 		7 x  7 2&9999.v666#F+++(000"6***r-   c                 F    t           j                            dd           dS )z6See https://github.com/celery/celery/issues#issue/161.celery_dummy_proxyset_by_celerydN)r5   environ
setdefaultrc   s    r+   r   z'Worker.macOS_proxy_detection_workaround  s"    

24DEEEEEr-   c                 ~    t          j        d| dt          j        t          j                   d| j                  S )Ncelerydr   r   )r   rD   )r	   set_mp_process_titlestrargvrW   argvrD   )rH   r   s     r+   rj   zWorker.set_process_status  sE    -::I-ch77:::]
 
 
 	
r-   )F)FNNNr`   )Tr3   r   )T)__name__r   __qualname____doc__rJ   r^   rd   rf   ri   r   ra   rh   r   r   r   rk   r   rj   __classcell__)r]   s   @r+   r   r   U   s7       
7 
7 
7 
7 37DH
 
 
 
 
 
 B B B& & & & &P
. 
. 
.: : :
 
 
 
l l l
 
 
 
9 9 9(( (( (( ((T  ,F F F
 
 
 
 
 
 
r-   r   SIGTERMWarmTr   c                 r      fd}t          d           |_        |t          j        <   dS )a<  Install signal handler for warm/cold shutdown.

    The handler will run from the MainProcess.

    Args:
        worker (Worker): The worker that received the signal.
        sig (str, optional): The signal that was received. Defaults to 'TERM'.
        how (str, optional): The type of shutdown to perform. Defaults to 'Warm'.
        callback (Callable, optional): Signal handler. Defaults to None.
        exitcode (int, optional): The exit code to use. Defaults to EX_OK.
        verbose (bool, optional): Whether to print the type of shutdown. Defaults to True.
    c                  n   t                      5  ddlm} t                      j        dk    rUr            rt          d dt          j                   t          j	        
                    j                   t          |ddd	                    d d d            d S # 1 swxY w Y   d S )
Nr   stateMainProcesszworker: z shutdown (MainProcess))r=   sighowexitcodeshould_stopshould_terminate)r   Cold)r   celery.workerr   r   _namer:   rW   r   r
   worker_shutting_downrF   rD   setattr)argsr   callbackr   r   r   verboser   s     r+   _handle_requestz*_shutdown_handler.<locals>._handle_request'  sI   __ 	H 	H++++++  &-77 %HV$$$ VDDDDcnUUU,11!?% 2    EM$68 88;=>FH H H	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hs   BB**B.1B.worker_N)rn   r   r	   r
   )r   r   r   r   r   r   r   s   `````` r+   _shutdown_handlerr    so    H H H H H H H H H H  #?S??33O,Icr-   c                 .    ddl m}  |t                    )au  Signal handler for hard shutdown.

    The handler will terminate the worker immediately by force using the exit code ``EX_FAILURE``.

    In practice, you should never get here, as the standard shutdown process should be enough.
    This handler is only for the worst-case scenario, where the worker is stuck and cannot be
    terminated gracefully (e.g., spamming the Ctrl+C in the terminal to force the worker to terminate).

    Args:
        worker (Worker): The worker that received the signal.

    Raises:
        WorkerTerminate: This exception will be raised in the MainProcess to terminate the worker immediately.
    r   )WorkerTerminate)celery.exceptionsr  r   )r   r  s     r+   on_hard_shutdownr  9  s&     211111
/*
%
%%r-   c                     t          | dt          d           t          | dt                     | j                                         t	          dt
          j                   dS )am  This signal handler is called when the worker is in the middle of the soft shutdown process.

    When the worker is in the soft shutdown process, it is waiting for tasks to finish. If the worker
    receives a SIGINT (Ctrl+C) or SIGQUIT signal (or possibly SIGTERM if REMAP_SIGTERM is set to "SIGQUIT"),
    the handler will cancels all unacked requests to allow the worker to terminate gracefully and replace the
    signal handler for SIGINT and SIGQUIT with the hard shutdown handler ``on_hard_shutdown`` to terminate
    the worker immediately by force next time the signal is received.

    It will give the worker once last chance to gracefully terminate (the cold shutdown), after canceling all
    unacked requests, before using the hard shutdown handler to terminate the worker forcefully.

    Args:
        worker (Worker): The worker that received the signal.
    SIGINTF)r   r   r   SIGQUITr   r   z3Waiting gracefully for cold shutdown to complete...N)r   r  r   cancel_all_unacked_requestsr:   rW   r   r   s    r+   during_soft_shutdownr  L  sd    " %VDT^cdddd$VEUVVVV O//111 BCNSSSSSr-   c                 p   t          dt          j                   t          | dt                     t          | dt                     t
          dk    rt          | dt                     |                                  | j                                         | j        j	        
                                 dS )aZ	  Signal handler for cold shutdown.

    Registered for SIGQUIT and SIGINT (Ctrl+C) signals. If REMAP_SIGTERM is set to "SIGQUIT", this handler will also
    be registered for SIGTERM.

    This handler will initiate the cold (and soft if enabled) shutdown procesdure for the worker.

    Worker running with N tasks:
        - SIGTERM:
            -The worker will initiate the warm shutdown process until all tasks are finished. Additional.
            SIGTERM signals will be ignored. SIGQUIT will transition to the cold shutdown process described below.
        - SIGQUIT:
            - The worker will initiate the cold shutdown process.
            - If the soft shutdown is enabled, the worker will wait for the tasks to finish up to the soft
            shutdown timeout (practically having a limited warm shutdown just before the cold shutdown).
            - Cancel all tasks (from the MainProcess) and allow the worker to complete the cold shutdown
            process gracefully.

    Caveats:
        - SIGINT (Ctrl+C) signal is defined to replace itself with the cold shutdown (SIGQUIT) after first use,
        and to emit a message to the user to hit Ctrl+C again to initiate the cold shutdown process. But, most
        important, it will also be caught in WorkController.start() to initiate the warm shutdown process.
        - SIGTERM will also be handled in WorkController.start() to initiate the warm shutdown process (the same).
        - If REMAP_SIGTERM is set to "SIGQUIT", the SIGTERM signal will be remapped to SIGQUIT, and the cold
        shutdown process will be initiated instead of the warm shutdown process using SIGTERM.
        - If SIGQUIT is received (also via SIGINT) during the cold/soft shutdown process, the handler will cancel all
        unacked requests but still wait for the soft shutdown process to finish before terminating the worker
        gracefully. The next time the signal is received though, the worker will terminate immediately by force.

    So, the purpose of this handler is to allow waiting for the soft shutdown timeout, then cancel all tasks from
    the MainProcess and let the WorkController.terminate() to terminate the worker naturally. If the soft shutdown
    is disabled, it will immediately cancel all tasks let the cold shutdown finish normally.

    Args:
        worker (Worker): The worker that received the signal.
    z>worker: Hitting Ctrl+C again will terminate all running tasks!r  r	  r  r   N)r:   rW   r   r   r  r   wait_for_soft_shutdownr   r
  r   stopr  s    r+   on_cold_shutdownr  k  s    J Ms~^^^ %VDXYYYY$VEYZZZZ	!!(YI]^^^^ !!### O//111 Or-   r  r   )r   r   r   r   )r   r   c                      d S r`   rO   )akws     r+   <lambda>r    s    D r-   c                 ^    t          dt          j                   t          | dd           d S )NzXworker: Hitting Ctrl+C again will initiate cold shutdown, terminating all running tasks!r  F)r   r   )r:   rW   r   r   r  s    r+   	on_SIGINTr    s7    g^  $V5IIIIIIr-   r  )r   r   r   c                      d S r`   rO   )r   rI   s     r+   r   r     s    r-   c                      t          j        t          j        t          j        t          j        g           t          j        t          j        t          j        gt          j	        z              d S r`   )
r	   close_open_fdsrW   	__stdin__r   
__stderr__r5   execv
executabler   rO   r-   r+   _reload_current_workerr    sQ    s~s~    HS^cn-899999r-   SIGHUPc                 *    d }|t           j        |<   d S )Nc                      t          d           t          dd                    t          j                   dt          j                   ddl}|                    t                     ddl	m
} t          |_        dS )z5Signal handler restarting the current python program.TzRestarting celery worker (r   r   r   Nr   )r   r:   r~   rW   r   r   atexitregisterr  r   r   r   r   )r   r"  r   s      r+   restart_worker_sig_handlerzBinstall_worker_restart_handler.<locals>.restart_worker_sig_handler  s    $Cchhsx.@.@CCC	! 	! 	!.///''''''!r-   r	   r
   )r   r   r$  s      r+   r   r     s&    " " " 8Icr-   SIGUSR1c                 <    t           rd S d }|t          j        | <   d S )Nc                      t                      5  t          t                                 ddd           dS # 1 swxY w Y   dS )z=Signal handler logging the stack-trace of all active threads.N)r   r:   r   )r   s    r+   cry_handlerz(install_cry_handler.<locals>.cry_handler  s    __ 	 	SUUOOO	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   8<<)is_pypyr	   r
   )r   r)  s     r+   r   r     s3        )Icr-   CELERY_RDBSIGSIGUSR2c                 l    d }t           j                            |           r|t          j        |<   d S d S )Nc                      t                      5  ddlm}m} | r| d         n |            j        } ||           ddd           dS # 1 swxY w Y   dS )z=Signal handler setting a rdb breakpoint at the current frame.r   )_frame	set_tracer&   N)r   celery.contrib.rdbr/  r0  f_back)r   r/  r0  frames       r+   rdb_handlerz(install_rdb_handler.<locals>.rdb_handler  s    __ 	 	<<<<<<<<  $8DGGEIe	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   -A		AA)r5   r   getr	   r
   )envvarr   r4  s      r+   r   r     sF       
z~~f -!,	#- -r-   c                 0    fd}|t           j        <   d S )Nc                     t                      5  t          d                                         d d d            d S # 1 swxY w Y   d S )NzH{sig} not supported: Restarting with {sig} is unstable on this platform!)r   )r   r:   r   )signumr3  r   s     r+   warn_on_HUP_handlerz>install_HUP_not_supported_handler.<locals>.warn_on_HUP_handler  s    __ 	C 	C 228&S&//C C C	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	Cs   $AAAr%  )r   r   r:  s    ` r+   r   r     s6    C C C C C 1Icr-   )r  )r&  )r+  r,  )Fr   r   r5   r   r   rW   r   	functoolsr   billiard.commonr   billiard.processr   kombu.utils.encodingr   celeryr   r	   r
   
celery.appr   celery.loaders.appr   celery.platformsr   r   r   r   celery.utilsr   r   celery.utils.debugr   celery.utils.importsr   celery.utils.logr   r   r   celery.utils.textr   r   r   __all__r   rq   r(   	is_jythonr4   r*  r   r   r   r0   r  r:   r   r  r  r  r  r   r   r  r   r  r   r   r   r   rO   r-   r+   <module>rJ     s     				     



             ) ) ) ) ) ) , , , , , , ) ) ) ) ) ) 5 5 5 5 5 5 5 5 5 5       ( ( ( ( ( ( H H H H H H H H H H H H % % % % % % % % " " " " " " ) ) ) ) ) ) I I I I I I I I I I ' ' ' ' ' ' ( ( ( ( ( (
	H		L##F++	
'#*
+
+  
 3 3 3 N 4 4 4 4
B
 B
 B
 B
 B
^ B
 B
 B
J +4$Y^hl - -f - - - ->&V & & & &&T T T T T>5 V 5  5  5  5 r I")'yf?OZd# # # #*'yf# # #
  A'.wyf?OZd( ( ($$
 ,A+@A(J J J  !(x)" " "
  : : :8 8 8 8	) 	) 	) 	)  /%- - - -1 1 1 1 1 1r-   