Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run_gunicorn.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. from optparse import make_option
  6. import sys
  7. from django.core.management.base import BaseCommand, CommandError
  8. from gunicorn.app.djangoapp import DjangoApplicationCommand
  9. from gunicorn.config import make_settings
  10. from gunicorn import util
  11. # monkey patch django.
  12. # This patch make sure that we use real threads to get the ident which
  13. # is going to happen if we are using gevent or eventlet.
  14. try:
  15. from django.db.backends import BaseDatabaseWrapper, DatabaseError
  16. if "validate_thread_sharing" in BaseDatabaseWrapper.__dict__:
  17. import thread
  18. _get_ident = thread.get_ident
  19. __old__init__ = BaseDatabaseWrapper.__init__
  20. def _init(self, *args, **kwargs):
  21. __old__init__(self, *args, **kwargs)
  22. self._thread_ident = _get_ident()
  23. def _validate_thread_sharing(self):
  24. if (not self.allow_thread_sharing
  25. and self._thread_ident != _get_ident()):
  26. raise DatabaseError("DatabaseWrapper objects created in a "
  27. "thread can only be used in that same thread. The object "
  28. "with alias '%s' was created in thread id %s and this is "
  29. "thread id %s."
  30. % (self.alias, self._thread_ident, _get_ident()))
  31. BaseDatabaseWrapper.__init__ = _init
  32. BaseDatabaseWrapper.validate_thread_sharing = _validate_thread_sharing
  33. except ImportError:
  34. pass
  35. def make_options():
  36. opts = [
  37. make_option('--adminmedia', dest='admin_media_path', default='',
  38. help='Specifies the directory from which to serve admin media.')
  39. ]
  40. g_settings = make_settings(ignore=("version"))
  41. keys = g_settings.keys()
  42. for k in keys:
  43. if k in ('pythonpath', 'django_settings',):
  44. continue
  45. setting = g_settings[k]
  46. if not setting.cli:
  47. continue
  48. args = tuple(setting.cli)
  49. kwargs = {
  50. "dest": setting.name,
  51. "metavar": setting.meta or None,
  52. "action": setting.action or "store",
  53. "type": setting.type or "string",
  54. "default": None,
  55. "help": "%s [%s]" % (setting.short, setting.default)
  56. }
  57. if kwargs["action"] != "store":
  58. kwargs.pop("type")
  59. opts.append(make_option(*args, **kwargs))
  60. return tuple(opts)
  61. GUNICORN_OPTIONS = make_options()
  62. class Command(BaseCommand):
  63. option_list = BaseCommand.option_list + GUNICORN_OPTIONS
  64. help = "Starts a fully-functional Web server using gunicorn."
  65. args = '[optional port number, or ipaddr:port or unix:/path/to/sockfile]'
  66. # Validation is called explicitly each time the server is reloaded.
  67. requires_model_validation = False
  68. def handle(self, addrport=None, *args, **options):
  69. # deprecation warning to announce future deletion in R21
  70. util.warn("""This command is deprecated.
  71. You should now run your application with the WSGI interface
  72. installed with your project. Ex.:
  73. gunicorn myproject.wsgi:application
  74. See https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/gunicorn/
  75. for more info.""")
  76. if args:
  77. raise CommandError('Usage is run_gunicorn %s' % self.args)
  78. if addrport:
  79. sys.argv = sys.argv[:-1]
  80. options['bind'] = addrport
  81. admin_media_path = options.pop('admin_media_path', '')
  82. DjangoApplicationCommand(options, admin_media_path).run()