mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
Close old conns
This commit is contained in:
parent
5d6cfa7349
commit
76771e88be
@ -1,9 +1,14 @@
|
|||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
from asgiref.sync import async_to_sync
|
from asgiref.sync import async_to_sync
|
||||||
from channels.exceptions import AcceptConnection
|
from channels.exceptions import AcceptConnection
|
||||||
from channels.exceptions import DenyConnection
|
from channels.exceptions import DenyConnection
|
||||||
from channels.generic.websocket import WebsocketConsumer
|
from channels.generic.websocket import WebsocketConsumer
|
||||||
|
from django.db import close_old_connections
|
||||||
|
from django.db import connections
|
||||||
|
|
||||||
|
logger = logging.getLogger("paperless.websockets")
|
||||||
|
|
||||||
|
|
||||||
class StatusConsumer(WebsocketConsumer):
|
class StatusConsumer(WebsocketConsumer):
|
||||||
@ -20,7 +25,14 @@ class StatusConsumer(WebsocketConsumer):
|
|||||||
else True
|
else True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _discard_database_connections(self):
|
||||||
|
logger.debug("Discarding %s database connections...", len(connections.all()))
|
||||||
|
for conn in connections.all():
|
||||||
|
conn.close()
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
|
logger.debug("Connecting ws...")
|
||||||
|
close_old_connections()
|
||||||
if not self._authenticated():
|
if not self._authenticated():
|
||||||
raise DenyConnection
|
raise DenyConnection
|
||||||
else:
|
else:
|
||||||
@ -31,11 +43,17 @@ class StatusConsumer(WebsocketConsumer):
|
|||||||
raise AcceptConnection
|
raise AcceptConnection
|
||||||
|
|
||||||
def disconnect(self, close_code):
|
def disconnect(self, close_code):
|
||||||
|
logger.debug("Disconnecting ws...")
|
||||||
|
self._discard_database_connections()
|
||||||
async_to_sync(self.channel_layer.group_discard)(
|
async_to_sync(self.channel_layer.group_discard)(
|
||||||
"status_updates",
|
"status_updates",
|
||||||
self.channel_name,
|
self.channel_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def close(self, code=None, reason=None):
|
||||||
|
self._discard_database_connections()
|
||||||
|
return super().close(code, reason)
|
||||||
|
|
||||||
def status_update(self, event):
|
def status_update(self, event):
|
||||||
if not self._authenticated():
|
if not self._authenticated():
|
||||||
self.close()
|
self.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user