summaryrefslogtreecommitdiff
path: root/recording/task.go
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-09-14 03:02:51 +0800
committerKeuin <[email protected]>2022-09-14 03:04:52 +0800
commit7902849dc021610b0ec16d1130b9515efd1f64b1 (patch)
tree058c1aca3a4947f75b0ba73f6f205498a1e186e5 /recording/task.go
parented3db79b86e9ea2c796cccedf1454a45823293a4 (diff)
Refactor: proper error handling.v0.3.3
Diffstat (limited to 'recording/task.go')
-rw-r--r--recording/task.go34
1 files changed, 31 insertions, 3 deletions
diff --git a/recording/task.go b/recording/task.go
index 3d417ba..78be348 100644
--- a/recording/task.go
+++ b/recording/task.go
@@ -8,7 +8,9 @@ Concrete task works are done in the `runner.go` file.
import (
"context"
"fmt"
+ "github.com/keuin/slbr/common"
"github.com/keuin/slbr/logging"
+ "time"
)
type TaskStatus int
@@ -31,8 +33,6 @@ type RunningTask struct {
TaskConfig
// ctx: the biggest context this task uses. It may create children contexts.
ctx context.Context
- // result: if the task is ended, here is the returned error
- result error
// status: running status
status TaskStatus
// hookStarted: called asynchronously when the task is started. This won't be called when restarting.
@@ -70,7 +70,7 @@ func (t *RunningTask) StartTask() error {
t.hookStarted()
defer t.hookStopped()
// do the task
- _ = t.runTaskWithAutoRestart()
+ t.runTaskWithAutoRestart()
}()
return nil
case StRunning:
@@ -85,3 +85,31 @@ func (t *RunningTask) StartTask() error {
}
panic(fmt.Errorf("invalid task status: %v", st))
}
+
+func AutoRetryWithTask[T any](
+ t *RunningTask,
+ supplier func() (T, error),
+) (T, error) {
+ return common.AutoRetry[T](
+ t.ctx,
+ supplier,
+ t.Transport.MaxRetryTimes,
+ time.Duration(t.Transport.RetryIntervalSeconds)*time.Second,
+ &t.logger,
+ )
+}
+
+func AutoRetryWithConfig[T any](
+ ctx context.Context,
+ logger logging.Logger,
+ t *TaskConfig,
+ supplier func() (T, error),
+) (T, error) {
+ return common.AutoRetry[T](
+ ctx,
+ supplier,
+ t.Transport.MaxRetryTimes,
+ time.Duration(t.Transport.RetryIntervalSeconds)*time.Second,
+ &logger,
+ )
+}