Exemplo n.º 1
0
    def _list_running_servers(runtime_dir: str = None) -> Generator:
        """Iterate over the server info files of running notebook servers.

        Given a runtime directory, find nbserver-* files in the security
        directory, and yield dicts of their information, each one pertaining to
        a currently running notebook server instance.

        Copied from notebook.notebookapp.list_running_servers() (version 5.7.8)
        since the highest version compatible with Python 3.5 (version 5.6.0)
        has a bug.
        """
        if runtime_dir is None:
            runtime_dir = jupyter_runtime_dir()

        # The runtime dir might not exist
        if not os.path.isdir(runtime_dir):
            return

        for file_name in os.listdir(runtime_dir):
            if re.match('nbserver-(.+).json', file_name):
                with io.open(os.path.join(runtime_dir, file_name),
                             encoding='utf-8') as f:
                    info = json.load(f)

                # Simple check whether that process is really still running
                # Also remove leftover files from IPython 2.x without a pid
                # field
                if ('pid' in info) and check_pid(info['pid']):
                    yield info
                else:
                    # If the process has died, try to delete its info file
                    try:
                        os.unlink(os.path.join(runtime_dir, file_name))
                    except OSError:
                        pass  # TODO: This should warn or log or something
Exemplo n.º 2
0
def _list_running_servers_jl3():
    import io
    import json
    from notebook.utils import check_pid
    from jupyter_core.paths import jupyter_runtime_dir
    import os.path
    import re

    runtime_dir = jupyter_runtime_dir()

    if not os.path.isdir(runtime_dir):
        return

    for file_name in os.listdir(runtime_dir):
        # here is the fix:
        if re.match('nbserver-(.+).json', file_name) or re.match(
                'jpserver-(.+).json', file_name):
            with io.open(os.path.join(runtime_dir, file_name),
                         encoding='utf-8') as f:
                info = json.load(f)

            if ('pid' in info) and check_pid(info['pid']):
                yield info
            else:
                try:
                    os.unlink(os.path.join(runtime_dir, file_name))
                except OSError:
                    pass
Exemplo n.º 3
0
    def check_alive(self):
        if not check_pid(self.pid):
            return False

        try:
            requests.head(self.url)
            return True
        except requests.ConnectionError:
            return False
Exemplo n.º 4
0
def list_running_servers_v2(runtime_dir=None):
    """Iterate over the server info files of running notebook servers.
    
    Given a runtime directory, find nbserver-* files in the security directory,
    and yield dicts of their information, each one pertaining to
    a currently running notebook server instance.
    """

    if runtime_dir is None:
        runtime_dir = jupyter_runtime_dir()

    # The runtime dir might not exist
    if not os.path.isdir(runtime_dir):
        return

    for file_name in os.listdir(runtime_dir):
        if re.match('nbserver-(.+).json', file_name):
            with io.open(os.path.join(runtime_dir, file_name),
                         encoding='utf-8') as f:
                info = json.load(f)

            # Simple check whether that process is really still running
            # Also remove leftover files from IPython 2.x without a pid field
            if ('pid' in info) and check_pid(info['pid']):
                #yield info
                pass
            else:
                # If the process has died, try to delete its info file
                try:
                    os.unlink(os.path.join(runtime_dir, file_name))
                except OSError:
                    pass  # TODO: This should warn or log or something

                try:
                    os.unlink(
                        os.path.join(runtime_dir, file_name)[:-5] +
                        "-open.html")
                except OSError:
                    pass  # TODO: This should warn or log or something

        # Delete all "*-open.html" files which hasn't pair json
        if re.match('nbserver-(.+)-open.html', file_name):
            if os.path.isfile(
                    os.path.join(runtime_dir, file_name)[:-10] + ".json"):
                pass
            else:
                try:
                    os.unlink(os.path.join(runtime_dir, file_name))
                except OSError:
                    pass  # TODO: This should warn or log or something
Exemplo n.º 5
0
    def start(self):
        """
        Executes the stop command.
        """
        try:
            status = JaffleStatus.load(self.status_file_path)
            print('Stopping Jaffle - PID: {}'.format(status.pid))
            os.kill(status.pid, signal.SIGTERM)

            for _ in range(50000):
                if check_pid(status.pid):
                    print("PID {} has finished".format(status.pid))
                    self._cleanup_runtime_dir(status)
                    sys.exit(0)
                time.sleep(0.1)

            os.kill(status.pid, signal.SIGKILL)

            for _ in range(50000):
                if check_pid(status.pid):
                    print("PID {} has finished".format(status.pid))
                    self._cleanup_runtime_dir(status)
                    sys.exit(0)
                time.sleep(0.1)

            print('Failed to stop Jaffle - PID: {}'.format(status.pid))

        except FileNotFoundError:
            print('Jaffle is not running - runtime_dir: {}'.format(
                self.runtime_dir),
                  file=sys.stderr)
            self.exit(1)

        except ProcessLookupError:
            print('Jaffle has already stopped')
            self._cleanup_runtime_dir(status)
            self.exit(1)
Exemplo n.º 6
0
 def wait(self, interval=0.01):
     # os.waitpid() only works with child processes, so we need a busy loop
     pid = self.pid
     while check_pid(pid):
         time.sleep(interval)