mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-03-31 13:35:08 -05:00
Close old conns
This commit is contained in:
parent
5d6cfa7349
commit
76771e88be
@ -1,9 +1,14 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from asgiref.sync import async_to_sync
|
||||
from channels.exceptions import AcceptConnection
|
||||
from channels.exceptions import DenyConnection
|
||||
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):
|
||||
@ -20,7 +25,14 @@ class StatusConsumer(WebsocketConsumer):
|
||||
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):
|
||||
logger.debug("Connecting ws...")
|
||||
close_old_connections()
|
||||
if not self._authenticated():
|
||||
raise DenyConnection
|
||||
else:
|
||||
@ -31,11 +43,17 @@ class StatusConsumer(WebsocketConsumer):
|
||||
raise AcceptConnection
|
||||
|
||||
def disconnect(self, close_code):
|
||||
logger.debug("Disconnecting ws...")
|
||||
self._discard_database_connections()
|
||||
async_to_sync(self.channel_layer.group_discard)(
|
||||
"status_updates",
|
||||
self.channel_name,
|
||||
)
|
||||
|
||||
def close(self, code=None, reason=None):
|
||||
self._discard_database_connections()
|
||||
return super().close(code, reason)
|
||||
|
||||
def status_update(self, event):
|
||||
if not self._authenticated():
|
||||
self.close()
|
||||
|
Loading…
x
Reference in New Issue
Block a user