Actualizar nodo OmniVision
Browse files
main.go
CHANGED
|
@@ -526,56 +526,19 @@ func (w *Worker) executeTask(taskCtx context.Context, job *CurrentJob, task prot
|
|
| 526 |
log.Printf("[%s] Compresión exitosa! Original: %d bytes -> Comprimido: %d bytes (%.1fs)",
|
| 527 |
task.TaskID, originalSize, compressedSize, elapsed)
|
| 528 |
|
| 529 |
-
// 4. Subir archivo comprimido al Manager
|
|
|
|
|
|
|
| 530 |
uploadURL := w.resolveURL(task.UploadURL)
|
| 531 |
_ = w.sendMessage(protocol.TypeTaskProgress, protocol.TaskProgress{TaskID: task.TaskID, Stage: "uploading", Percent: 0})
|
| 532 |
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
w.reportFailed(task.TaskID, fmt.Sprintf("error leyendo archivo comprimido: %v", err))
|
| 538 |
-
return
|
| 539 |
-
}
|
| 540 |
-
|
| 541 |
-
progressBody := &uploadProgressReader{file: outFile, worker: w, taskID: task.TaskID, total: compressedSize}
|
| 542 |
-
upReq, err := http.NewRequestWithContext(taskCtx, "POST", uploadURL, progressBody)
|
| 543 |
-
if err != nil {
|
| 544 |
-
_ = outFile.Close()
|
| 545 |
-
w.reportFailed(task.TaskID, fmt.Sprintf("error creando petición de subida: %v", err))
|
| 546 |
return
|
| 547 |
}
|
| 548 |
-
|
| 549 |
-
upReq.Header.Set("Content-Type", "application/octet-stream")
|
| 550 |
-
upReq.Header.Set("X-Worker-Secret", w.secret)
|
| 551 |
-
upReq.ContentLength = compressedSize
|
| 552 |
-
|
| 553 |
-
upResp, err := httpClient.Do(upReq)
|
| 554 |
-
_ = outFile.Close()
|
| 555 |
-
|
| 556 |
-
if err == nil && upResp.StatusCode == http.StatusOK {
|
| 557 |
-
_ = upResp.Body.Close()
|
| 558 |
-
uploadSuccess = true
|
| 559 |
-
break
|
| 560 |
-
}
|
| 561 |
-
|
| 562 |
-
if upResp != nil {
|
| 563 |
-
_ = upResp.Body.Close()
|
| 564 |
-
}
|
| 565 |
-
|
| 566 |
-
if attempt < 60 {
|
| 567 |
-
log.Printf("[%s] Manager no listo para recibir subida (intento %d/60): %v. Reintentando en 3s...", task.TaskID, attempt, err)
|
| 568 |
-
select {
|
| 569 |
-
case <-taskCtx.Done():
|
| 570 |
-
w.reportFailed(task.TaskID, "subida cancelada")
|
| 571 |
-
return
|
| 572 |
-
case <-time.After(3 * time.Second):
|
| 573 |
-
}
|
| 574 |
-
}
|
| 575 |
-
}
|
| 576 |
-
|
| 577 |
-
if !uploadSuccess {
|
| 578 |
-
w.reportFailed(task.TaskID, "falló subida al manager tras múltiples reintentos")
|
| 579 |
return
|
| 580 |
}
|
| 581 |
|
|
@@ -595,44 +558,105 @@ func (w *Worker) executeTask(taskCtx context.Context, job *CurrentJob, task prot
|
|
| 595 |
log.Printf("[%s] ✅ Tarea finalizada y confirmada.", task.TaskID)
|
| 596 |
}
|
| 597 |
|
| 598 |
-
|
| 599 |
-
file
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
started time.Time
|
| 605 |
-
lastReport time.Time
|
| 606 |
-
}
|
| 607 |
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
}
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
}
|
| 628 |
-
|
| 629 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
}
|
| 631 |
-
_ = r.worker.sendMessage(protocol.TypeTaskProgress, progress)
|
| 632 |
-
r.lastReport = now
|
| 633 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
}
|
| 635 |
-
|
|
|
|
| 636 |
}
|
| 637 |
|
| 638 |
// downloadWatchdog aborta una descarga si no llegan bytes durante maxIdle.
|
|
|
|
| 526 |
log.Printf("[%s] Compresión exitosa! Original: %d bytes -> Comprimido: %d bytes (%.1fs)",
|
| 527 |
task.TaskID, originalSize, compressedSize, elapsed)
|
| 528 |
|
| 529 |
+
// 4. Subir archivo comprimido al Manager por chunks de 16MB
|
| 530 |
+
// Evita que los proxies reversos (Caddy/Cloudflare) corten la conexión TLS a los 60s
|
| 531 |
+
// y permite reanudar transparentemente ante microcortes.
|
| 532 |
uploadURL := w.resolveURL(task.UploadURL)
|
| 533 |
_ = w.sendMessage(protocol.TypeTaskProgress, protocol.TaskProgress{TaskID: task.TaskID, Stage: "uploading", Percent: 0})
|
| 534 |
|
| 535 |
+
if err := w.uploadFileInChunks(taskCtx, task.TaskID, uploadURL, outputPath, compressedSize); err != nil {
|
| 536 |
+
if taskCtx.Err() != nil {
|
| 537 |
+
log.Printf("[%s] Subida cancelada por solicitud de usuario/manager.", task.TaskID)
|
| 538 |
+
w.reportFailed(task.TaskID, "subida cancelada")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
return
|
| 540 |
}
|
| 541 |
+
w.reportFailed(task.TaskID, fmt.Sprintf("falló subida al manager: %v", err))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
return
|
| 543 |
}
|
| 544 |
|
|
|
|
| 558 |
log.Printf("[%s] ✅ Tarea finalizada y confirmada.", task.TaskID)
|
| 559 |
}
|
| 560 |
|
| 561 |
+
func (w *Worker) uploadFileInChunks(ctx context.Context, taskID string, uploadURL string, filePath string, totalSize int64) error {
|
| 562 |
+
file, err := os.Open(filePath)
|
| 563 |
+
if err != nil {
|
| 564 |
+
return fmt.Errorf("error abriendo archivo para subida: %w", err)
|
| 565 |
+
}
|
| 566 |
+
defer file.Close()
|
|
|
|
|
|
|
|
|
|
| 567 |
|
| 568 |
+
const chunkSize = 16 * 1024 * 1024 // 16 MB por chunk para evitar timeouts del proxy reverso (60s)
|
| 569 |
+
startTime := time.Now()
|
| 570 |
+
var offset int64
|
| 571 |
+
|
| 572 |
+
for offset < totalSize {
|
| 573 |
+
if ctx.Err() != nil {
|
| 574 |
+
return ctx.Err()
|
| 575 |
+
}
|
| 576 |
+
|
| 577 |
+
currentChunkSize := int64(chunkSize)
|
| 578 |
+
if offset+currentChunkSize > totalSize {
|
| 579 |
+
currentChunkSize = totalSize - offset
|
| 580 |
+
}
|
| 581 |
+
|
| 582 |
+
var chunkSuccess bool
|
| 583 |
+
var lastErr error
|
| 584 |
+
|
| 585 |
+
for attempt := 1; attempt <= 15; attempt++ {
|
| 586 |
+
if ctx.Err() != nil {
|
| 587 |
+
return ctx.Err()
|
| 588 |
}
|
| 589 |
+
|
| 590 |
+
sectionReader := io.NewSectionReader(file, offset, currentChunkSize)
|
| 591 |
+
sep := "?"
|
| 592 |
+
if strings.Contains(uploadURL, "?") {
|
| 593 |
+
sep = "&"
|
| 594 |
+
}
|
| 595 |
+
chunkURL := fmt.Sprintf("%s%soffset=%d&total=%d", uploadURL, sep, offset, totalSize)
|
| 596 |
+
|
| 597 |
+
req, err := http.NewRequestWithContext(ctx, "POST", chunkURL, sectionReader)
|
| 598 |
+
if err != nil {
|
| 599 |
+
return fmt.Errorf("error creando petición HTTP de chunk: %w", err)
|
| 600 |
}
|
| 601 |
+
|
| 602 |
+
req.Header.Set("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")
|
| 603 |
+
req.Header.Set("X-Worker-Secret", w.secret)
|
| 604 |
+
req.Header.Set("Content-Type", "application/octet-stream")
|
| 605 |
+
req.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", offset, offset+currentChunkSize-1, totalSize))
|
| 606 |
+
req.ContentLength = currentChunkSize
|
| 607 |
+
|
| 608 |
+
resp, err := httpClient.Do(req)
|
| 609 |
+
if err == nil {
|
| 610 |
+
_ = resp.Body.Close()
|
| 611 |
+
if resp.StatusCode == http.StatusOK {
|
| 612 |
+
chunkSuccess = true
|
| 613 |
+
break
|
| 614 |
+
}
|
| 615 |
+
lastErr = fmt.Errorf("status HTTP %d", resp.StatusCode)
|
| 616 |
+
} else {
|
| 617 |
+
lastErr = err
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
+
if attempt < 15 {
|
| 621 |
+
log.Printf("[%s] Reintentando chunk @%d (%d bytes, intento %d/15): %v. Esperando %ds...",
|
| 622 |
+
taskID, offset, currentChunkSize, attempt, lastErr, attempt)
|
| 623 |
+
select {
|
| 624 |
+
case <-ctx.Done():
|
| 625 |
+
return ctx.Err()
|
| 626 |
+
case <-time.After(time.Duration(attempt) * time.Second):
|
| 627 |
+
}
|
| 628 |
}
|
|
|
|
|
|
|
| 629 |
}
|
| 630 |
+
|
| 631 |
+
if !chunkSuccess {
|
| 632 |
+
return fmt.Errorf("falló subida de chunk @%d tras 15 intentos: %v", offset, lastErr)
|
| 633 |
+
}
|
| 634 |
+
|
| 635 |
+
offset += currentChunkSize
|
| 636 |
+
|
| 637 |
+
// Notificar progreso acumulado
|
| 638 |
+
elapsed := time.Since(startTime).Seconds()
|
| 639 |
+
if elapsed < 0.001 {
|
| 640 |
+
elapsed = 0.001
|
| 641 |
+
}
|
| 642 |
+
percent := float64(offset) * 100.0 / float64(totalSize)
|
| 643 |
+
speedMBs := float64(offset) / elapsed / (1024 * 1024)
|
| 644 |
+
progress := protocol.TaskProgress{
|
| 645 |
+
TaskID: taskID,
|
| 646 |
+
Stage: "uploading",
|
| 647 |
+
Percent: percent,
|
| 648 |
+
Speed: fmt.Sprintf("%.1f MB/s", speedMBs),
|
| 649 |
+
Current: offset,
|
| 650 |
+
Total: totalSize,
|
| 651 |
+
}
|
| 652 |
+
if offset < totalSize && speedMBs > 0 {
|
| 653 |
+
remainingSec := int(float64(totalSize-offset) / (speedMBs * 1024 * 1024))
|
| 654 |
+
progress.ETA = fmt.Sprintf("%ds", remainingSec)
|
| 655 |
+
}
|
| 656 |
+
_ = w.sendMessage(protocol.TypeTaskProgress, progress)
|
| 657 |
}
|
| 658 |
+
|
| 659 |
+
return nil
|
| 660 |
}
|
| 661 |
|
| 662 |
// downloadWatchdog aborta una descarga si no llegan bytes durante maxIdle.
|