a
    d*                     @   s   d dl 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 d dlZd dlmZ d dlmZ eeZG d	d
 d
ejZG dd dejZG dd dejZdS )    )	timedeltadatetime)modelstransaction)UniqueConstraint)timezone)gettext_lazyN)util)get_django_internal_datetimec                   @   sT   e Zd ZejddeddZejdddeddZe	 Z
dd ZG d	d
 d
ZdS )	DjangoJob   TzUnique id for this job.)
max_lengthprimary_key	help_textzADate and time at which this job is scheduled to be executed next.)db_indexblanknullr   c                 C   s.   | j rdt| j  nd}| j d| dS )Nznext run at: pausedz ())next_run_timer	   Zget_local_dt_formatid)selfstatus r   U/var/www/html/django/DPS/env/lib/python3.9/site-packages/django_apscheduler/models.py__str__    s
    zDjangoJob.__str__c                   @   s   e Zd ZdZdS )zDjangoJob.Meta)r   N)__name__
__module____qualname__orderingr   r   r   r   Meta(   s   r    N)r   r   r   r   	CharField_r   DateTimeFieldr   ZBinaryFieldZ	job_stater   r    r   r   r   r   r      s   
	r   c                   @   s   e Zd ZedddZdS )DjangoJobExecutionManager)max_agec                 C   s"   | j t t|d d  dS )z
        Delete old job executions from the database.

        :param max_age: The maximum age (in seconds). Executions that are older
        than this will be deleted.
        )seconds)Zrun_time__lteN)filterr   nowr   delete)r   r%   r   r   r   delete_old_job_executions-   s    z3DjangoJobExecutionManager.delete_old_job_executionsN)r   r   r   intr*   r   r   r   r   r$   ,   s   r$   c                
   @   s  e Zd ZdZdZdZdZdZdd eeefD Ze	j
ded	d
Ze	jee	jeddZe	jdeeddZe	jdeddZe	jddddeddZe	jddddeddZe	jddeddZe	jdeddZe Zeejd$e e!e e e d dddZ"d d! Z#G d"d# d#Z$dS )%DjangoJobExecutionzStarted executionZExecutedzMissed!zMax instances!zError!c                 C   s   g | ]}||fqS r   r   ).0xr   r   r   
<listcomp>>   s   zDjangoJobExecution.<listcomp>Tz!Unique ID for this job execution.)r   r   z'The job that this execution relates to.)Z	on_deleter   2   z)The current status of this job execution.)r   choicesr   z-Date and time at which this job was executed.)r   r         Nz(Total run time of this job (in seconds).)Z
max_digitsZdecimal_placesdefaultr   r   z)Timestamp at which this job was finished.i  zADetails of exception that occurred during job execution (if any).)r   r   r   zCTraceback of exception that occurred during job execution (if any).)r   r   )job_idrun_timer   	exception	tracebackreturnc           
      C   s2  | t |}t t }||  }| }zt ~ tj	 j
||d}	|tjkr||	W  d   W W  d   S ||	_||	_||	_|r||	_|r||	_|	  W d   n1 s0    Y  W nB tjy   |tjkrd}d}tjj|||||||d}	Y n0 W d   n1 s$0    Y  |	S )a  
        Uses an APScheduler lock to ensure that only one database entry can be created / updated at a time.

        This keeps django_apscheduler in sync with APScheduler and maintains a 1:1 mapping between APScheduler events
        that are triggered and the corresponding DjangoJobExecution model instances that are persisted to the database.
        :param lock: The lock to use when updating the database - probably obtained by calling _scheduler._create_lock()
        :param job_id: The ID to the APScheduler job that this job execution is for.
        :param run_time: The scheduler runtime for this job execution.
        :param status: The new status for ths job execution.
        :param exception: Details of any exceptions that need to be logged.
        :param traceback: Traceback of any exceptions that occurred while executing the job.
        :return: The ID of the newly created or updated DjangoJobExecution.
        )r5   r6   N)r5   r6   r   durationfinishedr7   r8   )r
   r   r(   total_seconds	timestampr   Zatomicr,   objectsZselect_for_updategetSENTr;   r:   r   r7   r8   saveZDoesNotExistcreate)
clslockr5   r6   r   r7   r8   r;   r:   Zjob_executionr   r   r   atomic_update_or_create   sD    


	"*
,
z*DjangoJobExecution.atomic_update_or_createc                 C   s   | j  d| j d| j dS )Nz: job 'z' (r   )r   r5   r   )r   r   r   r   r      s    zDjangoJobExecution.__str__c                   @   s"   e Zd ZdZeddgddgZdS )zDjangoJobExecution.Meta)z	-run_timer5   r6   Zunique_job_executions)fieldsnameN)r   r   r   r   r   constraintsr   r   r   r   r       s
   r    )NN)%r   r   r   r@   SUCCESSZMISSEDZMAX_INSTANCESERRORZSTATUS_CHOICESr   ZBigAutoFieldr"   r   Z
ForeignKeyr   ZCASCADEZjobr!   r   r#   r6   ZDecimalFieldr:   r;   r7   Z	TextFieldr8   r$   r>   classmethodr	   Zretry_on_db_operational_errorstrr   rE   r   r    r   r   r   r   r,   7   s   	  Qr,   )r   r   Z	django.dbr   r   Zdjango.db.modelsr   Zdjango.utilsr   Zdjango.utils.translationr   r"   loggingZdjango_apschedulerr	   Zdjango_apscheduler.utilr
   	getLoggerr   loggerZModelr   Managerr$   r,   r   r   r   r   <module>   s   
