(op *ManagementOp[gallery.GalleryModel, gallery.ModelConfig], cl *config.ModelConfigLoader, systemState *system.SystemState)
| 21 | ) |
| 22 | |
| 23 | func (g *GalleryService) modelHandler(op *ManagementOp[gallery.GalleryModel, gallery.ModelConfig], cl *config.ModelConfigLoader, systemState *system.SystemState) error { |
| 24 | utils.ResetDownloadTimers() |
| 25 | |
| 26 | // Dedup check in distributed mode — skip if another instance is already processing this element |
| 27 | if g.galleryStore != nil && op.GalleryElementName != "" && !op.Delete { |
| 28 | dup, err := g.galleryStore.FindDuplicate(op.GalleryElementName) |
| 29 | if err == nil && dup != nil && dup.ID != op.ID { |
| 30 | g.UpdateStatus(op.ID, &OpStatus{ |
| 31 | Processed: true, |
| 32 | Message: fmt.Sprintf("already being processed by another instance (op %s)", dup.ID), |
| 33 | }) |
| 34 | return nil |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Check if already cancelled |
| 39 | if op.Context != nil { |
| 40 | select { |
| 41 | case <-op.Context.Done(): |
| 42 | g.UpdateStatus(op.ID, &OpStatus{ |
| 43 | Cancelled: true, |
| 44 | Processed: true, |
| 45 | Message: "cancelled", |
| 46 | GalleryElementName: op.GalleryElementName, |
| 47 | }) |
| 48 | return op.Context.Err() |
| 49 | default: |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | g.UpdateStatus(op.ID, &OpStatus{Message: fmt.Sprintf("processing model: %s", op.GalleryElementName), Progress: 0, Cancellable: true}) |
| 54 | |
| 55 | // displayDownload displays the download progress |
| 56 | progressCallback := func(fileName string, current string, total string, percentage float64) { |
| 57 | // Check for cancellation during progress updates |
| 58 | if op.Context != nil { |
| 59 | select { |
| 60 | case <-op.Context.Done(): |
| 61 | return |
| 62 | default: |
| 63 | } |
| 64 | } |
| 65 | g.UpdateStatus(op.ID, &OpStatus{Message: fmt.Sprintf(processingMessage, fileName, total, current), FileName: fileName, Progress: percentage, TotalFileSize: total, DownloadedFileSize: current, Cancellable: true}) |
| 66 | utils.DisplayDownloadFunction(fileName, current, total, percentage) |
| 67 | } |
| 68 | |
| 69 | var err error |
| 70 | if op.Delete { |
| 71 | err = g.modelManager.DeleteModel(op.GalleryElementName) |
| 72 | } else { |
| 73 | err = g.modelManager.InstallModel(op.Context, op, progressCallback) |
| 74 | } |
| 75 | if err != nil { |
| 76 | // Check if error is due to cancellation |
| 77 | if op.Context != nil && errors.Is(err, op.Context.Err()) { |
| 78 | g.UpdateStatus(op.ID, &OpStatus{ |
| 79 | Cancelled: true, |
| 80 | Processed: true, |
no test coverage detected