Maps
Maps

Reputation: 135

How can I stop a django server programatically

I want to perform some checks across a few environments being pointed to on startup of my django server to ensure consistency of the different environment configrations. For example, that they are all in dev configuration, or all in prod configuration. If this is not the case I would like to shut down the server.

My question is what is the best way to shut down the server and report the issue? At the moment my plan is to raise a RuntimeError with the appropriate exception message. But I was wondering if there is any problem with this approach, or if there is a more suitable approach which I can not find.

Researching the issue has unfortunately not yielded sufficient information.

Upvotes: 1

Views: 108

Answers (1)

markwalker_
markwalker_

Reputation: 12849

I assume you'll be running with apache, which can't, or perhaps more accurately shouldn't, be controlled by Django.

What you should aim to do use python logging & if you find an issue log a critical level message. You can then have a handler setup to use mail_admins for those logging messages; https://docs.djangoproject.com/en/2.0/topics/logging/#examples

There are several levels to logging;

  • Debug
  • Info
  • Warning
  • Error
  • Critical

If you set a level, it's the base level that will be required by the handler or logger etc. So if you set 'level': 'ERROR' it'll log error and critical messages.

If you've implemented logging, you can then do similar to the following in your code;

import logging

logger = logging.getLogger(__name__)

...

if error_found:
    logger.critical("Oh no, we've found an error")

try:
    ...
except RuntimeError:
    logger.error("We've got a problem", exc_info=True)

For some further reading on setting up logging have a read of this.

Upvotes: 1

Related Questions