mirror of
https://github.com/jazzband/django-ddp.git
synced 2026-03-16 22:40:24 +00:00
Use logging rather than print statements, find WSGI_APPLICATION rather than using donor project default.
This commit is contained in:
parent
25a9fe6ef3
commit
29b9acf8e4
3 changed files with 40 additions and 26 deletions
|
|
@ -10,6 +10,7 @@ import socket
|
|||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import connection, close_old_connections
|
||||
from django.utils.module_loading import import_string
|
||||
import ejson
|
||||
import gevent
|
||||
import gevent.monkey
|
||||
|
|
@ -69,16 +70,6 @@ def ddpp_sockjs_info(environ, start_response):
|
|||
]))
|
||||
|
||||
|
||||
from meerqat import wsgi
|
||||
resource = geventwebsocket.Resource({
|
||||
r'/websocket': DDPWebSocketApplication,
|
||||
r'^/sockjs/\d+/\w+/websocket$': DDPWebSocketApplication,
|
||||
r'^/sockjs/\d+/\w+/xhr$': ddpp_sockjs_xhr,
|
||||
r'^/sockjs/info$': ddpp_sockjs_info,
|
||||
r'^/(?!(websocket|sockjs)/)': wsgi.application,
|
||||
})
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
|
||||
"""Command to run DDP web service."""
|
||||
|
|
@ -105,10 +96,30 @@ class Command(BaseCommand):
|
|||
gevent.monkey.patch_all()
|
||||
psycogreen.gevent.patch_psycopg()
|
||||
|
||||
debug = int(options['verbosity']) > 1
|
||||
|
||||
# setup PostgresGreenlet to multiplex DB calls
|
||||
postgres = PostgresGreenlet(connection)
|
||||
postgres = PostgresGreenlet(connection, debug=debug)
|
||||
DDPWebSocketApplication.pgworker = postgres
|
||||
|
||||
# use settings.WSGI_APPLICATION or fallback to default Django WSGI app
|
||||
from django.conf import settings
|
||||
if hasattr(settings, 'WSGI_APPLICATION'):
|
||||
wsgi_name = settings.WSGI_APPLICATION
|
||||
wsgi_app = import_string(wsgi_name)
|
||||
else:
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
wsgi_app = get_wsgi_application()
|
||||
wsgi_name = str(wsgi_app.__class__)
|
||||
|
||||
resource = geventwebsocket.Resource({
|
||||
r'/websocket': DDPWebSocketApplication,
|
||||
r'^/sockjs/\d+/\w+/websocket$': DDPWebSocketApplication,
|
||||
r'^/sockjs/\d+/\w+/xhr$': ddpp_sockjs_xhr,
|
||||
r'^/sockjs/info$': ddpp_sockjs_info,
|
||||
r'^/(?!(websocket|sockjs)/)': wsgi_app,
|
||||
})
|
||||
|
||||
# setup WebSocketServer to dispatch web requests
|
||||
host = options['host']
|
||||
port = options['port']
|
||||
|
|
@ -119,7 +130,7 @@ class Command(BaseCommand):
|
|||
webserver = geventwebsocket.WebSocketServer(
|
||||
(host, port),
|
||||
resource,
|
||||
debug=int(options['verbosity']) > 1,
|
||||
debug=debug,
|
||||
)
|
||||
|
||||
def killall(*args, **kwargs):
|
||||
|
|
@ -135,7 +146,8 @@ class Command(BaseCommand):
|
|||
postgres.start()
|
||||
print('=> Started PostgresGreenlet.')
|
||||
web = gevent.spawn(webserver.serve_forever)
|
||||
print('=> Started your app.')
|
||||
print('=> Started DDPWebSocketApplication.')
|
||||
print('=> Started your app (%s).' % wsgi_name)
|
||||
print('')
|
||||
print('=> App running at: http://%s:%d/' % (host, port))
|
||||
gevent.joinall([postgres, web])
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import gevent
|
|||
import gevent.queue
|
||||
import gevent.select
|
||||
import psycopg2 # green
|
||||
from geventwebsocket.logging import create_logger
|
||||
import psycopg2.extras
|
||||
|
||||
|
||||
|
|
@ -20,10 +21,11 @@ class PostgresGreenlet(gevent.Greenlet):
|
|||
|
||||
"""Greenlet for multiplexing database operations."""
|
||||
|
||||
def __init__(self, conn):
|
||||
def __init__(self, conn, debug=False):
|
||||
"""Prepare async connection."""
|
||||
# greenify!
|
||||
super(PostgresGreenlet, self).__init__()
|
||||
self.logger = create_logger(__name__, debug=debug)
|
||||
|
||||
# queues for processing incoming sub/unsub requests and processing
|
||||
self.subs = gevent.queue.Queue()
|
||||
|
|
@ -78,7 +80,7 @@ class PostgresGreenlet(gevent.Greenlet):
|
|||
while self.conn.notifies:
|
||||
notify = self.conn.notifies.pop()
|
||||
name = notify.channel
|
||||
print("Got NOTIFY:", notify.pid, name, notify.payload)
|
||||
self.logger.info("Got NOTIFY (pid=%d, name=%r, payload=%r)", notify.pid, name, notify.payload)
|
||||
try:
|
||||
self._sub_lock.acquire()
|
||||
subs = self.all_subs[name]
|
||||
|
|
@ -93,7 +95,7 @@ class PostgresGreenlet(gevent.Greenlet):
|
|||
elif state == psycopg2.extensions.POLL_READ:
|
||||
gevent.select.select([self.conn.fileno()], [], [])
|
||||
else:
|
||||
print('POLL_ERR: %s' % state)
|
||||
self.logger.warn('POLL_ERR: %s' % state)
|
||||
|
||||
def process_subs(self):
|
||||
"""Subtask to process `sub` requests from `self.subs` queue."""
|
||||
|
|
@ -103,7 +105,7 @@ class PostgresGreenlet(gevent.Greenlet):
|
|||
self._sub_lock.acquire()
|
||||
subs = self.all_subs[name]
|
||||
if len(subs) == 0:
|
||||
print('LISTEN "%s";' % name)
|
||||
self.logger.debug('LISTEN "%s";', name)
|
||||
self.poll()
|
||||
self.cur.execute('LISTEN "%s";' % name)
|
||||
self.poll()
|
||||
|
|
@ -120,7 +122,7 @@ class PostgresGreenlet(gevent.Greenlet):
|
|||
for name, subs in self.all_subs.items():
|
||||
subs.pop((obj, id_), None)
|
||||
if len(subs) == 0:
|
||||
print('UNLISTEN "%s";' % name)
|
||||
self.logger.info('UNLISTEN "%s";', name)
|
||||
self.cur.execute('UNLISTEN "%s";' % name)
|
||||
self.poll()
|
||||
del self.all_subs[name]
|
||||
|
|
|
|||
|
|
@ -80,6 +80,7 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
'pre1',
|
||||
'pre2',
|
||||
]
|
||||
logger = None
|
||||
pgworker = None
|
||||
remote_addr = None
|
||||
version = None
|
||||
|
|
@ -91,6 +92,7 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
|
||||
def on_open(self):
|
||||
"""Handle new websocket connection."""
|
||||
self.logger = self.ws.logger
|
||||
self.request = WSGIRequest(self.ws.environ)
|
||||
# Apply request middleware (so we get request.user and other attrs)
|
||||
# pylint: disable=protected-access
|
||||
|
|
@ -105,7 +107,7 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
self.ws.environ,
|
||||
)
|
||||
self.subs = {}
|
||||
print('+ %s OPEN %s' % (self, self.request.user))
|
||||
self.logger.info('+ %s OPEN %s', self, self.request.user)
|
||||
self.send('o')
|
||||
self.send('a["{\\"server_id\\":\\"0\\"}"]')
|
||||
|
||||
|
|
@ -115,14 +117,14 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
|
||||
def on_close(self, reason):
|
||||
"""Handle closing of websocket connection."""
|
||||
print('- %s %s' % (self, reason or 'CLOSE'))
|
||||
self.logger.info('- %s %s', self, reason or 'CLOSE')
|
||||
|
||||
def on_message(self, message):
|
||||
"""Process a message received from remote."""
|
||||
if self.ws.closed:
|
||||
return None
|
||||
try:
|
||||
print('< %s %r' % (self, message))
|
||||
self.logger.debug('< %s %r', self, message)
|
||||
|
||||
# parse message set
|
||||
try:
|
||||
|
|
@ -183,7 +185,7 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
|
||||
def send(self, data):
|
||||
"""Send raw `data` to WebSocket client."""
|
||||
print('> %s %r' % (self, data))
|
||||
self.logger.debug('> %s %r', self, data)
|
||||
try:
|
||||
self.ws.send(data)
|
||||
except geventwebsocket.WebSocketError:
|
||||
|
|
@ -206,7 +208,7 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
data['reason'] = reason
|
||||
if detail:
|
||||
data['detail'] = detail
|
||||
print('! %s %r' % (self, data))
|
||||
self.logger.error('! %s %r', self, data)
|
||||
self.reply('error', **data)
|
||||
|
||||
def recv_connect(self, version, support, session=None):
|
||||
|
|
@ -268,7 +270,5 @@ class DDPWebSocketApplication(geventwebsocket.WebSocketApplication):
|
|||
else:
|
||||
try:
|
||||
self.reply('result', id=id_, result=func(**params))
|
||||
except Exception, err:
|
||||
except Exception, err: # pylint: disable=W0703
|
||||
self.reply('result', id=id_, error='%s' % err)
|
||||
finally:
|
||||
pass
|
||||
|
|
|
|||
Loading…
Reference in a new issue