mirror of https://github.com/jumpserver/jumpserver
perf: 在线用户添加是否活跃的属性
parent
5e54792d94
commit
7746491e19
|
@ -12,6 +12,7 @@ from django.utils.translation import gettext, gettext_lazy as _
|
||||||
|
|
||||||
from common.db.encoder import ModelJSONFieldEncoder
|
from common.db.encoder import ModelJSONFieldEncoder
|
||||||
from common.utils import lazyproperty, i18n_trans
|
from common.utils import lazyproperty, i18n_trans
|
||||||
|
from notifications.ws import WS_SESSION_KEY
|
||||||
from ops.models import JobExecution
|
from ops.models import JobExecution
|
||||||
from orgs.mixins.models import OrgModelMixin, Organization
|
from orgs.mixins.models import OrgModelMixin, Organization
|
||||||
from orgs.utils import current_org
|
from orgs.utils import current_org
|
||||||
|
@ -275,6 +276,10 @@ class UserSession(models.Model):
|
||||||
def backend_display(self):
|
def backend_display(self):
|
||||||
return gettext(self.backend)
|
return gettext(self.backend)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_active(self):
|
||||||
|
return caches.sismember(WS_SESSION_KEY, self.key)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def date_expired(self):
|
def date_expired(self):
|
||||||
session_store_cls = import_module(settings.SESSION_ENGINE).SessionStore
|
session_store_cls = import_module(settings.SESSION_ENGINE).SessionStore
|
||||||
|
|
|
@ -1,23 +1,27 @@
|
||||||
import threading
|
|
||||||
import json
|
import json
|
||||||
from channels.generic.websocket import JsonWebsocketConsumer
|
|
||||||
|
|
||||||
from common.utils import get_logger
|
from channels.generic.websocket import JsonWebsocketConsumer
|
||||||
|
from django.core.cache import caches
|
||||||
|
|
||||||
from common.db.utils import safe_db_connection
|
from common.db.utils import safe_db_connection
|
||||||
from .site_msg import SiteMessageUtil
|
from common.utils import get_logger
|
||||||
from .signal_handlers import new_site_msg_chan
|
from .signal_handlers import new_site_msg_chan
|
||||||
|
from .site_msg import SiteMessageUtil
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
WS_SESSION_KEY = 'ws_session_key'
|
||||||
|
|
||||||
|
|
||||||
class SiteMsgWebsocket(JsonWebsocketConsumer):
|
class SiteMsgWebsocket(JsonWebsocketConsumer):
|
||||||
refresh_every_seconds = 10
|
|
||||||
sub = None
|
sub = None
|
||||||
|
refresh_every_seconds = 10
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
user = self.scope["user"]
|
user = self.scope["user"]
|
||||||
if user.is_authenticated:
|
if user.is_authenticated:
|
||||||
self.accept()
|
self.accept()
|
||||||
|
session = self.scope['session']
|
||||||
|
caches.sadd(WS_SESSION_KEY, session.session_key)
|
||||||
self.sub = self.watch_recv_new_site_msg()
|
self.sub = self.watch_recv_new_site_msg()
|
||||||
else:
|
else:
|
||||||
self.close()
|
self.close()
|
||||||
|
@ -58,6 +62,8 @@ class SiteMsgWebsocket(JsonWebsocketConsumer):
|
||||||
return new_site_msg_chan.subscribe(handle_new_site_msg_recv)
|
return new_site_msg_chan.subscribe(handle_new_site_msg_recv)
|
||||||
|
|
||||||
def disconnect(self, code):
|
def disconnect(self, code):
|
||||||
if self.sub:
|
if not self.sub:
|
||||||
|
return
|
||||||
self.sub.unsubscribe()
|
self.sub.unsubscribe()
|
||||||
|
session = self.scope['session']
|
||||||
|
caches.srem(WS_SESSION_KEY, session.session_key)
|
||||||
|
|
Loading…
Reference in New Issue