Skip to content

Proposal: add execution_state filed on terminal #625

@Wh1isper

Description

@Wh1isper

Problem

As I am working on a plugin to check if user is running code, so that user can running code as a background task without being killed by my modified jupyterhub
but I found terminal don't have execution_state filed, so I cant check terminal's state
Hope I can check both kernel and terminal,

Proposed Solution

I have tried add some process on on_message and write_message and TerminalManager's get_terminal_model

class TermSocket(WebSocketMixin, JupyterHandler, terminado.TermSocket):
    ... # some code 
    def on_message(self, message):
        super(TermSocket, self).on_message(message)
        self._update_activity()
        self._set_state_busy()

    def write_message(self, message, binary=False):
        super(TermSocket, self).write_message(message, binary=binary)
        self._update_activity()
        if message != '["stdout", "\\r\\n"]':
            self._set_state_idle()

    def _update_activity(self):
        self.application.settings["terminal_last_activity"] = utcnow()
        # terminal may not be around on deletion/cull
        if self.term_name in self.terminal_manager.terminals:
            self.terminal_manager.terminals[self.term_name].last_activity = utcnow()

    def _set_state_busy(self):
        if self.term_name in self.terminal_manager.terminals:
            self.terminal_manager.terminals[self.term_name].execution_state = 'busy'

    def _set_state_idle(self):
        if self.term_name in self.terminal_manager.terminals:
            self.terminal_manager.terminals[self.term_name].execution_state = 'idle'
class TerminalManager(LoggingConfigurable, terminado.NamedTermManager):
    ... # some code 
    def get_terminal_model(self, name):
        """Return a JSON-safe dict representing a terminal.
        For use in representing terminals in the JSON APIs.
        """
        self._check_terminal(name)
        term = self.terminals[name]
        model = {
            "name": name,
            "last_activity": isoformat(term.last_activity),
        }
        # added
        if hasattr(term, 'execution_state'):
            model['execution_state'] = term.execution_state

        return model

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions