Actualizar nodo OmniVision
Browse files
client.py
CHANGED
|
@@ -517,39 +517,93 @@ class WorkerEngine:
|
|
| 517 |
elapsed = time.time() - start_t
|
| 518 |
STATE.log(f"[{task_id}] Compresi贸n exitosa! Original: {original_size} bytes -> Comprimido: {compressed_size} bytes ({elapsed:.1f}s)")
|
| 519 |
|
| 520 |
-
# 4. Subir archivo comprimido al Manager
|
| 521 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 522 |
asyncio.run_coroutine_threadsafe(
|
| 523 |
self._send_ws_safe(TYPE_TASK_PROGRESS, {
|
| 524 |
-
"task_id": task_id, "stage": "uploading", "percent": 0.0
|
|
|
|
| 525 |
}), self.loop
|
| 526 |
)
|
| 527 |
-
job.status = "Subiendo..."
|
| 528 |
-
upload_success = False
|
| 529 |
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
|
| 554 |
if not upload_success:
|
| 555 |
self._report_failed(task_id, "fall贸 subida al manager tras m煤ltiples reintentos")
|
|
|
|
| 517 |
elapsed = time.time() - start_t
|
| 518 |
STATE.log(f"[{task_id}] Compresi贸n exitosa! Original: {original_size} bytes -> Comprimido: {compressed_size} bytes ({elapsed:.1f}s)")
|
| 519 |
|
| 520 |
+
# 4. Subir archivo comprimido al Manager en bloques (chunked resumable upload)
|
| 521 |
+
# para evitar cortes por timeouts de reverse proxy (Caddy / Cloudflare / Nginx 60s)
|
| 522 |
+
upload_base_url = self._resolve_url(task.get("upload_url", ""))
|
| 523 |
+
job.status = "Subiendo..."
|
| 524 |
+
upload_success = False
|
| 525 |
+
|
| 526 |
+
chunk_size = 16 * 1024 * 1024 # 16 MB por bloque
|
| 527 |
+
offset = 0
|
| 528 |
+
started = time.monotonic()
|
| 529 |
+
|
| 530 |
asyncio.run_coroutine_threadsafe(
|
| 531 |
self._send_ws_safe(TYPE_TASK_PROGRESS, {
|
| 532 |
+
"task_id": task_id, "stage": "uploading", "percent": 0.0,
|
| 533 |
+
"current": 0, "total": compressed_size,
|
| 534 |
}), self.loop
|
| 535 |
)
|
|
|
|
|
|
|
| 536 |
|
| 537 |
+
with open(output_path, "rb") as out_f:
|
| 538 |
+
while offset < compressed_size:
|
| 539 |
+
if cancel_event.is_set():
|
| 540 |
+
self._report_failed(task_id, "subida cancelada")
|
| 541 |
+
return
|
| 542 |
+
|
| 543 |
+
out_f.seek(offset)
|
| 544 |
+
chunk_data = out_f.read(chunk_size)
|
| 545 |
+
if not chunk_data:
|
| 546 |
+
break
|
| 547 |
+
|
| 548 |
+
chunk_len = len(chunk_data)
|
| 549 |
+
end_offset = offset + chunk_len - 1
|
| 550 |
+
|
| 551 |
+
sep = "&" if "?" in upload_base_url else "?"
|
| 552 |
+
chunk_url = f"{upload_base_url}{sep}offset={offset}&total={compressed_size}"
|
| 553 |
+
|
| 554 |
+
chunk_headers = {
|
| 555 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
|
| 556 |
+
"Content-Type": "application/octet-stream",
|
| 557 |
+
"X-Worker-Secret": self.secret,
|
| 558 |
+
"Content-Length": str(chunk_len),
|
| 559 |
+
"Content-Range": f"bytes {offset}-{end_offset}/{compressed_size}",
|
| 560 |
+
}
|
| 561 |
+
|
| 562 |
+
chunk_uploaded = False
|
| 563 |
+
for chunk_attempt in range(1, 16):
|
| 564 |
+
if cancel_event.is_set():
|
| 565 |
+
self._report_failed(task_id, "subida cancelada")
|
| 566 |
+
return
|
| 567 |
+
try:
|
| 568 |
+
resp = self.session.post(chunk_url, data=chunk_data, headers=chunk_headers, timeout=60)
|
| 569 |
+
if resp.status_code in (200, 206):
|
| 570 |
+
chunk_uploaded = True
|
| 571 |
+
break
|
| 572 |
+
else:
|
| 573 |
+
STATE.log(f"[{task_id}] Manager respondi贸 status {resp.status_code} en bloque @{offset}")
|
| 574 |
+
time.sleep(2)
|
| 575 |
+
except Exception as ex:
|
| 576 |
+
if chunk_attempt < 15:
|
| 577 |
+
STATE.log(f"[{task_id}] Reintentando bloque @{offset} ({chunk_attempt}/15): {ex}", level="WARNING")
|
| 578 |
+
time.sleep(3)
|
| 579 |
+
|
| 580 |
+
if not chunk_uploaded:
|
| 581 |
+
STATE.log(f"[{task_id}] Fall贸 la subida del bloque @{offset} tras m煤ltiples reintentos", level="ERROR")
|
| 582 |
+
break
|
| 583 |
+
|
| 584 |
+
offset += chunk_len
|
| 585 |
+
|
| 586 |
+
now = time.monotonic()
|
| 587 |
+
elapsed = max(now - started, 0.001)
|
| 588 |
+
percent = min(100.0, offset * 100.0 / max(compressed_size, 1))
|
| 589 |
+
speed_mb = (offset / elapsed) / (1024 * 1024)
|
| 590 |
+
eta_sec = int((compressed_size - offset) / (offset / elapsed)) if offset < compressed_size else 0
|
| 591 |
+
progress = {
|
| 592 |
+
"task_id": task_id,
|
| 593 |
+
"stage": "uploading",
|
| 594 |
+
"percent": percent,
|
| 595 |
+
"current": offset,
|
| 596 |
+
"total": compressed_size,
|
| 597 |
+
"speed": f"{speed_mb:.1f} MB/s",
|
| 598 |
+
"eta": f"{eta_sec}s" if eta_sec > 0 else "",
|
| 599 |
+
}
|
| 600 |
+
asyncio.run_coroutine_threadsafe(
|
| 601 |
+
self._send_ws_safe(TYPE_TASK_PROGRESS, progress),
|
| 602 |
+
self.loop,
|
| 603 |
+
)
|
| 604 |
+
|
| 605 |
+
if offset >= compressed_size:
|
| 606 |
+
upload_success = True
|
| 607 |
|
| 608 |
if not upload_success:
|
| 609 |
self._report_failed(task_id, "fall贸 subida al manager tras m煤ltiples reintentos")
|