Added cleanup

This commit is contained in:
2021-12-24 17:43:07 +01:00
parent a8bd48e09f
commit ed4475149a
5 changed files with 95 additions and 1 deletions

View File

@@ -0,0 +1,35 @@
package cleanup
import (
"context"
"downloader/internal/core/ports/download_request"
"go.uber.org/zap"
"time"
)
type cleanUp struct {
repository download_request.Repository
logger *zap.SugaredLogger
}
func New(repository download_request.Repository, logger *zap.SugaredLogger) *cleanUp {
return &cleanUp{repository: repository, logger: logger}
}
func (c *cleanUp) RunOnSchedule() {
ctx := context.TODO()
go func() {
for true {
requests, err := c.repository.GetOldOrStuck(ctx)
if err == nil {
c.logger.Debugw("Cleaning up downloads",
"downloads", requests)
_ = c.repository.BatchDelete(ctx, requests)
} else {
c.logger.Warn("could not process old or stuck in-progress jobs")
}
time.Sleep(time.Minute)
}
}()
}