jazzband / django-ddp

Django/PostgreSQL implementation of the Meteor server.
MIT License
167 stars 29 forks source link

use management command so that can run dddp server by ./manage.py dddp #42

Closed yjmade closed 8 years ago

yjmade commented 8 years ago

and also decrease the risk that django throw errors when initiating

tysonclugg commented 8 years ago

Thanks for the PR, great effort. :-)

I think there may be a subtle issue here that gevent monkey patching won't work cleanly because the threading module gets imported before being patched. This can be fixed by applying a patch to the project manage.py, but that means adding more steps to the getting started docs and hence more things that can go wrong. It does have an advantage that you don't have to "install" your project which I suspect is the fundamental reason you'd like this PR applied...

If be happy to merge this after the docs and other issues mentioned above are addressed. Thanks!

yjmade commented 8 years ago

the fundamental reason I did this is because I encounter some bugs when I try to start the dddp serve with the django-ddp-todos project. if I set the database engine as django.contrib.gis.db.backends.postgis

(django-ddp)yjmbp:django-ddp-todos yjmade$ dddp --settings=djme_todos.settings 0.0.0.0
Traceback (most recent call last):
  File "/Users/yjmade/Envs/django-ddp/bin/dddp", line 9, in <module>
    load_entry_point('django-ddp', 'console_scripts', 'dddp')()
  File "/Users/yjmade/Envs/django-ddp/src/django-ddp/dddp/main.py", line 349, in main
    verbosity=namespace.verbosity,
  File "/Users/yjmade/Envs/django-ddp/src/django-ddp/dddp/main.py", line 279, in serve
    launcher = DDPLauncher(debug=verbosity == 3, verbosity=verbosity)
  File "/Users/yjmade/Envs/django-ddp/src/django-ddp/dddp/main.py", line 104, in __init__
    close_old_connections()
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/db/__init__.py", line 63, in close_old_connections
    for conn in connections.all():
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/db/utils.py", line 227, in all
    return [self[alias] for alias in self]
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/db/utils.py", line 212, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/db/utils.py", line 116, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 7, in <module>
    from .operations import PostGISOperations
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/operations.py", line 18, in <module>
    from .models import PostGISGeometryColumns, PostGISSpatialRefSys
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/contrib/gis/db/backends/postgis/models.py", line 10, in <module>
    class PostGISGeometryColumns(models.Model):
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/db/models/base.py", line 94, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

if I set it back to django.db.backends.postgresql_psycopg2, then another error

Traceback (most recent call last):
  File "/Users/yjmade/Envs/django-ddp/bin/dddp", line 9, in <module>
    load_entry_point('django-ddp', 'console_scripts', 'dddp')()
  File "/Users/yjmade/Envs/django-ddp/src/django-ddp/dddp/main.py", line 349, in main
    verbosity=namespace.verbosity,
  File "/Users/yjmade/Envs/django-ddp/src/django-ddp/dddp/main.py", line 279, in serve
    launcher = DDPLauncher(debug=verbosity == 3, verbosity=verbosity)
  File "/Users/yjmade/Envs/django-ddp/src/django-ddp/dddp/main.py", line 119, in __init__
    self.wsgi_app = get_wsgi_application()
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup()
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/yjmade/Envs/django-ddp/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
    raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant
yjmade commented 8 years ago

found the problem, the django-ddp-todos project install django-ddp from git+https://github.com/cxhandley/django-ddp.git@develop#egg=django-ddp, as I reinstall django-ddp from pypi, it's fine now

yjmade commented 8 years ago

but I still don't like to start a django daemon from another entrance beside manage.py. for example, celery you can install the djcelery package and start it by ./manage.py celeryd.

tysonclugg commented 8 years ago

"I prefer something else" without any persuasive reasoning is not convincing me to accept this PR. I'm closing this until you've provided sound reasoning as to why this is necessary - until then it's just bike shedding.

In the early stages of Django DDP there was indeed a management command to start the server, but I removed it because of the monkey patching issues I mentioned in one of my other comments on this PR. If you can solve said issues without placing undue burden on developers or users (ie: without having to hack their project manage.py), and you can convince me that the project would be better off after the change, then I'll reconsider.

yjmade commented 8 years ago

@tysonclugg Base on current code, if start daemon by dddp command, the greenify function is not been execute at all. the system threading module is being import when importing the geventwebsocket module. Also, the invoke of greenify is put under if __name__ == '__main__', so when start from dddp command, that piece of code will be ignored.

yjmade commented 8 years ago

For now I just follow your advice to put the greenify in the manage.py of my project, no really other way and lack of time to keep pursuing this.