o
    th                     @   s   d dl mZ d dlmZmZmZ d dlmZmZm	Z	 d dl
mZ zd dlZW n ey3   d dlZY nw z$d dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ W n eyc   ed	w G d
d deZdS )    )absolute_import)BaseJobStoreJobLookupErrorConflictingIdError)	maybe_refdatetime_to_utc_timestamputc_timestamp_to_datetime)JobN)	create_engineTableColumnMetaDataUnicodeFloatLargeBinaryselectand_)IntegrityError)nullz0SQLAlchemyJobStore requires SQLAlchemy installedc                       s   e Zd ZdZddddejddf fdd	Z fddZdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS ) SQLAlchemyJobStorea  
    Stores jobs in a database table using SQLAlchemy.
    The table will be created if it doesn't exist in the database.

    Plugin alias: ``sqlalchemy``

    :param str url: connection string (see
        :ref:`SQLAlchemy documentation <sqlalchemy:database_urls>` on this)
    :param engine: an SQLAlchemy :class:`~sqlalchemy.engine.Engine` to use instead of creating a
        new one based on ``url``
    :param str tablename: name of the table to store jobs in
    :param metadata: a :class:`~sqlalchemy.schema.MetaData` instance to use instead of creating a
        new one
    :param int pickle_protocol: pickle protocol level to use (for serialization), defaults to the
        highest available
    :param str tableschema: name of the (existing) schema in the target database where the table
        should be
    :param dict engine_options: keyword arguments to :func:`~sqlalchemy.create_engine`
        (ignored if ``engine`` is given)
    Napscheduler_jobsc              
      s   t t|   || _t|pt }|rt|| _n|r't|fi |p"i | _ntdt	||t
dtdddt
dtdddt
d	td
d|d| _d S )Nz%Need either "engine" or "url" definedid   T)primary_keynext_run_time   )index	job_stateF)nullable)schema)superr   __init__pickle_protocolr   r   enginer
   
ValueErrorr   r   r   r   r   jobs_t)selfurlr#   	tablenamemetadatar"   tableschemaengine_options	__class__ V/var/www/html/bot/env/lib/python3.10/site-packages/apscheduler/jobstores/sqlalchemy.pyr!   +   s   zSQLAlchemyJobStore.__init__c                    s&   t t| || | j| jd d S )NT)r    r   startr%   creater#   )r&   	scheduleraliasr,   r.   r/   r0   B   s   zSQLAlchemyJobStore.startc                 C   sl   t | jjj| jjj|k}| j }||	 }|r#| 
|nd W  d    S 1 s/w   Y  d S N)r   r%   cr   wherer   r#   beginexecutescalar_reconstitute_job)r&   job_id
selectable
connectionr   r.   r.   r/   
lookup_jobF   s
   $zSQLAlchemyJobStore.lookup_jobc                 C   s   t |}| | jjj|kS r4   )r   	_get_jobsr%   r5   r   )r&   now	timestampr.   r.   r/   get_due_jobsL   s   zSQLAlchemyJobStore.get_due_jobsc                 C   sv   t | jjj| jjjt k| jjjd}| j	 }|
| }t|W  d    S 1 s4w   Y  d S )N   )r   r%   r5   r   r6   r   order_bylimitr#   r7   r8   r9   r   )r&   r<   r=   r   r.   r.   r/   get_next_run_timeP   s   $z$SQLAlchemyJobStore.get_next_run_timec                 C   s   |   }| | |S r4   )r?   _fix_paused_jobs_sorting)r&   jobsr.   r.   r/   get_all_jobsX   s   
zSQLAlchemyJobStore.get_all_jobsc              	   C   s   | j  jdi |jt|jt| | j	d}| j
 }z|| W n ty3   t|jw W d    d S 1 s?w   Y  d S )N)r   r   r   r.   )r%   insertvaluesr   r   r   pickledumps__getstate__r"   r#   r7   r8   r   r   )r&   jobrJ   r=   r.   r.   r/   add_job]   s   

"zSQLAlchemyJobStore.add_jobc                 C   s   | j  jdi t|jt| | jd	| j j
j|jk}| j }||}|jdkr6t|jW d    d S 1 sAw   Y  d S )N)r   r   r   r.   )r%   updaterK   r   r   rL   rM   rN   r"   r6   r5   r   r#   r7   r8   rowcountr   )r&   rO   rQ   r=   resultr.   r.   r/   
update_jobi   s   


"zSQLAlchemyJobStore.update_jobc                 C   sf   | j  | j jj|k}| j }||}|jdkr!t	|W d    d S 1 s,w   Y  d S )Nr   )
r%   deleter6   r5   r   r#   r7   r8   rR   r   )r&   r;   rU   r=   rS   r.   r.   r/   
remove_jobs   s   

"zSQLAlchemyJobStore.remove_jobc                 C   sD   | j  }| j }|| W d    d S 1 sw   Y  d S r4   )r%   rU   r#   r7   r8   )r&   rU   r=   r.   r.   r/   remove_all_jobsz   s   
"z"SQLAlchemyJobStore.remove_all_jobsc                 C   s   | j   d S r4   )r#   disposer&   r.   r.   r/   shutdown   s   zSQLAlchemyJobStore.shutdownc                 C   s:   t |}| |d< tt}|| | j|_| j|_|S )Njobstore)rL   loadsr	   __new____setstate__
_scheduler_alias_jobstore_alias)r&   r   rO   r.   r.   r/   r:      s   


z$SQLAlchemyJobStore._reconstitute_jobc              
   G   s   g }t | jjj| jjj| jjj}|r|t| n|}t	 }| j
 P}||D ]$}z|| |j W q, tyP   | jd|j ||j Y q,w |rn| j | jjj|}|| W d    |S W d    |S 1 syw   Y  |S )Nz)Unable to restore job "%s" -- removing it)r   r%   r5   r   r   rD   r   r6   r   setr#   r7   r8   appendr:   BaseException_logger	exceptionaddrU   in_)r&   
conditionsrH   r<   failed_job_idsr=   rowrU   r.   r.   r/   r?      s0   

zSQLAlchemyJobStore._get_jobsc                 C   s   d| j j| jjf S )Nz<%s (url=%s)>)r-   __name__r#   r'   rY   r.   r.   r/   __repr__   s   zSQLAlchemyJobStore.__repr__)rl   
__module____qualname____doc__rL   HIGHEST_PROTOCOLr!   r0   r>   rB   rF   rI   rP   rT   rV   rW   rZ   r:   r?   rm   __classcell__r.   r.   r,   r/   r      s$    
	r   )
__future__r   apscheduler.jobstores.baser   r   r   apscheduler.utilr   r   r   apscheduler.jobr	   cPicklerL   ImportError
sqlalchemyr
   r   r   r   r   r   r   r   r   sqlalchemy.excr   sqlalchemy.sql.expressionr   r   r.   r.   r.   r/   <module>   s"    ,