o
    ýòTis  ã                   @   s’   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mZ d dlmZ e  e¡Zdd	„ ZG d
d„ deƒZdS )é    N)ÚVERSION)ÚBaseCommand)Úclose_old_connections)Ú
autoreload)ÚtasksÚautodiscover)ÚSignalManagerc                  C   s4   G dd„ dt ƒ} G dd„ dt ƒ}| ƒ t_|ƒ t_d S )Nc                   @   ó   e Zd Zdd„ ZdS )z)_configure_log_std.<locals>.StdOutWrapperc                 S   ó   t  |¡ d S ©N)ÚloggerÚinfo©ÚselfÚs© r   úq/var/www/html/evchargy.com/venv/lib/python3.10/site-packages/background_task/management/commands/process_tasks.pyÚwrite   ó   z/_configure_log_std.<locals>.StdOutWrapper.writeN©Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   ÚStdOutWrapper   ó    r   c                   @   r	   )z)_configure_log_std.<locals>.StdErrWrapperc                 S   r
   r   )r   Úerrorr   r   r   r   r      r   z/_configure_log_std.<locals>.StdErrWrapper.writeNr   r   r   r   r   ÚStdErrWrapper   r   r   )ÚobjectÚsysÚstdoutÚstderr)r   r   r   r   r   Ú_configure_log_std   s   r!   c                	       s¶   e Zd ZdZdddedddœfddd	ed
ddœfdddddœfdddddœfdddddœffZedk rEddlm	Z	 e
jedd„ eD ƒƒ Zdd„ Z‡ fdd„Zdd „ Zd!d"„ Z‡  ZS )#ÚCommandz0Run tasks that are scheduled to run on the queue)z
--durationÚstoreÚdurationr   zHRun task for this many seconds (0 or less to run forever) - default is 0)ÚactionÚdestÚtypeÚdefaultÚhelp)z--sleepÚsleepç      @z]Sleep for this many seconds before checking for new tasks (if none were found) - default is 5)z--queueÚqueuez&Only process tasks on this named queue)r%   r&   r)   )z	--log-stdÚ
store_trueÚlog_stdz0Redirect stdout and stderr to the logging system)z--devÚdevz?Auto-reload your code on changes. Use this only for development)é   é   ©Úmake_optionc                 C   s   g | ]\}}t |i |¤Ž‘qS r   r2   )Ú.0ÚargsÚkwargsr   r   r   Ú
<listcomp>E   s    zCommand.<listcomp>c                 C   s$   | j D ]\}}|j|i |¤Ž qd S r   )ÚOPTIONSÚadd_argument)r   Úparserr5   r6   r   r   r   Úadd_argumentsH   s   ÿzCommand.add_argumentsc                    s&   t t| ƒj|i |¤Ž d | _t| _d S r   )Úsuperr"   Ú__init__Úsig_managerr   Ú_tasks)r   r5   r6   ©Ú	__class__r   r   r=   L   s   
zCommand.__init__c           
      O   sî   |  dd¡}|  dd¡}|  dd ¡}|  dd¡}|  dd¡}| j}|r't ¡  |r,tƒ  tƒ  t ¡ }	|dks?t ¡ |	 |kru|jrDd S | j 	|¡sXt
ƒ  t d	¡ t |¡ nt t |jd |jd
 ¡¡ |dks?t ¡ |	 |ks?d S d S )Nr$   r   r*   r+   r,   r.   Fr/   zwaiting for tasksr0   )Úgetr>   r   Zraise_last_exceptionr!   r   ÚtimeZkill_nowr?   Zrun_next_taskr   r   Údebugr*   ÚrandomÚuniformZtime_to_wait)
r   r5   Úoptionsr$   r*   r,   r.   Úis_devr>   Ú
start_timer   r   r   ÚrunQ   s*   
 ôzCommand.runc                 O   s\   |  dd¡}tƒ | _|r$tj}tdk rtj}|| jg|¢R i |¤Ž d S | j|i |¤Ž d S )Nr/   F)é   rK   )rB   r   r>   r   Zrun_with_reloaderr   ÚmainrJ   )r   r5   rG   rH   Zreload_funcr   r   r   Úhandler   s   zCommand.handle)r   r   r   r)   ÚintÚfloatr8   r   Úoptparser3   r   Úoption_listÚtupler;   r=   rJ   rM   Ú__classcell__r   r   r@   r   r"      sN    ûûýýýç !r"   )ÚloggingrE   r   rC   Údjangor   Zdjango.core.management.baser   Z	django.dbr   Zdjango.utilsr   Zbackground_task.tasksr   r   Zbackground_task.utilsr   Ú	getLoggerr   r   r!   r"   r   r   r   r   Ú<module>   s   
