mirror of https://github.com/jumpserver/jumpserver
Browse Source
* [Update] add ws support * [Update] 修改log使用websocket * [Update] 修复 资产用户 右侧动作菜单,字体颜色 * [Update] 修改Dockerfile * [Bugfix] 修复settings中WSG_APPLICATIONpull/3293/head
老广
5 years ago
committed by
GitHub
28 changed files with 171 additions and 105 deletions
@ -0,0 +1,7 @@
|
||||
import os |
||||
import django |
||||
from channels.routing import get_default_application |
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jumpserver.settings") |
||||
django.setup() |
||||
application = get_default_application() |
@ -0,0 +1,13 @@
|
||||
from channels.auth import AuthMiddlewareStack |
||||
from channels.routing import ProtocolTypeRouter, URLRouter |
||||
|
||||
from ops.urls.ws_urls import urlpatterns as ops_urlpatterns |
||||
|
||||
urlpatterns = [] |
||||
urlpatterns += ops_urlpatterns |
||||
|
||||
application = ProtocolTypeRouter({ |
||||
'websocket': AuthMiddlewareStack( |
||||
URLRouter(urlpatterns) |
||||
), |
||||
}) |
@ -0,0 +1,9 @@
|
||||
from django.urls import path |
||||
|
||||
from .. import ws |
||||
|
||||
app_name = 'ops' |
||||
|
||||
urlpatterns = [ |
||||
path('ws/ops/tasks/<uuid:task_id>/log/', ws.CeleryLogWebsocket, name='task-log-ws'), |
||||
] |
@ -0,0 +1,41 @@
|
||||
import time |
||||
import threading |
||||
|
||||
from .celery.utils import get_celery_task_log_path |
||||
from channels.generic.websocket import JsonWebsocketConsumer |
||||
|
||||
|
||||
class CeleryLogWebsocket(JsonWebsocketConsumer): |
||||
task = '' |
||||
task_log_f = None |
||||
disconnected = False |
||||
|
||||
def connect(self): |
||||
task_id = self.scope['url_route']['kwargs']['task_id'] |
||||
log_path = get_celery_task_log_path(task_id) |
||||
try: |
||||
self.task_log_f = open(log_path) |
||||
except OSError: |
||||
self.send({'message': "Task {} log not found".format(task_id)}) |
||||
self.disconnect(None) |
||||
return |
||||
|
||||
self.accept() |
||||
self.send_log_to_client() |
||||
|
||||
def disconnect(self, close_code): |
||||
self.disconnected = True |
||||
if self.task_log_f and not self.task_log_f.closed: |
||||
self.task_log_f.close() |
||||
self.close() |
||||
|
||||
def send_log_to_client(self): |
||||
def func(): |
||||
while not self.disconnected: |
||||
data = self.task_log_f.read(4096) |
||||
if data: |
||||
data = data.replace('\n', '\r\n') |
||||
self.send_json({'message': data}) |
||||
time.sleep(0.2) |
||||
thread = threading.Thread(target=func) |
||||
thread.start() |
Loading…
Reference in new issue