2021-12-24 17:43:07 +01:00
|
|
|
package cleanup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"downloader/internal/core/ports/download_request"
|
2021-12-26 00:02:51 +01:00
|
|
|
"downloader/internal/core/ports/fileorchestrator"
|
2021-12-24 17:43:07 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cleanUp struct {
|
|
|
|
repository download_request.Repository
|
|
|
|
logger *zap.SugaredLogger
|
2021-12-26 00:02:51 +01:00
|
|
|
fo *fileorchestrator.FileOrchestrator
|
2021-12-24 17:43:07 +01:00
|
|
|
}
|
|
|
|
|
2021-12-26 00:02:51 +01:00
|
|
|
func New(repository download_request.Repository, logger *zap.SugaredLogger, fo *fileorchestrator.FileOrchestrator) *cleanUp {
|
|
|
|
return &cleanUp{repository: repository, logger: logger, fo: fo}
|
2021-12-24 17:43:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cleanUp) RunOnSchedule() {
|
|
|
|
ctx := context.TODO()
|
|
|
|
go func() {
|
|
|
|
for true {
|
|
|
|
requests, err := c.repository.GetOldOrStuck(ctx)
|
2021-12-26 00:02:51 +01:00
|
|
|
if err != nil {
|
2021-12-24 17:43:07 +01:00
|
|
|
c.logger.Warn("could not process old or stuck in-progress jobs")
|
2021-12-26 00:02:51 +01:00
|
|
|
time.Sleep(5 * time.Minute)
|
|
|
|
continue
|
2021-12-24 17:43:07 +01:00
|
|
|
}
|
|
|
|
|
2021-12-26 00:02:51 +01:00
|
|
|
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)
|
|
|
|
|
2021-12-24 17:43:07 +01:00
|
|
|
time.Sleep(time.Minute)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|