1 parent
a69bb6e3c3
commit
e9f1e9d0f5
2 files changed
+23
-6
No files matched your search
@@ -117,13 +117,8 @@ func (s *Service) runTaskAsync(task *domain.StrmTask) {
|
||||
s.clearTaskRunState(task.ID, task.AccountID)
|
||||
s.mu.Unlock()
|
||||
}()
|
||||
if parent == nil {
|
||||
parent = context.Background()
|
||||
}
|
||||
runCtx, cancel := context.WithCancel(parent)
|
||||
runCtx, cancel := taskRunContext(parent)
|
||||
defer cancel()
|
||||
runCtx, timeoutCancel := context.WithTimeout(runCtx, 30*time.Minute)
|
||||
defer timeoutCancel()
|
||||
s.mu.Lock()
|
||||
s.taskCancels[task.ID] = cancel
|
||||
s.mu.Unlock()
|
||||
@@ -206,6 +201,13 @@ func (s *Service) runTaskAsync(task *domain.StrmTask) {
|
||||
}()
|
||||
}
|
||||
|
||||
func taskRunContext(parent context.Context) (context.Context, context.CancelFunc) {
|
||||
if parent == nil {
|
||||
parent = context.Background()
|
||||
}
|
||||
return context.WithCancel(parent)
|
||||
}
|
||||
|
||||
func (s *Service) updateScanPersist(taskID int64, patch domain.StrmScanPatch) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -2,6 +2,7 @@ package strm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"sync"
|
||||
@@ -77,3 +78,17 @@ func TestShouldRunCrossBusyCheckNoDeadlock(t *testing.T) {
|
||||
t.Fatal("shouldRun cross busy check deadlocked")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskRunContextHasNoFixedDeadline(t *testing.T) {
|
||||
ctx, cancel := taskRunContext(context.Background())
|
||||
if _, ok := ctx.Deadline(); ok {
|
||||
cancel()
|
||||
t.Fatal("STRM 任务不应有固定执行期限")
|
||||
}
|
||||
|
||||
cancel()
|
||||
<-ctx.Done()
|
||||
if !errors.Is(ctx.Err(), context.Canceled) {
|
||||
t.Fatalf("取消任务后错误 = %v,期望 context.Canceled", ctx.Err())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user