mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-05 04:58:33 +02:00
Prepare using a context for export / import
This will allow cancelling the export if the import fails before the export finished. For now the context isn't passed on to the actual export functions, hence there is not yet any cancellation happening.
This commit is contained in:
parent
536fae6a46
commit
3b545a0fd6
3 changed files with 37 additions and 16 deletions
|
@ -18,6 +18,7 @@ Scotty. If not, see <https://www.gnu.org/licenses/>.
|
|||
package backends
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"go.uploadedlobster.com/scotty/internal/models"
|
||||
|
@ -25,7 +26,7 @@ import (
|
|||
|
||||
type ImportProcessor[T models.ListensResult | models.LovesResult] interface {
|
||||
ImportBackend() models.ImportBackend
|
||||
Process(wg *sync.WaitGroup, results chan T, out chan models.ImportResult, progress chan models.TransferProgress)
|
||||
Process(ctx context.Context, wg *sync.WaitGroup, results chan T, out chan models.ImportResult, progress chan models.TransferProgress)
|
||||
Import(export T, result models.ImportResult, out chan models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error)
|
||||
}
|
||||
|
||||
|
@ -37,8 +38,8 @@ func (p ListensImportProcessor) ImportBackend() models.ImportBackend {
|
|||
return p.Backend
|
||||
}
|
||||
|
||||
func (p ListensImportProcessor) Process(wg *sync.WaitGroup, results chan models.ListensResult, out chan models.ImportResult, progress chan models.TransferProgress) {
|
||||
process(wg, p, results, out, progress)
|
||||
func (p ListensImportProcessor) Process(ctx context.Context, wg *sync.WaitGroup, results chan models.ListensResult, out chan models.ImportResult, progress chan models.TransferProgress) {
|
||||
process(ctx, wg, p, results, out, progress)
|
||||
}
|
||||
|
||||
func (p ListensImportProcessor) Import(export models.ListensResult, result models.ImportResult, out chan models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error) {
|
||||
|
@ -66,8 +67,8 @@ func (p LovesImportProcessor) ImportBackend() models.ImportBackend {
|
|||
return p.Backend
|
||||
}
|
||||
|
||||
func (p LovesImportProcessor) Process(wg *sync.WaitGroup, results chan models.LovesResult, out chan models.ImportResult, progress chan models.TransferProgress) {
|
||||
process(wg, p, results, out, progress)
|
||||
func (p LovesImportProcessor) Process(ctx context.Context, wg *sync.WaitGroup, results chan models.LovesResult, out chan models.ImportResult, progress chan models.TransferProgress) {
|
||||
process(ctx, wg, p, results, out, progress)
|
||||
}
|
||||
|
||||
func (p LovesImportProcessor) Import(export models.LovesResult, result models.ImportResult, out chan models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error) {
|
||||
|
@ -87,7 +88,12 @@ func (p LovesImportProcessor) Import(export models.LovesResult, result models.Im
|
|||
return importResult, nil
|
||||
}
|
||||
|
||||
func process[R models.LovesResult | models.ListensResult, P ImportProcessor[R]](wg *sync.WaitGroup, processor P, results chan R, out chan models.ImportResult, progress chan models.TransferProgress) {
|
||||
func process[R models.LovesResult | models.ListensResult, P ImportProcessor[R]](
|
||||
ctx context.Context, wg *sync.WaitGroup,
|
||||
processor P, results chan R,
|
||||
out chan models.ImportResult,
|
||||
progress chan models.TransferProgress,
|
||||
) {
|
||||
wg.Add(1)
|
||||
defer wg.Done()
|
||||
defer close(out)
|
||||
|
@ -100,14 +106,21 @@ func process[R models.LovesResult | models.ListensResult, P ImportProcessor[R]](
|
|||
}
|
||||
|
||||
for exportResult := range results {
|
||||
importResult, err := processor.Import(exportResult, result, out, progress)
|
||||
result.Update(importResult)
|
||||
if err != nil {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
processor.ImportBackend().FinishImport()
|
||||
out <- handleError(result, err, progress)
|
||||
out <- handleError(result, ctx.Err(), progress)
|
||||
return
|
||||
default:
|
||||
importResult, err := processor.Import(exportResult, result, out, progress)
|
||||
result.Update(importResult)
|
||||
if err != nil {
|
||||
processor.ImportBackend().FinishImport()
|
||||
out <- handleError(result, err, progress)
|
||||
return
|
||||
}
|
||||
progress <- p.FromImportResult(result, false)
|
||||
}
|
||||
progress <- p.FromImportResult(result, false)
|
||||
}
|
||||
|
||||
if err := processor.ImportBackend().FinishImport(); err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue