handle_task catch all exception (#3441)

### What problem does this PR solve?

handle_task catch all exception
Report heartbeats

### Type of change

- [x] Refactoring
This commit is contained in:
Zhichang Yu
2024-11-15 18:51:09 +08:00
committed by GitHub
parent 1e90a1bf36
commit 4ed5ca2666
4 changed files with 129 additions and 131 deletions

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License
#
import json
import logging
from datetime import datetime
from flask_login import login_required, current_user
@ -154,26 +154,16 @@ def status():
"error": str(e),
}
task_executor_heartbeats = {}
try:
v = REDIS_CONN.get("TASKEXE")
if not v:
raise Exception("No task executor running!")
obj = json.loads(v)
color = "green"
for id in obj.keys():
arr = obj[id]
if len(arr) == 1:
obj[id] = [0]
else:
obj[id] = [arr[i + 1] - arr[i] for i in range(len(arr) - 1)]
elapsed = max(obj[id])
if elapsed > 50:
color = "yellow"
if elapsed > 120:
color = "red"
res["task_executor"] = {"status": color, "elapsed": obj}
except Exception as e:
res["task_executor"] = {"status": "red", "error": str(e)}
task_executors = REDIS_CONN.smembers("TASKEXE")
now = datetime.now().timestamp()
for task_executor_id in task_executors:
heartbeats = REDIS_CONN.zrangebyscore(task_executor_id, now - 60*30, now)
task_executor_heartbeats[task_executor_id] = heartbeats
except Exception:
logging.exception("get task executor heartbeats failed!")
res["task_executor_heartbeats"] = task_executor_heartbeats
return get_json_result(data=res)

View File

@ -36,7 +36,7 @@ class TaskService(CommonService):
@classmethod
@DB.connection_context()
def get_tasks(cls, task_id):
def get_task(cls, task_id):
fields = [
cls.model.id,
cls.model.doc_id,
@ -63,7 +63,7 @@ class TaskService(CommonService):
.join(Tenant, on=(Knowledgebase.tenant_id == Tenant.id)) \
.where(cls.model.id == task_id)
docs = list(docs.dicts())
if not docs: return []
if not docs: return None
msg = "\nTask has been received."
prog = random.random() / 10.
@ -77,9 +77,9 @@ class TaskService(CommonService):
).where(
cls.model.id == docs[0]["id"]).execute()
if docs[0]["retry_count"] >= 3: return []
if docs[0]["retry_count"] >= 3: return None
return docs
return docs[0]
@classmethod
@DB.connection_context()
@ -108,7 +108,7 @@ class TaskService(CommonService):
task = cls.model.get_by_id(id)
_, doc = DocumentService.get_by_id(task.doc_id)
return doc.run == TaskStatus.CANCEL.value or doc.progress < 0
except Exception as e:
except Exception:
pass
return False