U
    W+d}                     @   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   4/tmp/pip-unpacked-wheel-dlxw5sjy/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   |   D ]}|| q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datetime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rdS d|j S nTt| j	| jj	kr|jdkrd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   maxsecondsdays)r!   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}|}|	 d krt jd||f  ||kr||7 }t jd| j|f  |r|
| td |d7 }qB| }||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sleepcommunicatewritegetvaluer    r,   
returncodelast_statuslast_output)r!   msgvtimeoutZlog_fpprocessZnsecsZcurrent_timeouttr   r   r   _runf   s0     


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)r1   r(   r)   r*   r+   rI   queuenew_messager"   r@   
message_idputdeleter<   )r!   rE   rF   delayrJ   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+   lookuprK   r"   r@   rL   rM   )r!   r   rJ   rE   r   r   r   r      s    
z
Task.start)N)r&   )__name__
__module____qualname____doc__r   r+   r   r   r7   r   r,   r   rC   rD   rL   classmethodr   r   r1   rI   rP   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 r   )r(   Zconnect_sqsrQ   rR   rJ   )r!   r   r   r   r   r      s    
zTaskPoller.__init__r&   c                 C   s~   | j |}|rnt| }|rx|jr4|j|jkrZtj	d|j
|jf  ||| qxtj	d|j
  q t| q d S )NzTask[%s] - read message %sz-Task[%s] - found extraneous message, ignoring)rJ   readr   Z	get_by_idget_bodyrL   r"   r(   r)   r*   r+   rP   r=   r>   )r!   waitrF   mr   r   r   r   r;      s    zTaskPoller.pollN)r&   r&   )rS   rT   rU   r   r;   r   r   r   r   rY      s   rY   )r(   Zboto.sdb.db.propertyr   r   r   Zboto.sdb.db.modelr   r   r8   r=   Zboto.compatr   r   r   objectrY   r   r   r   r   <module>   s   u