Pass context to export backends

This commit is contained in:
Philipp Wolfer 2025-05-22 11:09:39 +02:00
parent b5bca1d4ab
commit 26d9f5e840
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
12 changed files with 55 additions and 33 deletions

View file

@ -18,6 +18,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
package spotifyhistory
import (
"context"
"os"
"path"
"path/filepath"
@ -72,7 +73,7 @@ func (b *SpotifyHistoryBackend) InitConfig(config *config.ServiceConfig) error {
return nil
}
func (b *SpotifyHistoryBackend) ExportListens(oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
func (b *SpotifyHistoryBackend) ExportListens(ctx context.Context, oldestTimestamp time.Time, results chan models.ListensResult, progress chan models.TransferProgress) {
files, err := filepath.Glob(path.Join(b.dirPath, historyFileGlob))
p := models.TransferProgress{
Export: &models.Progress{},
@ -89,11 +90,20 @@ func (b *SpotifyHistoryBackend) ExportListens(oldestTimestamp time.Time, results
fileCount := int64(len(files))
p.Export.Total = fileCount
for i, filePath := range files {
history, err := readHistoryFile(filePath)
if err != nil {
select {
case <-ctx.Done():
results <- models.ListensResult{Error: ctx.Err()}
p.Export.Abort()
progress <- p
return
default:
}
history, err := readHistoryFile(filePath)
if err != nil {
results <- models.ListensResult{Error: err}
p.Export.Abort()
progress <- p
return
}
listens := history.AsListenList(ListenListOptions{