feat(core): persist tasks and recover them after restart

This commit is contained in:
krau
2026-08-25 15:05:55 +08:00
parent 54dc4caafe
commit 991f454096
14 changed files with 938 additions and 68 deletions
+18
View File
@@ -10,6 +10,7 @@ import (
"slices"
"github.com/charmbracelet/log"
"github.com/gotd/td/telegram/downloader"
"github.com/krau/SaveAny-Bot/api"
"github.com/krau/SaveAny-Bot/client/bot"
userclient "github.com/krau/SaveAny-Bot/client/user"
@@ -56,6 +57,14 @@ func Run(cmd *cobra.Command, _ []string) {
cancel()
}()
core.SetDownloaderProvider(func() downloader.Client {
if ectx := bot.ExtContext(); ectx != nil {
return ectx.Raw
}
return nil
})
core.RecoverTasks(ctx)
core.Run(ctx)
<-ctx.Done()
@@ -102,6 +111,15 @@ func cleanCache() {
log.Error("Invalid cache directory", "path", config.C().Temp.BasePath)
return
}
unfinished, err := database.CountUnfinishedTasks(context.Background())
if err != nil {
log.Error("Failed to count unfinished tasks, skipping cache cleanup", "error", err)
return
}
if unfinished > 0 {
log.Info("Skipping cache cleanup: unfinished tasks need their cache files for recovery", "tasks", unfinished)
return
}
currentDir, err := os.Getwd()
if err != nil {
log.Error("Failed to get working directory", "error", err)