B
    è?F[}  ã               @   sx   d dl Z d dlmZmZmZ d dlmZ d dlZd dlZd dl	Z	d dl
mZ dd„ ZG dd„ deƒZG d	d
„ d
eƒZdS )é    N)ÚStringPropertyÚDateTimePropertyÚIntegerProperty)ÚModel)ÚStringIOc             C   s,   | dkrd S t | ƒdk s$t | ƒdkr(t‚d S )NÚ*r   é   )ÚintÚ
ValueError)Úval© r   ú/lib/python3.7/site-packages/boto/manage/task.pyÚ
check_hour   s    r   c                   s†   e Zd ZdZeƒ ZededdZeddZe	ƒ Z
eƒ Zeƒ Zeƒ Zedd„ ƒZd‡ fd	d
„	Zdd„ Zdd„ Zddd„Zdd„ Z‡  ZS )ÚTaskaç  
    A scheduled, repeating task that can be executed by any participating servers.
    The scheduling is similar to cron jobs.  Each task has an hour attribute.
    The allowable values for hour are [0-23|*].

    To keep the operation reasonably efficient and not cause excessive polling,
    the minimum granularity of a Task is hourly.  Some examples:

         hour='*' - the task would be executed each hour
         hour='3' - the task would be executed at 3AM GMT each day.

    Tr   )ÚrequiredZ	validatorÚdefault)r   c             C   s    x|   ¡ D ]}| |¡ q
W d S )N)ÚallÚstart)ÚclsÚ
queue_nameÚtaskr   r   r   Ú	start_all9   s    zTask.start_allNc                s<   t t| ƒj|f|Ž | jdk| _| jdk| _tj ¡ | _d S )Nr   )	Úsuperr   Ú__init__ÚhourÚhourlyÚdailyÚdatetimeZutcnowÚnow)ÚselfÚidÚkw)Ú	__class__r   r   r   >   s    zTask.__init__c             C   s   t j d| j| j| jf ¡ | jr,| js,dS | jr|| js|t| j	ƒ| jj	krNdS t
t| j	ƒ| jj	 | jj	t| j	ƒ ƒd d S | j| j }| jr¨|jdkrœdS d|j S nTt| j	ƒ| jj	krÎ|jdkrÈdS dS n.t
t| j	ƒ| jj	 | jj	t| j	ƒ ƒd d S dS )ag  
        Determine how long until the next scheduled time for a Task.
        Returns the number of seconds until the next scheduled time or zero
        if the task needs to be run immediately.
        If it's an hourly task and it's never been run, run it now.
        If it's a daily task and it's never been run and the hour is right, run it now.
        z!checking Task[%s]-now=%s, last=%sr   é<   i  é   ipC N)ÚbotoÚlogÚinfoÚnamer   Úlast_executedr   r   r	   r   ÚmaxZsecondsZdays)r   Zdeltar   r   r   ÚcheckD   s"    .

z
Task.checkc             C   s  t j d| j| jf ¡ tƒ }tj| jdtjtjtjd}d}|}xj| 	¡ d kr¬t j d||f ¡ ||kr˜||7 }t j d| j|f ¡ |r˜| 
|¡ t d¡ |d7 }qDW | ¡ }| |d ¡ | |d ¡ t j d	| j| ¡ f ¡ | j| _|j| _| ¡ dd
… | _d S )NzTask[%s] - running:%sT)ÚshellÚstdinÚstdoutÚstderré   znsecs=%s, timeout=%sz(Task[%s] - setting timeout to %d secondsr   r$   zTask[%s] - output: %siÿ  )r%   r&   r'   r(   Úcommandr   Ú
subprocessÚPopenÚPIPEÚpollÚchange_visibilityÚtimeÚsleepZcommunicateÚwriteÚgetvaluer   r)   Ú
returncodeÚlast_statusÚlast_output)r   ÚmsgÚvtimeoutZlog_fpZprocessZnsecsZcurrent_timeoutÚtr   r   r   Ú_runf   s,    

z	Task._runr#   c             C   s¶   |   ¡ }tj d| j|f ¡ |dkr˜|  ||¡ |j}| | j¡}| 	|¡}|j| _
|  ¡  tj d| j|jf ¡ | ¡  tj d| j|jf ¡ ntj d| ¡ | |¡ d S )NzTask[%s] - delay=%s secondsr   zTask[%s] - new message id=%szTask[%s] - deleted message %sznew_vtimeout: %d)r+   r%   r&   r'   r(   rA   ÚqueueÚnew_messager    r9   Ú
message_idÚputÚdeleter6   )r   r>   r?   ZdelayrB   Znew_msgr   r   r   Úrun~   s    
zTask.runc             C   s^   t j d| j|f ¡ t  d|¡}| | j¡}| |¡}|j| _|  	¡  t j d| j ¡ d S )Nz"Task[%s] - starting with queue: %sÚsqszTask[%s] - start successful)
r%   r&   r'   r(   ÚlookuprC   r    r9   rD   rE   )r   r   rB   r>   r   r   r   r      s    
z
Task.start)N)r#   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r   r   r1   r   r)   r   r<   r=   rD   Úclassmethodr   r   r+   rA   rG   r   Ú__classcell__r   r   )r"   r   r   #   s   
"
r   c               @   s   e Zd Zdd„ Zddd„ZdS )Ú
TaskPollerc             C   s   t  ¡ | _| j |¡| _d S )N)r%   Zconnect_sqsrH   rI   rB   )r   r   r   r   r   r   š   s    
zTaskPoller.__init__r#   c             C   s‚   x|| j  |¡}|rpt | ¡ ¡}|rz|jr6|j|jkr\tj 	d|j
|jf ¡ | ||¡ qztj 	d|j
 ¡ qt |¡ qW d S )NzTask[%s] - read message %sz-Task[%s] - found extraneous message, ignoring)rB   Úreadr   Z	get_by_idZget_bodyrD   r    r%   r&   r'   r(   rG   r7   r8   )r   Úwaitr?   Úmr   r   r   r   r5   ž   s    zTaskPoller.pollN)r#   r#   )rJ   rK   rL   r   r5   r   r   r   r   rP   ˜   s   rP   )r%   Zboto.sdb.db.propertyr   r   r   Zboto.sdb.db.modelr   r   r2   r7   Zboto.compatr   r   r   ÚobjectrP   r   r   r   r   Ú<module>   s   u