Example #1
0
    def inner_run(self, *args, **options):
        # ------------------------------------------------------------------------
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        # ------------------------------------------------------------------------
        autoreload.raise_last_exception()
        logger.info('"Performing system checks...\n\n',
                    extra={"emit_to_server": False})
        self.check(display_num_errors=True)

        # -----------------------------------------------------------
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        # -----------------------------------------------------------
        self.check_migrations()
        quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-C"
        serverStartDta = (
            f"Django version {self.get_version()}, using settings {settings.SETTINGS_MODULE}\n"
            f"Starting development async gRPC server at {self.address}\n"
            f"Quit the server with {quit_command}s.\n")

        # --------------------------------------------
        # ---  START ASYNC GRPC SERVER             ---
        # --------------------------------------------
        logger.info(serverStartDta, extra={"emit_to_server": False})
        asyncio.run(self._serve())
Example #2
0
    def _serve(self, max_workers, port, *args, **kwargs):
        from django.conf import settings

        config = getattr(settings, "GRPCSERVER", dict())

        autoreload.raise_last_exception()
        self.stdout.write("Starting server at %s" % datetime.datetime.now())

        if not config.get("authentication"):
            self.stdout.write("Starting basic grpcserver")
            server = create_server(max_workers, port)
        elif config.get("authentication") == "ssl":
            self.stdout.write("Starting ssl grpcserver")
            server = create_ssl_server(max_workers, port)
        server.start()

        self.stdout.write("Server is listening port %s" % port)

        if kwargs["list_handlers"] is True:
            self.stdout.write("Registered handlers:")
            for handler in extract_handlers(server):
                self.stdout.write("* %s" % handler)

        # since server.start() will not block,
        # a sleep-loop is added to keep alive
        try:
            while True:
                time.sleep(86400)
        except KeyboardInterrupt:
            server.stop(0)
            sys.exit(0)
Example #3
0
def restart_celery(*args, **kwargs):
    print("Restarting celery...")
    autoreload.raise_last_exception()
    kill_worker_cmd = "ps aux | grep bin/celery | awk '{print $2}' | xargs kill -9"
    subprocess.call(kill_worker_cmd, shell=True)
    start_worker_cmd = "celery -A conf worker -l info"
    subprocess.call(shlex.split(start_worker_cmd))
Example #4
0
    def run(self, *args, **options):
        duration = options.get('duration', 0)
        sleep = options.get('sleep', 5.0)
        queue = options.get('queue', None)
        log_std = options.get('log_std', False)
        is_dev = options.get('dev', False)
        sig_manager = self.sig_manager

        if is_dev:
            # raise last Exception is exist
            autoreload.raise_last_exception()

        if log_std:
            _configure_log_std()

        autodiscover()

        start_time = time.time()

        while (duration <= 0) or (time.time() - start_time) <= duration:
            if sig_manager.kill_now:
                # shutting down gracefully
                break

            if not self._tasks.run_next_task(queue):
                # there were no tasks in the queue, let's recover.
                close_connection()
                #logger.debug('waiting for tasks')
                time.sleep(sleep)
            else:
                # there were some tasks to process, let's check if there is more work to do after a little break.
                time.sleep(
                    random.uniform(sig_manager.time_to_wait[0],
                                   sig_manager.time_to_wait[1]))
Example #5
0
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        threading = options["use_threading"]
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get("shutdown_message", "")
        quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-C"

        if not options["skip_checks"]:
            self.stdout.write("Performing system checks...\n\n")
            self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        now = datetime.now().strftime("%B %d, %Y - %X")
        self.stdout.write(now)
        self.stdout.write(
            ("Django version %(version)s, using settings %(settings)r\n"
             "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
             "Quit the server with %(quit_command)s.") % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "protocol": self.protocol,
                 "addr": "[%s]" % self.addr if self._raw_ipv6 else self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })

        try:
            handler = self.get_handler(*args, **options)
            run(
                self.addr,
                int(self.port),
                handler,
                ipv6=self.use_ipv6,
                threading=threading,
                server_cls=self.server_cls,
            )
        except OSError as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #6
0
    def run(self, **options):
        autoreload.raise_last_exception()
        self.check(display_num_errors=True)
        self.stdout.write(self.style.SUCCESS('Server is running!'))

        run(addr="127.0.0.1",
            port=8000,
            wsgi_handler=get_internal_wsgi_application(),
            threading=True,
            server_cls=WSGIServer)
Example #7
0
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        try:
            if sio.async_mode == 'threading':
                super(Command, self).handle(*args, **options)
            elif sio.async_mode == 'eventlet':
                # deploy with eventlet
                import eventlet
                import eventlet.wsgi
                from apsma.wsgi import application
                eventlet.wsgi.server(eventlet.listen(('', int(self.port))),
                                     application)
            elif sio.async_mode == 'gevent':
                # deploy with gevent
                from gevent import pywsgi
                from apsma.wsgi import application
                try:
                    from geventwebsocket.handler import WebSocketHandler
                    websocket = True
                except ImportError:
                    websocket = False
                if websocket:
                    pywsgi.WSGIServer(
                        ('', 8000),
                        application,
                        handler_class=WebSocketHandler).serve_forever()
                else:
                    pywsgi.WSGIServer(('', 8000), application).serve_forever()
            elif sio.async_mode == 'gevent_uwsgi':
                print(
                    'Start the application through the uwsgi server. Example:')
                print('uwsgi --http :5000 --gevent 1000 --http-websockets '
                      '--master --wsgi-file django_example/wsgi.py --callable '
                      'application')
            else:
                print('Unknown async_mode: ' + sio.async_mode)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = force_text(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            sys.exit(0)
Example #8
0
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        threading = options['use_threading']
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        now = datetime.now().strftime('%B %d, %Y - %X')
        if six.PY2:
            now = now.decode(get_system_encoding())
        self.stdout.write(now)
        self.stdout.write((
            "Django version %(version)s, using settings %(settings)r\n"
            "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
            "Quit the server with %(quit_command)s.\n"
        ) % {
            "version": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "protocol": self.protocol,
            "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })

        try:
            handler = self.get_handler(*args, **options)
            run(self.addr, int(self.port), handler,
                ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = force_text(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #9
0
    def inner_run(self, *args, **options):  # pylint: disable=unused-argument
        autoreload.raise_last_exception()

        for pidfile in (options['celery_pidfile'], options['celerybeat_pidfile']):
            if os.path.exists(pidfile):
                with open(pidfile, 'r') as f:
                    pid = f.read().strip()
                with settings(warn_only=True), hide('commands'):  # pylint: disable=not-context-manager
                    local('kill -9 {}'.format(pid))
                    local('rm -f {}'.format(pidfile))
        call_command('startcelery', silent=True, pipe='', **options)
        self.stdout.write(self.style.SUCCESS("Successfully reloaded celery and celerybeat."))
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        threading = options.get('use_threading')
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        self.check_migrations()
        now = datetime.now().strftime('%B %d, %Y - %X')
        if six.PY2:
            now = now.decode(get_system_encoding())
        self.stdout.write(now)
        self.stdout.write(
            ("Django version %(version)s, using settings %(settings)r\n"
             "Starting development server at http://%(addr)s:%(port)s/\n"
             "Quit the server with %(quit_command)s.\n") % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })

        try:
            handler = self.get_handler(*args, **options)
            run(self.addr,
                int(self.port),
                handler,
                ipv6=self.use_ipv6,
                threading=threading)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = force_text(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #11
0
    def test_raises_exception(self):
        class MyException(Exception):
            pass

        # Create an exception
        try:
            raise MyException('Test Message')
        except MyException:
            exc_info = sys.exc_info()

        with mock.patch('django.utils.autoreload._exception', exc_info):
            with self.assertRaisesMessage(MyException, 'Test Message'):
                autoreload.raise_last_exception()
Example #12
0
    def test_raises_exception(self):
        class MyException(Exception):
            pass

        # Create an exception
        try:
            raise MyException('Test Message')
        except MyException:
            exc_info = sys.exc_info()

        with mock.patch('django.utils.autoreload._exception', exc_info):
            with self.assertRaisesMessage(MyException, 'Test Message'):
                autoreload.raise_last_exception()
Example #13
0
    def inner_run(self, *args: Any, **options: Any) -> None:
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        threading = options['use_threading']
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.check(display_num_errors=False)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        self.stdout.write(
            ("Django process (re)started. Quit the server with %(quit_command)s.\n"
             ) % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "protocol": self.protocol,
                 "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })

        try:
            handler = self.get_handler(*args, **options)
            run(self.addr,
                int(self.port),
                handler,
                ipv6=self.use_ipv6,
                threading=threading,
                server_cls=self.server_cls)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = str(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #14
0
    def test_raises_custom_exception(self):
        class MyException(Exception):
            def __init__(self, msg, extra_context):
                super().__init__(msg)
                self.extra_context = extra_context
        # Create an exception.
        try:
            raise MyException('Test Message', 'extra context')
        except MyException:
            exc_info = sys.exc_info()

        with mock.patch('django.utils.autoreload._exception', exc_info):
            with self.assertRaisesMessage(MyException, 'Test Message'):
                autoreload.raise_last_exception()
Example #15
0
    def test_raises_custom_exception(self):
        class MyException(Exception):
            def __init__(self, msg, extra_context):
                super().__init__(msg)
                self.extra_context = extra_context
        # Create an exception.
        try:
            raise MyException('Test Message', 'extra context')
        except MyException:
            exc_info = sys.exc_info()

        with mock.patch('django.utils.autoreload._exception', exc_info):
            with self.assertRaisesMessage(MyException, 'Test Message'):
                autoreload.raise_last_exception()
Example #16
0
    def test_raises_exception_with_context(self):
        try:
            raise Exception(2)
        except Exception as e:
            try:
                raise Exception(1) from e
            except Exception:
                exc_info = sys.exc_info()

        with mock.patch('django.utils.autoreload._exception', exc_info):
            with self.assertRaises(Exception) as cm:
                autoreload.raise_last_exception()
            self.assertEqual(cm.exception.args[0], 1)
            self.assertEqual(cm.exception.__cause__.args[0], 2)
Example #17
0
    def test_raises_exception_with_context(self):
        try:
            raise Exception(2)
        except Exception as e:
            try:
                raise Exception(1) from e
            except Exception:
                exc_info = sys.exc_info()

        with mock.patch('django.utils.autoreload._exception', exc_info):
            with self.assertRaises(Exception) as cm:
                autoreload.raise_last_exception()
            self.assertEqual(cm.exception.args[0], 1)
            self.assertEqual(cm.exception.__cause__.args[0], 2)
Example #18
0
    def inner_run(self, *args, **options):
        autoreload.raise_last_exception()

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write((
            "Starting file watcher...\n\n"
            "Quit with {}.\n"
        ).format(quit_command))

        try:
            self.watch()
        except KeyboardInterrupt:
            sys.exit(0)
Example #19
0
    def _serve(self, max_workers, port, *args, **kwargs):
        autoreload.raise_last_exception()
        self.stdout.write("Starting server at %s" % datetime.datetime.now())

        server = create_server(max_workers, port)
        server.start()

        self.stdout.write("Server is listening port %s" % port)

        if kwargs['list_handlers'] is True:
            self.stdout.write("Registered handlers:")
            for handler in extract_handlers(server):
                self.stdout.write("* %s" % handler)

        server.wait_for_termination()
Example #20
0
    def inner_run(self, *args, **options):
        # ------------------------------------------------------------------------
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        # ------------------------------------------------------------------------
        autoreload.raise_last_exception()
        logger.info('"Performing system checks...\n\n')
        self.check(display_num_errors=True)

        # -----------------------------------------------------------
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        # -----------------------------------------------------------
        self.check_migrations()
        quit_command = "CTRL-BREAK" if sys.platform == "win32" else "CONTROL-C"
        serverStartDta = (
            f"Django version {self.get_version()}, using settings {settings.SETTINGS_MODULE}\n"
            f"Starting development gRPC server at {self.address}\n"
            f"Quit the server with {quit_command}s.\n"
        )

        # --------------------------------------------
        # ---  START GRPC   SERVER                 ---
        # --------------------------------------------
        logger.info(serverStartDta)
        try:
            self._serve()
        except OSError as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            errorData = f"Error: {error_text}"
            logger.error(errorData)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)

        # ---------------------------------------
        # ----  EXIT OF GRPC SERVER           ---
        except KeyboardInterrupt:
            logger.warning("Exit gRPC Server")
            sys.exit(0)
Example #21
0
    def run_server(self, host, port):
        """Runs the RPC server locally
        :param host: str Host to use
        :param port: int port to use
        """
        autoreload.raise_last_exception()
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write(f"Django Manifold version {get_manifold_version()}\n"
                          f"Django version {self.get_version()}, "
                          f"using settings {settings.SETTINGS_MODULE}\n"
                          f"Starting development RPC server at {host}:{port}\n"
                          f"Quit the server with {quit_command}.\n")

        server = rpc.make_server()
        server.serve()
Example #22
0
    def run(self):
        autoreload.raise_last_exception()
        self.stdout.write(datetime.now().strftime('%B %d, %Y - %X'))
        self.stdout.write(
            ("Django version %(version)s, using settings %(settings)r\n"
             "Starting race bot (PID: %(pid)d)\n") % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "pid": os.getpid(),
             })

        try:
            bot = RaceBot(os.getpid())
            while True:
                bot.handle()
        except KeyboardInterrupt:
            sys.exit(0)
    def run(self, *args, **options):
        duration = options.get('duration', 0)
        sleep = options.get('sleep', 5.0)
        queue = options.get('queue', None)
        log_std = options.get('log_std', False)
        is_dev = options.get('dev', False)
        sig_manager = self.sig_manager

        if is_dev:
            # raise last Exception is exist
            autoreload.raise_last_exception()

        if log_std:
            _configure_log_std()

        autodiscover()

        start_time = time.time()

        while (duration <= 0) or (
                time.time() - start_time) <= duration or TaskCount.count > 0:
            is_past_duration = duration > 0 and (time.time() -
                                                 start_time) > duration

            if sig_manager.kill_now:
                # shutting down gracefully
                break

            # if is_past_duration, we don't want to run any more tasks. We're just waiting for what's running
            # to complete so we can exit.
            if is_past_duration:
                logger.debug(
                    'waiting for %s tasks to finish before exiting. %s',
                    TaskCount.count, TaskCount.task_guids)
                time.sleep(sleep)
            elif not self._tasks.run_next_task(queue):
                # there were no tasks in the queue, let's recover.
                close_connection()
                logger.debug('waiting for tasks')
                time.sleep(sleep)
            else:
                # there were some tasks to process, let's check if there is more work to do after a little break.
                time.sleep(
                    random.uniform(sig_manager.time_to_wait[0],
                                   sig_manager.time_to_wait[1]))
Example #24
0
    def run(self, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        self.stdout.write('Performing system checks...\n\n')
        self.check(display_num_errors=True)
        self.check_migrations()

        now = datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)

        try:
            self.stop_celery()
            self.start_celery(app=options['app_name'])
        except KeyboardInterrupt:
            self.stop_celery()
            sys.exit(0)
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        now = datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
        self.stdout.write((
            "Django version %(version)s, using settings %(settings)r\n"
            "Starting development gRPC server at %(address)s\n"
            "Quit the server with %(quit_command)s.\n"
        ) % {
            "version": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "address": self.address,
            "quit_command": quit_command,
        })
        try:
            self._serve()
        except OSError as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            sys.exit(0)
Example #26
0
    def _serve(self, max_workers, port, *args, **kwargs):
        autoreload.raise_last_exception()
        self.stdout.write("Starting server at %s" % datetime.datetime.now())

        server = create_server(max_workers, port)
        server.start()

        self.stdout.write("Server is listening port %s" % port)

        if kwargs['list_handlers'] is True:
            self.stdout.write("Registered handlers:")
            for handler in extract_handlers(server):
                self.stdout.write("* %s" % handler)

        # since server.start() will not block,
        # a sleep-loop is added to keep alive
        try:
            while True:
                time.sleep(86400)
        except KeyboardInterrupt:
            server.stop(0)
            sys.exit(0)
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        import threading
        ct = threading.current_thread()
        print(
            '【django.core.management.commands.runserver.Command.inner_run】当前线程:',
            ct.name, ct.ident)

        autoreload.raise_last_exception()

        threading = options['use_threading']
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        #self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        # 检查数据库版本迁移,需要在终端执行 python manage.py migrate 之类的命令
        self.check_migrations()
        print('【django.core.management.commands.runserver.Command.inner_run】',
              end='')
        print(
            f"Starting development server at {self.protocol}://{self.addr}:{self.port}"
        )
        """
        now = datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)
        self.stdout.write((
            "Django version %(version)s, using settings %(settings)r\n"
            "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
            "Quit the server with %(quit_command)s."
        ) % {
            "version": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "protocol": self.protocol,
            "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })
        """

        try:
            # 这里 self 是「命令处理对象」
            # 下面的 handle 是 django.core.handlers.wsgi.WSGIHandler 类的实例
            # 此实例就相当于 Flask 中的 app 应用对象
            handler = self.get_handler(*args, **options)
            # 这个 run 方法是核心,定义在 django.core.servers.basehttp 模块中
            # 在方法内部会创建服务器对象并启动监听
            # 参数 handler 是应用对象,server_cls 是服务器类,其实例就是携带 TCP 套接字的对象
            run(self.addr,
                int(self.port),
                handler,
                ipv6=self.use_ipv6,
                threading=threading,
                server_cls=self.server_cls)
        except OSError as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()
        max_workers = options.get('max_workers', 5)

        # Handle certificate
        ssl, server_kwargs = False, {}
        certificate_chain_pairs = options.get('certificate_chain_pairs', '')
        root_certificates = options.get('root_certificates', '')

        if certificate_chain_pairs:
            ssl = True
            certificate_chain_pairs = load_credential_from_args(
                certificate_chain_pairs)
            if root_certificates:
                root_certificates = load_credential_from_file(
                    root_certificates)
                server_kwargs['root_certificate'] = root_certificates
            server_kwargs['certificate_key'] = certificate_chain_pairs[0]
            server_kwargs['certificate'] = certificate_chain_pairs[1]

        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        now = datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)
        self.stdout.write(
            (
                "Django version %(version)s, using settings %(settings)r\n"  # noqa
                "Starting development server at %(protocol)s:%(addr)s:%(port)s\n"
                "Quit the server with %(quit_command)s.\n") % {
                    "version": self.get_version(),
                    "settings": settings.SETTINGS_MODULE,
                    "protocol": self.protocol,
                    "addr": '%s' % self.addr,
                    "port": self.port,
                    "quit_command": quit_command,
                })

        try:
            server = self.server_cls(max_workers=max_workers, ssl=ssl)
            with server.start(self.addr, self.port, **server_kwargs) as ser:
                ser.wait_for_termination()
        except OSError as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
from __future__ import unicode_literals
Example #30
0
 def test_no_exception(self):
     # Should raise no exception if _exception is None
     autoreload.raise_last_exception()
Example #31
0
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        threading = options['use_threading']
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n")
        #=======================================================================
        # Added by Adam Nieto
        # Gathering xss suppression file path
        user_current_directory = os.path.dirname(os.path.abspath(sys.argv[0]))
        suppression_file_path = os.path.join(user_current_directory,
                                             "xss_detector_suppressions.txt")
        rule_file_path = os.path.join(user_current_directory,
                                      "additional_xss_detector_rules.txt")
        # Checking if xss suppression file should be created
        if not self.check_suppression_file_exists(suppression_file_path):
            self.create_suppression_file(suppression_file_path)
        # Checking if xss rule file should be created
        if not self.check_rule_file_exists(rule_file_path):
            self.create_rule_file(rule_file_path)

        self.stdout.write("Performing xss vulnerability checks...\n\n")
        xss_warnings_are_silenced = options["silence_xss_warnings"]
        self.check_xss_vulnerabilities(xss_warnings_are_silenced,
                                       suppression_file_path, rule_file_path)
        #=======================================================================
        self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        now = datetime.now().strftime('%B %d, %Y - %X')
        if six.PY2:
            now = now.decode(get_system_encoding())
        self.stdout.write(now)
        self.stdout.write(
            ("Django version %(version)s, using settings %(settings)r\n"
             "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
             "Quit the server with %(quit_command)s.\n") % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "protocol": self.protocol,
                 "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })

        try:
            handler = self.get_handler(*args, **options)
            run(self.addr,
                int(self.port),
                handler,
                ipv6=self.use_ipv6,
                threading=threading,
                server_cls=self.server_cls)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = force_text(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #32
0
 def test_no_exception(self):
     # Should raise no exception if _exception is None
     autoreload.raise_last_exception()
Example #33
0
    def inner_run(self, *args, **options):
        from django.conf import settings
        from django.utils import translation

        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        threading = options.get('use_threading')
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n\n")
        self.validate(display_num_errors=True)
        try:
            self.check_migrations()
        except ImproperlyConfigured:
            pass
        now = datetime.now().strftime('%B %d, %Y - %X')
        if six.PY2:
            now = now.decode(get_system_encoding())
        self.stdout.write((
            "%(started_at)s\n"
            "Django version %(version)s, using settings %(settings)r\n"
            "Starting development server at http://%(addr)s:%(port)s/\n"
            "Quit the server with %(quit_command)s.\n"
        ) % {
            "started_at": now,
            "version": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })
        # django.core.management.base forces the locale to en-us. We should
        # set it up correctly for the first request (particularly important
        # in the "--noreload" case).
        translation.activate(settings.LANGUAGE_CODE)

        try:
            handler = self.get_handler(*args, **options)
            run(self.addr, int(self.port), handler,
                ipv6=self.use_ipv6, threading=threading)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned-to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = force_text(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #34
0
    def inner_run(self, *args, **options):  #  非常重要 的 方法
        """
        self = <django.contrib.staticfiles.management.commands.runserver.Command object at 0x0365E150>
        :param args: ()
        :param options:  为命令解析 结果
        :return:
        """
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()  # 作用不理解

        threading = options['use_threading']  # True
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')  # ""

        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C' # "CTRL-BREAK"

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)          # 不理解
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()                      # 不理解
        now = datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)
        self.stdout.write((
            "Django version %(version)s, using settings %(settings)r\n"
            "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
            "Quit the server with %(quit_command)s.\n"
        ) % {
            "version": self.get_version(),
            "settings": settings.SETTINGS_MODULE,
            "protocol": self.protocol,
            "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
            "port": self.port,
            "quit_command": quit_command,
        })

        try:
            # 关键代码
            handler = self.get_handler(*args, **options) #  返回了  静态文件 句柄  对象
            # handler == <django.contrib.staticfiles.handlers.StaticFilesHandler object at 0x000001D13A0C7308>
            """
            作用: 
                1. 加载 wsgi.py 中 的 application 
                2. 解析 静态 ulr, settings.STATIC_URL
                3. 加载  中间件  , settings.MIDDLEWARE
            """
            # 关键代码
            run(self.addr, int(self.port), handler,      #  开启 wsgi Web  服务
                ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
Example #35
0
    def inner_run(self, *args, **options):
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        autoreload.raise_last_exception()

        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        self.check_migrations()
        now = datetime.datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)
        self.stdout.write(
            ("Django version %(version)s, using settings %(settings)r\n"
             "Starting aidjango server at http://%(addr)s:%(port)s/\n"
             "Quit the server with %(quit_command)s.\n") % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })

        if options.get('use_reloader'):
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
        else:
            loop = asyncio.get_event_loop()
        app = self.get_handler(*args, **options)
        log = logging.getLogger('aiodjango.runserver')
        log.propagate = False
        log.setLevel(logging.INFO)
        stdout = logging.StreamHandler(stream=self.stdout)
        log.addHandler(stdout)
        handler = app.make_handler(access_log=log,
                                   access_log_format='%t "%r" %s %b %D')
        server = None
        try:
            server = loop.run_until_complete(
                loop.create_server(handler, self.addr, int(self.port)))
            loop.run_forever()
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = force_text(e)
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)
        finally:
            loop.run_until_complete(handler.finish_connections(1.0))
            if server is not None:
                server.close()
                loop.run_until_complete(server.wait_closed())
            loop.run_until_complete(app.finish())
        loop.close()
Example #36
0
    def inner_run(self, *args, **options):
        # 1 配置工作
        # If an exception was silenced in ManagementUtility.execute in order
        # to be raised in the child process, raise it now.
        MY('Running', '\n\t开启运行(child线程)')
        autoreload.raise_last_exception()

        # django 默认开启threading, 允许在开发服务器中使用多线程
        # 可以通过选项: --nothreading关闭
        threading = options['use_threading']
        # 'shutdown_message' is a stealth option.
        shutdown_message = options.get('shutdown_message', '')
        quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'

        self.stdout.write("Performing system checks...\n\n")
        self.check(display_num_errors=True)
        # Need to check migrations here, so can't use the
        # requires_migrations_check attribute.
        self.check_migrations()
        now = datetime.now().strftime('%B %d, %Y - %X')
        self.stdout.write(now)
        self.stdout.write(
            ("Django version %(version)s, using settings %(settings)r\n"
             "Starting development server at %(protocol)s://%(addr)s:%(port)s/\n"
             "Quit the server with %(quit_command)s.\n") % {
                 "version": self.get_version(),
                 "settings": settings.SETTINGS_MODULE,
                 "protocol": self.protocol,
                 "addr": '[%s]' % self.addr if self._raw_ipv6 else self.addr,
                 "port": self.port,
                 "quit_command": quit_command,
             })

        # 2 主要处理逻辑
        try:
            # 2.1 获取handler, 实际获取get_internal_wsgi_application 返回application
            #       类型: WSGIHandler(实现WSGI 协议的对象)
            #       功能: 处理 Request/Resonse, 见WSGIHandler类(可调用)
            #       调用: 其中handler调用: handler(environ, start_response)
            handler = self.get_handler(*args, **options)
            # 2.2 runserver启动
            # 开始正式进入core.servers.basehttp.run
            # HTTP, 其中server_cls=WSGIServer使用wsgiref模块来实现HTTP
            # 功能, 类: wsgiref.simple_server.WSGIServer
            # 最终会通过handler来处理每一个请求
            run(self.addr,
                int(self.port),
                handler,
                ipv6=self.use_ipv6,
                threading=threading,
                server_cls=self.server_cls)
        except socket.error as e:
            # Use helpful error messages instead of ugly tracebacks.
            ERRORS = {
                errno.EACCES: "You don't have permission to access that port.",
                errno.EADDRINUSE: "That port is already in use.",
                errno.EADDRNOTAVAIL: "That IP address can't be assigned to.",
            }
            try:
                error_text = ERRORS[e.errno]
            except KeyError:
                error_text = e
            self.stderr.write("Error: %s" % error_text)
            # Need to use an OS exit because sys.exit doesn't work in a thread
            os._exit(1)
        except KeyboardInterrupt:
            if shutdown_message:
                self.stdout.write(shutdown_message)
            sys.exit(0)