Add db
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package download
|
||||
|
||||
import "downloader/internal/core/entities"
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/entities"
|
||||
)
|
||||
|
||||
type BackgroundService interface {
|
||||
Run(download *entities.Download) error
|
||||
Run(ctx context.Context, download *entities.Download) error
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package _default
|
||||
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/entities"
|
||||
"downloader/internal/core/ports/download_request"
|
||||
"downloader/internal/core/services/download"
|
||||
@@ -18,12 +19,13 @@ func NewLocalBackgroundService(repository download_request.Repository, logger *z
|
||||
return &localBackgroundService{repository: repository, logger: logger, downloader: downloader}
|
||||
}
|
||||
|
||||
func (l localBackgroundService) Run(download *entities.Download) error {
|
||||
func (l localBackgroundService) Run(ctx context.Context, download *entities.Download) error {
|
||||
logger := l.logger.With("downloadId", download.ID)
|
||||
|
||||
go func() {
|
||||
longRunningCtx := context.TODO()
|
||||
download.Status = "started"
|
||||
_ = l.repository.Update(download)
|
||||
_ = l.repository.Update(longRunningCtx, download)
|
||||
|
||||
err := l.downloader.Download(download.Link, download.ID)
|
||||
download.Status = "done"
|
||||
@@ -33,13 +35,13 @@ func (l localBackgroundService) Run(download *entities.Download) error {
|
||||
download.Status = "failed"
|
||||
}
|
||||
|
||||
err = l.repository.Update(download)
|
||||
err = l.repository.Update(longRunningCtx, download)
|
||||
if err != nil {
|
||||
logger.Errorw("download request failed",
|
||||
"downloadLink", download.Link)
|
||||
download.Status = "failed"
|
||||
|
||||
if updateErr := l.repository.Update(download); updateErr != nil {
|
||||
if updateErr := l.repository.Update(longRunningCtx, download); updateErr != nil {
|
||||
panic(updateErr)
|
||||
}
|
||||
} else {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package _default
|
||||
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/entities"
|
||||
"downloader/internal/core/ports/download_request"
|
||||
"downloader/internal/core/services/download"
|
||||
@@ -24,7 +25,7 @@ func NewLocalService(repository download_request.Repository, uuidGen uuid.Gen, b
|
||||
}
|
||||
}
|
||||
|
||||
func (l *localService) Schedule(link string) (*entities.Download, error) {
|
||||
func (l *localService) Schedule(ctx context.Context, link string) (*entities.Download, error) {
|
||||
download, err := entities.NewDownload(link)(l.uuidGen)
|
||||
if err != nil {
|
||||
l.logger.Warn("Could not parse download")
|
||||
@@ -33,13 +34,13 @@ func (l *localService) Schedule(link string) (*entities.Download, error) {
|
||||
|
||||
logger := l.logger.With("downloadId", download.ID)
|
||||
|
||||
persistedDownloadRequest, uploadErr := l.repository.Create(download)
|
||||
persistedDownloadRequest, uploadErr := l.repository.Create(ctx, download)
|
||||
if uploadErr != nil {
|
||||
logger.Error("failed to insert download request")
|
||||
return nil, uploadErr
|
||||
}
|
||||
|
||||
err = l.BackgroundService.Run(persistedDownloadRequest)
|
||||
err = l.BackgroundService.Run(ctx, persistedDownloadRequest)
|
||||
if err != nil {
|
||||
logger.Error("failed to run download request")
|
||||
return nil, err
|
||||
@@ -48,10 +49,10 @@ func (l *localService) Schedule(link string) (*entities.Download, error) {
|
||||
return persistedDownloadRequest, nil
|
||||
}
|
||||
|
||||
func (l *localService) Get(id string) (*entities.Download, error) {
|
||||
return l.repository.GetById(id)
|
||||
func (l *localService) Get(ctx context.Context, id string) (*entities.Download, error) {
|
||||
return l.repository.GetById(ctx, id)
|
||||
}
|
||||
|
||||
func (l *localService) GetAll(active bool) ([]*entities.Download, error) {
|
||||
return l.repository.Get(active)
|
||||
func (l *localService) GetAll(ctx context.Context, active bool) ([]*entities.Download, error) {
|
||||
return l.repository.Get(ctx, active)
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/ports/download_request"
|
||||
"downloader/internal/core/ports/downloadhandler"
|
||||
"fmt"
|
||||
@@ -20,7 +21,7 @@ func New(repository download_request.Repository, logger *zap.SugaredLogger) down
|
||||
}
|
||||
|
||||
func (o *onDownloadEventHandler) OnTickEvent(downloadId string, progress string) {
|
||||
download, err := o.repository.GetById(downloadId)
|
||||
download, err := o.repository.GetById(context.TODO(), downloadId)
|
||||
if err != nil {
|
||||
o.logger.Warnw("could not finish updating progress as not download id available",
|
||||
"downloadId", downloadId,
|
||||
@@ -28,5 +29,5 @@ func (o *onDownloadEventHandler) OnTickEvent(downloadId string, progress string)
|
||||
return
|
||||
}
|
||||
download.Status = fmt.Sprintf("in-progress: %s", progress)
|
||||
_ = o.repository.Update(download)
|
||||
_ = o.repository.Update(context.TODO(), download)
|
||||
}
|
||||
|
@@ -1,9 +1,12 @@
|
||||
package download
|
||||
|
||||
import "downloader/internal/core/entities"
|
||||
import (
|
||||
"context"
|
||||
"downloader/internal/core/entities"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
Schedule(link string) (*entities.Download, error)
|
||||
Get(id string) (*entities.Download, error)
|
||||
GetAll(active bool) ([]*entities.Download, error)
|
||||
Schedule(ctx context.Context, link string) (*entities.Download, error)
|
||||
Get(ctx context.Context, id string) (*entities.Download, error)
|
||||
GetAll(ctx context.Context, active bool) ([]*entities.Download, error)
|
||||
}
|
||||
|
Reference in New Issue
Block a user