mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-09-04 23:26:39 +08:00
fix(core): recover task state correctly on restart
This commit is contained in:
@@ -6,11 +6,17 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/core"
|
||||
"github.com/krau/SaveAny-Bot/pkg/enums/ctxkey"
|
||||
"github.com/krau/SaveAny-Bot/pkg/enums/tasktype"
|
||||
"github.com/krau/SaveAny-Bot/pkg/storagetypes"
|
||||
"github.com/krau/SaveAny-Bot/storage"
|
||||
)
|
||||
|
||||
func ctxOverwrite(ctx context.Context) bool {
|
||||
overwrite, _ := ctx.Value(ctxkey.OverwriteExisting).(bool)
|
||||
return overwrite
|
||||
}
|
||||
|
||||
type elementPayload struct {
|
||||
ID string `json:"id"`
|
||||
SourceStorage string `json:"source_storage"`
|
||||
@@ -26,6 +32,7 @@ type taskPayload struct {
|
||||
ChatID int64 `json:"chat_id"`
|
||||
MessageID int `json:"message_id"`
|
||||
IgnoreErrors bool `json:"ignore_errors"`
|
||||
Overwrite bool `json:"overwrite"`
|
||||
}
|
||||
|
||||
type taskCodec struct{}
|
||||
@@ -42,6 +49,7 @@ func (taskCodec) Marshal(task core.Executable) ([]byte, error) {
|
||||
p := taskPayload{
|
||||
ID: t.ID,
|
||||
IgnoreErrors: t.IgnoreErrors,
|
||||
Overwrite: ctxOverwrite(t.ctx),
|
||||
}
|
||||
for _, elem := range t.elems {
|
||||
p.Elements = append(p.Elements, elementPayload{
|
||||
@@ -88,5 +96,7 @@ func (taskCodec) Unmarshal(data []byte) (core.Executable, error) {
|
||||
if p.ChatID != 0 {
|
||||
progress = NewProgressTracker(p.MessageID, p.ChatID)
|
||||
}
|
||||
return NewTransferTask(p.ID, context.Background(), elems, progress, p.IgnoreErrors), nil
|
||||
task := NewTransferTask(p.ID, context.Background(), elems, progress, p.IgnoreErrors)
|
||||
task.overwrite = p.Overwrite
|
||||
return task, nil
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ import (
|
||||
func (t *Task) Execute(ctx context.Context) error {
|
||||
logger := log.FromContext(ctx).WithPrefix(fmt.Sprintf("transfer[%s]", t.ID))
|
||||
logger.Info("Starting transfer task")
|
||||
if t.overwrite {
|
||||
ctx = storage.WithOverwrite(ctx)
|
||||
}
|
||||
if t.Progress != nil {
|
||||
t.Progress.OnStart(ctx, t)
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ type Task struct {
|
||||
processing map[string]TaskElementInfo
|
||||
processingMu sync.RWMutex
|
||||
failed map[string]error
|
||||
overwrite bool // recovered: overwrite storage targets instead of uniquifying
|
||||
}
|
||||
|
||||
// Title implements core.Executable.
|
||||
|
||||
Reference in New Issue
Block a user