(ctx context.Context, dep dependency.Dep)
| 121 | } |
| 122 | |
| 123 | func (m *ImportTask) processImport(ctx context.Context, dep dependency.Dep) (task.Status, error) { |
| 124 | user := inventory.UserFromContext(ctx) |
| 125 | |
| 126 | dst, err := fs.NewUriFromString(m.state.Dst) |
| 127 | if err != nil { |
| 128 | return task.StatusError, fmt.Errorf("failed to parse dst: %s (%w)", err, queue.CriticalErr) |
| 129 | } |
| 130 | |
| 131 | // Use a temporary file manager just for listing physical files |
| 132 | listFm := manager.NewFileManager(dep, user) |
| 133 | physicalFiles, err := listFm.ListPhysical(ctx, m.state.Src, m.state.PolicyID, m.state.Recursive, |
| 134 | func(i int) { |
| 135 | atomic.AddInt64(&m.progress[ProgressTypeIndexed].Current, int64(i)) |
| 136 | }) |
| 137 | listFm.Recycle() |
| 138 | if err != nil { |
| 139 | return task.StatusError, fmt.Errorf("failed to list physical files: %w", err) |
| 140 | } |
| 141 | |
| 142 | m.l.Info("Importing %d physical files", len(physicalFiles)) |
| 143 | |
| 144 | m.Lock() |
| 145 | m.progress[ProgressTypeImported] = &queue.Progress{ |
| 146 | Total: int64(len(physicalFiles)), |
| 147 | } |
| 148 | delete(m.progress, ProgressTypeIndexed) |
| 149 | m.Unlock() |
| 150 | |
| 151 | failed := 0 |
| 152 | totalFiles := len(physicalFiles) |
| 153 | |
| 154 | // Process files in batches to control memory usage |
| 155 | for batchStart := 0; batchStart < totalFiles; batchStart += ImportBatchSize { |
| 156 | batchEnd := min(batchStart+ImportBatchSize, totalFiles) |
| 157 | |
| 158 | batch := physicalFiles[batchStart:batchEnd] |
| 159 | batchFailed := m.processBatch(ctx, dep, user, dst, batch) |
| 160 | failed += batchFailed |
| 161 | |
| 162 | // Clear batch elements to allow GC of individual items |
| 163 | for i := batchStart; i < batchEnd; i++ { |
| 164 | physicalFiles[i] = fs.PhysicalObject{} |
| 165 | } |
| 166 | |
| 167 | // Run GC after each batch to free memory |
| 168 | runtime.GC() |
| 169 | } |
| 170 | |
| 171 | // Clear the entire slice to allow GC |
| 172 | physicalFiles = nil |
| 173 | runtime.GC() |
| 174 | |
| 175 | m.state.Failed = failed |
| 176 | return task.StatusCompleted, nil |
| 177 | } |
| 178 | |
| 179 | // processBatch processes a batch of physical files with a fresh file manager. |
| 180 | func (m *ImportTask) processBatch(ctx context.Context, dep dependency.Dep, user *ent.User, dst *fs.URI, batch []fs.PhysicalObject) int { |
no test coverage detected