* refactor: a big refactor. wip * refactor: port handle file * refactor: place all handlers * fix: task info nil pointer * feat: enhance task progress tracking and context management * feat: cancel task * feat: stream mode * feat: silent mode * feat: dir cmd * refactor: remove unused old file * feat: rule cmd * feat: handle silent mode * feat: batch task * fix: batch task progress and temp file cleanup * refactor: update file creation and cleanup methods for better resource management * feat: add save command with silent mode handling * feat: message link * feat: update message prompts to include file count in storage selection * feat: slient save links * refactor: reduce dup code * feat: rule type * feat: chose dir * feat: refactor file handling and storage rules, improve error handling and logging * feat: rule mode * feat: telegraph pics * fix: tphpics nil pointer and inaccurate dirpath * feat: silent save telegraph * feat: add suffix to avoid file overwrite * feat: new storage telegram * chore: tidy go mod
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package recovery
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
"github.com/charmbracelet/log"
|
|
"github.com/go-faster/errors"
|
|
"github.com/gotd/td/bin"
|
|
"github.com/gotd/td/telegram"
|
|
"github.com/gotd/td/tg"
|
|
"github.com/gotd/td/tgerr"
|
|
)
|
|
|
|
type recovery struct {
|
|
ctx context.Context
|
|
backoff backoff.BackOff
|
|
}
|
|
|
|
func New(ctx context.Context, backoff backoff.BackOff) telegram.Middleware {
|
|
return &recovery{
|
|
ctx: ctx,
|
|
backoff: backoff,
|
|
}
|
|
}
|
|
|
|
func (r *recovery) Handle(next tg.Invoker) telegram.InvokeFunc {
|
|
return func(ctx context.Context, input bin.Encoder, output bin.Decoder) error {
|
|
|
|
return backoff.RetryNotify(func() error {
|
|
if err := next.Invoke(ctx, input, output); err != nil {
|
|
if r.shouldRecover(ctx, err) {
|
|
return errors.Wrap(err, "recover")
|
|
}
|
|
|
|
return backoff.Permanent(err)
|
|
}
|
|
|
|
return nil
|
|
}, r.backoff, func(err error, duration time.Duration) {
|
|
log.FromContext(ctx).Debug("Wait for connection recovery", "error", err, "duration", duration)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (r *recovery) shouldRecover(ctx context.Context, err error) bool {
|
|
// context in recovery is used to stop recovery process by external os signal, otherwise we will wait till max retries when user press ctrl+c
|
|
select {
|
|
case <-r.ctx.Done():
|
|
return false
|
|
case <-ctx.Done():
|
|
return false
|
|
default:
|
|
}
|
|
|
|
// we try recover when encountered any error that is not telegram business error
|
|
_, ok := tgerr.As(err)
|
|
|
|
return !ok
|
|
}
|