55 lines
1.5 KiB
Plaintext
55 lines
1.5 KiB
Plaintext
# Demucs 任务状态查询
|
||
# 部署到: /data/ymq/demucs-service/app/api/demucs-status.dspy
|
||
# GET /demucs/api/demucs-status?task_id=xxx
|
||
import json
|
||
|
||
task_id = params_kw.task_id
|
||
if not task_id:
|
||
return {"error": "task_id is required"}
|
||
|
||
env = request._run_ns
|
||
longtasks = env.longtasks
|
||
|
||
# longtasks 内部通过 Redis 存储结果,key 格式: {queue_name}:result:{task_id}
|
||
import redis.asyncio as redis
|
||
r = redis.Redis(host='127.0.0.1', port=6379, db=0, decode_responses=True)
|
||
|
||
# 尝试获取结果
|
||
result_key = f"demucs:result:{task_id}"
|
||
raw = await r.get(result_key)
|
||
|
||
if raw:
|
||
data = json.loads(raw)
|
||
return {
|
||
"task_id": task_id,
|
||
"status": "SUCCEEDED",
|
||
"vocals_url": data.get("vocals_path", ""),
|
||
"accompaniment_url": data.get("accompaniment_path", ""),
|
||
"duration": data.get("duration", 0),
|
||
"usage": data.get("usage", {"次": 1}),
|
||
}
|
||
|
||
# 检查是否在队列中
|
||
queue_key = f"demucs:queue"
|
||
in_queue = await r.lrange(queue_key, 0, -1)
|
||
for item in in_queue:
|
||
try:
|
||
item_data = json.loads(item)
|
||
if item_data.get("task_id") == task_id:
|
||
return {"task_id": task_id, "status": "queued"}
|
||
except:
|
||
pass
|
||
|
||
# 检查是否在处理中
|
||
processing_key = f"demucs:processing"
|
||
processing = await r.get(processing_key)
|
||
if processing:
|
||
try:
|
||
proc_data = json.loads(processing)
|
||
if proc_data.get("task_id") == task_id:
|
||
return {"task_id": task_id, "status": "processing"}
|
||
except:
|
||
pass
|
||
|
||
return {"task_id": task_id, "status": "not_found"}
|