51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package cleanup
|
|
|
|
import (
|
|
"context"
|
|
"downloader/internal/core/ports/download_request"
|
|
"downloader/internal/core/ports/fileorchestrator"
|
|
"go.uber.org/zap"
|
|
"time"
|
|
)
|
|
|
|
type cleanUp struct {
|
|
repository download_request.Repository
|
|
logger *zap.SugaredLogger
|
|
fo *fileorchestrator.FileOrchestrator
|
|
}
|
|
|
|
func New(repository download_request.Repository, logger *zap.SugaredLogger, fo *fileorchestrator.FileOrchestrator) *cleanUp {
|
|
return &cleanUp{repository: repository, logger: logger, fo: fo}
|
|
}
|
|
|
|
func (c *cleanUp) RunOnSchedule() {
|
|
ctx := context.TODO()
|
|
go func() {
|
|
for true {
|
|
requests, err := c.repository.GetOldOrStuck(ctx)
|
|
if err != nil {
|
|
c.logger.Warn("could not process old or stuck in-progress jobs")
|
|
time.Sleep(5 * time.Minute)
|
|
continue
|
|
}
|
|
|
|
c.logger.Debugw("Cleaning up downloads",
|
|
"downloads", requests)
|
|
|
|
for _, request := range requests {
|
|
basePath, err := c.fo.Begin(request.Link)
|
|
if err != nil {
|
|
c.logger.Warnw("could not process request",
|
|
"downloadId", request.ID)
|
|
continue
|
|
}
|
|
|
|
c.fo.CleanUp(basePath)
|
|
}
|
|
_ = c.repository.BatchDelete(ctx, requests)
|
|
|
|
time.Sleep(time.Minute)
|
|
}
|
|
}()
|
|
}
|