Ejemplo n.º 1
0
def ShutdownSiblingInstance(db_dir):

    port_found = False

    ports = HydrusData.GetSiblingProcessPorts(db_dir, 'server')

    if ports is None:

        raise HydrusExceptions.PermissionException(
            'Could not figure out the existing server\'s ports, so could not shut it down!'
        )

    for port in ports:

        try:

            connection = HydrusNetworking.GetLocalConnection(port, https=True)

            connection.request('GET', '/')

            response = connection.getresponse()

            response.read()

            server_name = response.getheader('Server')

        except:

            text = 'Could not contact existing server\'s port ' + str(
                port) + '!'
            text += os.linesep
            text += traceback.format_exc()

            raise HydrusExceptions.PermissionException(text)

        if 'server administration' in server_name:

            port_found = True

            HydrusData.Print(u'Sending shut down instruction\u2026')

            connection.request('POST', '/shutdown')

            response = connection.getresponse()

            result = response.read()

            if response.status != 200:

                text = 'When told to shut down, the existing server gave an error!'
                text += os.linesep
                text += result

                raise HydrusExceptions.PermissionException(text)

            time_waited = 0

            while HydrusData.IsAlreadyRunning(db_dir, 'server'):

                time.sleep(1)

                time_waited += 1

                if time_waited > 20:

                    raise HydrusExceptions.PermissionException(
                        'Attempted to shut the existing server down, but it took too long!'
                    )

            break

    if not port_found:

        raise HydrusExceptions.PermissionException(
            'The existing server did not have an administration service!')

    HydrusData.Print('The existing server is shut down!')