a
    ø£Be’  ã                   @   sˆ   d 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 ddlmZmZ ddlmZ dd	lmZ ejZG d
d„ dee	ƒZdS )zgA MultiTerminalManager for use in the notebook webserver
- raises HTTPErrors
- creates REST API models
é    )Ú	timedelta)Ú	isoformatÚutcnow)Úmetrics)ÚNamedTermManager)Úweb)ÚIOLoopÚPeriodicCallback)ÚInteger)ÚLoggingConfigurablec                       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	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dd„ Z‡  ZS )!ÚTerminalManagerz8A MultiTerminalManager for use in the notebook webserverNFr   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                 K   s:   | j f i |¤Ž\}}tƒ |_|  |¡}t ¡  |  ¡  |S )zCreate a new terminal.)Znew_named_terminalr   Úlast_activityÚget_terminal_modelÚRUNNING_TOTALÚincÚ_initialize_culler)ÚselfÚkwargsÚnameÚtermÚmodel© r   úk/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server_terminals/terminalmanager.pyÚcreate,   s    
zTerminalManager.createc                 C   s   |   |¡}|S )zGet terminal 'name'.©r   )r   r   r   r   r   r   Úget:   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>A   ó    z(TerminalManager.list.<locals>.<listcomp>)Ú	terminalsr   ÚsetÚlen)r   Úmodelsr   r   r   Úlist?   s    zTerminalManager.listc                 ƒ   s,   |   |¡ tƒ j||d�I dH  t ¡  dS )zTerminate terminal 'name'.©ÚforceN)Ú_check_terminalÚsuperÚ	terminater   Údec)r   r   r(   ©Ú	__class__r   r   r+   G   s    
zTerminalManager.terminatec                 Ã   s,   t | jƒ}|D ]}| j|dd�I dH  qdS )zTerminate all terminals.Tr'   N)r&   r"   r+   )r   Ztermsr   r   r   r   Úterminate_allP   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   V   s    

þz"TerminalManager.get_terminal_modelc                 C   s   || j vrt dd| ¡‚dS )z9Check a that terminal 'name' exists and raise 404 if not.i”  zTerminal not found: %sN)r"   r   Ú	HTTPError)r   r   r   r   r   r)   b   s    
zTerminalManager._check_terminalc                 C   s„   | j sz| jdkrz| jdu 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   Ú_r   r   r   r   g   s(    

ýÿý
z"TerminalManager._initialize_cullerc                 Ã   sv   | j  d| j| j¡ t| jƒD ]R}z|  |¡I d H  W q tyn } z | j  d 	||¡¡ W Y d }~qd }~0 0 qd S )NzCPolling every %s seconds for terminals inactive for > %s seconds...zVThe following exception was encountered while checking the activity of terminal {}: {})
r6   Údebugr5   r2   r&   r"   Ú_cull_inactive_terminalÚ	ExceptionÚ	exceptionÚformat)r   r   Úer   r   r   r9   �   s    ýÿÿzTerminalManager._cull_terminalsc                 Ã   s˜   z| j | }W n ty"   Y d S 0 | 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.Tr'   )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_terminalc                 C   s   t ƒ |_dS )zThe pre-pty read hook.N)r   r   )r   Zptywclientsr   r   r   Úpre_pty_read_hook¥   s    z!TerminalManager.pre_pty_read_hook)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r1   r
   r2   r8   r5   r   r   r&   r+   r/   r   r)   r   r9   r>   rH   Ú__classcell__r   r   r-   r   r      s2   ýý		r   N)rL   Údatetimer   Zjupyter_server._tzr   r   Zjupyter_server.prometheusr   Zterminado.managementr   Útornador   Ztornado.ioloopr   r	   Z	traitletsr
   Ztraitlets.configr   Z TERMINAL_CURRENTLY_RUNNING_TOTALr   r   r   r   r   r   Ú<module>   s   