B
    9‹dÞ  ã               @   sŽ   d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZmZ dd	lmZ d
dlmZ G dd„ deeƒZdS )zgA MultiTerminalManager for use in the notebook webserver
- raises HTTPErrors
- creates REST API models
é    N)Ú	timedelta)ÚutcnowÚ	isoformat)ÚNamedTermManager)Úweb)ÚIOLoopÚPeriodicCallback)ÚIntegerÚvalidate)ÚLoggingConfigurableé   )Ú TERMINAL_CURRENTLY_RUNNING_TOTALc                   s®   e Zd ZdZdZdZeddddZdZeedd	dZ	‡ fd
d„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd$‡ fdd„	Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Z‡  ZS )%ÚTerminalManagerz  NFr   Tz€Timeout (in seconds) in which a terminal has been inactive and ready to be culled.
        Values of 0 or lower disable culling.)ÚconfigÚhelpi,  z_The interval (in seconds) on which to check for terminals exceeding the inactive timeout value.c                s   t ƒ j||Ž d S )N)ÚsuperÚ__init__)ÚselfÚargsÚkwargs)Ú	__class__© ún/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/notebook/terminal/terminalmanager.pyr   )   s    zTerminalManager.__init__c             C   s   |   ¡ \}}|  ||¡S )zCreate a new terminal.)Znew_named_terminalÚ_finish_create)r   ÚnameÚtermr   r   r   Úcreate,   s    zTerminalManager.createc             C   s4   || j krt dd|› d¡‚|  |¡}|  ||¡S )zCreate a new terminal.i™  zA terminal with name 'z' already exists.)Ú	terminalsr   Ú	HTTPErrorZget_terminalr   )r   r   r   r   r   r   Úcreate_with_name1   s    

z TerminalManager.create_with_namec             C   s&   t ƒ |_|  |¡}t ¡  |  ¡  |S )N)r   Úlast_activityÚget_terminal_modelr   ÚincÚ_initialize_culler)r   r   r   Úmodelr   r   r   r   8   s
    
zTerminalManager._finish_createc             C   s   |   |¡}|S )zGet terminal 'name'.)r!   )r   r   r$   r   r   r   ÚgetD   s    
zTerminalManager.getc                s&   ‡ fdd„ˆ j D ƒ}t t|ƒ¡ |S )z$Get a list of all running terminals.c                s   g | ]}ˆ   |¡‘qS r   )r!   )Ú.0r   )r   r   r   ú
<listcomp>K   s    z(TerminalManager.list.<locals>.<listcomp>)r   r   ÚsetÚlen)r   Úmodelsr   )r   r   ÚlistI   s    
zTerminalManager.listc             ƒ   s,   |   |¡ tƒ j||dI dH  t ¡  dS )zTerminate terminal 'name'.)ÚforceN)Ú_check_terminalr   Ú	terminater   Údec)r   r   r,   )r   r   r   r.   S   s    
zTerminalManager.terminatec             Ã   s6   dd„ | j D ƒ}x |D ]}| j|ddI dH  qW dS )zTerminate all terminals.c             S   s   g | ]}|‘qS r   r   )r&   r   r   r   r   r'   ^   s    z1TerminalManager.terminate_all.<locals>.<listcomp>T)r,   N)r   r.   )r   Ztermsr   r   r   r   Úterminate_all\   s    
zTerminalManager.terminate_allc             C   s(   |   |¡ | j| }|t|jƒdœ}|S )zuReturn a JSON-safe dict representing a terminal.
        For use in representing terminals in the JSON APIs.
        )r   r    )r-   r   r   r    )r   r   r   r$   r   r   r   r!   b   s
    

z"TerminalManager.get_terminal_modelc             C   s    || j krt dd|› ¡‚dS )z9Check a that terminal 'name' exists and raise 404 if not.i”  zTerminal not found: N)r   r   r   )r   r   r   r   r   r-   n   s    
zTerminalManager._check_terminalc             C   s„   | j sz| jdkrz| jdkrzt ¡ }| jdkrH| j d| j| j¡ | j| _t	| j
d| j ƒ| _| j d| j| j¡ | j ¡  d| _ dS )z‡Start culler if 'cull_inactive_timeout' is greater than zero.
        Regardless of that value, set flag that we've been here.
        r   NzKInvalid value for 'cull_interval' detected (%s) - using default value (%s).iè  zICulling terminals with inactivity > %s seconds at %s second intervals ...T)Ú_initialized_cullerÚcull_inactive_timeoutÚ_culler_callbackr   ÚcurrentÚcull_intervalÚlogÚwarningÚcull_interval_defaultr   Ú_cull_terminalsÚinfoÚstart)r   Úloopr   r   r   r#   s   s    


z"TerminalManager._initialize_cullerc             Ã   s|   | j  d| j| j¡ xbt| jƒD ]T}y|  |¡I d H  W q  tk
rr } z| j  d|› d|› ¡ W d d }~X Y q X q W d S )NzCPolling every %s seconds for terminals inactive for > %s seconds...zPThe following exception was encountered while checking the activity of terminal z: )	r6   Údebugr5   r2   r+   r   Ú_cull_inactive_terminalÚ	ExceptionÚ	exception)r   r   Úer   r   r   r9   †   s    zTerminalManager._cull_terminalsc             Ã   s˜   y| j | }W n tk
r"   d S X | j d||j¡ t|dƒr”tƒ }||j }|t| jdk}|r”t	| 
¡ ƒ}| j d||¡ | j|ddI d H  d S )Nzname=%s, last_activity=%sr    )Úsecondsz6Culling terminal '%s' due to %s seconds of inactivity.T)r,   )r   ÚKeyErrorr6   r=   r    Úhasattrr   r   r2   ÚintÚtotal_secondsr7   r.   )r   r   r   Zdt_nowZdt_inactiveZis_timeZ
inactivityr   r   r   r>   ’   s    

z'TerminalManager._cull_inactive_terminal)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r1   r	   r2   r8   r5   r   r   r   r   r%   r+   r.   r0   r!   r-   r#   r9   r>   Ú__classcell__r   r   )r   r   r      s*   
	r   )rJ   ÚwarningsÚdatetimer   Znotebook._tzr   r   Z	terminador   Útornador   Ztornado.ioloopr   r   Z	traitletsr	   r
   Ztraitlets.configr   Zprometheus.metricsr   r   r   r   r   r   Ú<module>   s   