mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-30 12:46:41 +08:00
feat(core): persist tasks and recover them after restart
This commit is contained in:
+18
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user