Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: add execution_state filed on terminal #625

Closed
Wh1isper opened this issue Dec 2, 2021 · 4 comments
Closed

Proposal: add execution_state filed on terminal #625

Wh1isper opened this issue Dec 2, 2021 · 4 comments

Comments

@Wh1isper
Copy link
Contributor

Wh1isper commented Dec 2, 2021

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

@Wh1isper Wh1isper changed the title Suggest add execution_state in terminal Suggestion: add execution_state filed on terminal Dec 2, 2021
@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 2, 2021

and anothoer question, why we didn't move terminal into service and make its websocket route as /api/terminals/websocket/

@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 2, 2021

#626 meets my needs, but may not be a good general approach
Maybe we need to raise an issue with terminado?

@Wh1isper
Copy link
Contributor Author

Wh1isper commented Dec 5, 2021

#626 meets my needs, but may not be a good general approach Maybe we need to raise an issue with terminado?

Let me update the progress, in the development of terminal with status, I found that we need to extend the functionality of terminal_manager, finally made terminal_manager a configurable item and implemented a terminal_manager that uses logging of terminal returns to determine the status ( StatefulTerminalManager), which currently works in most linux and linux docker containers, hopefully someone will make it support more operating systems later.

The code is all here #626, feel free to make comments

@Wh1isper Wh1isper closed this as completed Dec 5, 2021
@Wh1isper Wh1isper reopened this Dec 5, 2021
@Wh1isper Wh1isper changed the title Suggestion: add execution_state filed on terminal Proposal: add execution_state filed on terminal Dec 5, 2021
@Wh1isper
Copy link
Contributor Author

Turn to #640
Thanks all !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant