2019-09-26 11:22:17 +00:00
|
|
|
import time
|
2019-10-10 04:47:21 +00:00
|
|
|
import os
|
2019-09-26 11:22:17 +00:00
|
|
|
import threading
|
2019-10-10 04:47:21 +00:00
|
|
|
import json
|
|
|
|
|
2019-10-21 11:28:33 +00:00
|
|
|
from common.utils import get_logger
|
2019-09-26 11:22:17 +00:00
|
|
|
|
|
|
|
from .celery.utils import get_celery_task_log_path
|
|
|
|
from channels.generic.websocket import JsonWebsocketConsumer
|
|
|
|
|
2019-10-21 11:28:33 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2019-09-26 11:22:17 +00:00
|
|
|
|
|
|
|
class CeleryLogWebsocket(JsonWebsocketConsumer):
|
|
|
|
disconnected = False
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.accept()
|
|
|
|
|
2019-10-10 04:47:21 +00:00
|
|
|
def receive(self, text_data=None, bytes_data=None, **kwargs):
|
|
|
|
data = json.loads(text_data)
|
|
|
|
task_id = data.get("task")
|
|
|
|
if task_id:
|
|
|
|
self.handle_task(task_id)
|
|
|
|
|
2019-10-23 02:14:07 +00:00
|
|
|
def wait_util_log_path_exist(self, task_id):
|
2019-10-10 04:47:21 +00:00
|
|
|
log_path = get_celery_task_log_path(task_id)
|
2019-10-23 02:14:07 +00:00
|
|
|
while not self.disconnected:
|
|
|
|
if not os.path.exists(log_path):
|
|
|
|
self.send_json({'message': '.', 'task': task_id})
|
|
|
|
time.sleep(0.5)
|
|
|
|
continue
|
|
|
|
self.send_json({'message': '\r\n'})
|
|
|
|
try:
|
|
|
|
logger.debug('Task log path: {}'.format(log_path))
|
|
|
|
task_log_f = open(log_path, 'rb')
|
|
|
|
return task_log_f
|
|
|
|
except OSError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def read_log_file(self, task_id):
|
|
|
|
task_log_f = self.wait_util_log_path_exist(task_id)
|
|
|
|
if not task_log_f:
|
2019-10-23 03:01:39 +00:00
|
|
|
logger.debug('Task log file is None: {}'.format(task_id))
|
2019-10-23 02:14:07 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
task_end_mark = []
|
|
|
|
while not self.disconnected:
|
|
|
|
data = task_log_f.read(4096)
|
|
|
|
if data:
|
|
|
|
data = data.replace(b'\n', b'\r\n')
|
|
|
|
self.send_json(
|
2019-10-23 03:01:39 +00:00
|
|
|
{'message': data.decode(errors='ignore'), 'task': task_id}
|
|
|
|
)
|
2019-10-23 02:14:07 +00:00
|
|
|
if data.find(b'succeeded in') != -1:
|
|
|
|
task_end_mark.append(1)
|
|
|
|
if data.find(bytes(task_id, 'utf8')) != -1:
|
|
|
|
task_end_mark.append(1)
|
|
|
|
elif len(task_end_mark) == 2:
|
|
|
|
logger.debug('Task log end: {}'.format(task_id))
|
|
|
|
break
|
|
|
|
time.sleep(0.2)
|
|
|
|
task_log_f.close()
|
2019-09-26 11:22:17 +00:00
|
|
|
|
2019-10-23 02:14:07 +00:00
|
|
|
def handle_task(self, task_id):
|
|
|
|
logger.info("Task id: {}".format(task_id))
|
|
|
|
thread = threading.Thread(target=self.read_log_file, args=(task_id,))
|
2019-09-26 11:22:17 +00:00
|
|
|
thread.start()
|
2019-10-10 04:47:21 +00:00
|
|
|
|
|
|
|
def disconnect(self, close_code):
|
|
|
|
self.disconnected = True
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|