|
- # -*- coding: utf-8 -*-
- import sys
- from multiprocessing import Queue
- from threading import Thread
- from time import sleep
- from traceback import format_exc
-
- from loguru import logger
-
- from bean.Feedback import upload_http_result
- from common.Constant import get_task_record, get_share_queue
- from concurrency.http.HttpFeedbackThread import HttpFeedbackThread
- from concurrency.http.HttpUploadFileProcess import HttpUploadFileProcess
- from enums.ExceptionEnum import UploadStrExceptionType
- from enums.StatusEnum import UploadTaskStatusType
- from exception.CustomerException import ServiceException
- from util.QueUtil import get_no_block_queue
-
-
- class HttpServiceImpl:
- __slots__ = ()
-
- def __init__(self, service_config):
- service_thread = Thread(target=self.start_service, args=(service_config,))
- service_thread.setDaemon(True)
- service_thread.start()
-
- @staticmethod
- def start_service(service_config):
- fb_queue = Queue()
- task, msg_queue = get_task_record(), get_share_queue()
- handle_method = {
- "upload": lambda x, y, z, h: handle_upload(x, y, z, h)
- }
- feedbackThread = None
- while True:
- try:
- if task["upload"] is not None and not task["upload"].is_alive():
- task["upload"] = None
- feedbackThread = start_feedback_thread(feedbackThread, fb_queue)
- message = get_no_block_queue(msg_queue)
- if message is not None and isinstance(message, tuple) and len(message) == 2:
- if handle_method.get(message[0]) is not None:
- handle_method[message[0]](message[1], service_config, task, fb_queue)
- else:
- sleep(1)
- except Exception:
- logger.error("服务异常: {}", format_exc())
-
-
- def handle_upload(msg, service_config, task, fb_queue):
- try:
- command = msg["command"]
- if 'start' == command:
- if task["upload"] is not None:
- logger.warning("上传任务已存在!!!")
- upload_http_result(fb_queue, msg["callback_url"], msg["request_id"],
- errorCode=UploadStrExceptionType.UPLOAD_TASK_IS_AREADLY.value[0],
- errorMsg=UploadStrExceptionType.UPLOAD_TASK_IS_AREADLY.value[1],
- status=UploadTaskStatusType.FAILED.value[0])
- return
- upload_p = HttpUploadFileProcess(fb_queue, service_config, msg["callback_url"], msg["request_id"])
- upload_p.start()
- task["upload"] = upload_p
- elif 'stop' == command:
- if task["upload"] is None:
- logger.error("任务不存在, 任务无法停止!")
- return
- task["upload"].sendEvent({"command": "stop"})
- except ServiceException as s:
- logger.error("文件上传请求异常: {}", s.msg)
- upload_http_result(fb_queue, msg["callback_url"], msg["request_id"],
- errorCode=s.code,
- errorMsg=s.msg,
- status=UploadTaskStatusType.FAILED.value[0])
- except Exception:
- logger.error("消息处理异常: {}", format_exc())
- upload_http_result(fb_queue, msg["callback_url"], msg["request_id"],
- errorCode=UploadStrExceptionType.SERVICE_INNER_EXCEPTION.value[0],
- errorMsg=UploadStrExceptionType.SERVICE_INNER_EXCEPTION.value[1],
- status=UploadTaskStatusType.FAILED.value[0])
- finally:
- del msg
-
-
- def start_feedback_thread(feedbackThread, fb_queue):
- if feedbackThread is None:
- feedbackThread = HttpFeedbackThread(fb_queue)
- feedbackThread.setDaemon(True)
- feedbackThread.start()
- else:
- if not feedbackThread.is_alive():
- logger.error("反馈线程异常停止! 开始终止程序!")
- sys.exit()
- return feedbackThread
|