mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-22 16:53:54 +08:00
fix: harden per-item processing tracking in tasks
Use the resource fingerprint as the dedup key on insert and delete. Check and set processing entries atomically instead of TOCTOU. Count only successful downloads.
This commit is contained in:
@@ -30,21 +30,20 @@ func (t *Task) Execute(ctx context.Context) error {
|
||||
eg.SetLimit(config.C().Workers)
|
||||
for _, resource := range t.item.Resources {
|
||||
eg.Go(func() error {
|
||||
t.processingMu.RLock()
|
||||
if t.processing[resource.ID()] != nil {
|
||||
return fmt.Errorf("resource %s is already being processed", resource.ID())
|
||||
}
|
||||
t.processingMu.RUnlock()
|
||||
resourceID := resource.ID()
|
||||
t.processingMu.Lock()
|
||||
t.processing[resource.ID()] = &resource
|
||||
if t.processing[resourceID] != nil {
|
||||
t.processingMu.Unlock()
|
||||
return fmt.Errorf("resource %s is already being processed", resourceID)
|
||||
}
|
||||
t.processing[resourceID] = &resource
|
||||
t.processingMu.Unlock()
|
||||
defer func() {
|
||||
t.processingMu.Lock()
|
||||
delete(t.processing, resource.URL)
|
||||
delete(t.processing, resourceID)
|
||||
t.processingMu.Unlock()
|
||||
}()
|
||||
err := t.processResource(gctx, resource)
|
||||
t.downloaded.Add(1)
|
||||
if errors.Is(err, context.Canceled) {
|
||||
logger.Debug("Resource processing canceled")
|
||||
return err
|
||||
@@ -53,6 +52,7 @@ func (t *Task) Execute(ctx context.Context) error {
|
||||
logger.Errorf("Error processing resource %s: %v", resource.URL, err)
|
||||
return fmt.Errorf("failed to process resource %s: %w", resource.URL, err)
|
||||
}
|
||||
t.downloaded.Add(1)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ type Task struct {
|
||||
downloadedBytes atomic.Int64 // downloaded bytes count
|
||||
processing map[string]ResourceInfo
|
||||
processingMu sync.RWMutex
|
||||
failed map[string]error // [TODO] errors for each resource
|
||||
}
|
||||
|
||||
// Title implements core.Exectable.
|
||||
@@ -84,6 +83,5 @@ func NewTask(
|
||||
progress: progressTracker,
|
||||
processing: make(map[string]ResourceInfo),
|
||||
processingMu: sync.RWMutex{},
|
||||
failed: make(map[string]error),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user