refactor: use new RetryableTask abstraction and move task function as a private method
This commit is contained in:
parent
1af1ea9505
commit
2a067102da
|
@ -17,7 +17,6 @@ import (
|
||||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||||
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||||
"github.com/go-co-op/gocron/v2"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
tusd "github.com/tus/tusd/v2/pkg/handler"
|
tusd "github.com/tus/tusd/v2/pkg/handler"
|
||||||
|
@ -383,7 +382,7 @@ func (s *StorageServiceImpl) tusWorker() {
|
||||||
s.logger.Error("Could not complete tus upload", zap.Error(err))
|
s.logger.Error("Could not complete tus upload", zap.Error(err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err = s.ScheduleTusUpload(info.Upload.ID, 0)
|
err = s.ScheduleTusUpload(info.Upload.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Could not schedule tus upload", zap.Error(err))
|
s.logger.Error("Could not schedule tus upload", zap.Error(err))
|
||||||
continue
|
continue
|
||||||
|
@ -466,7 +465,7 @@ func (s *StorageServiceImpl) DeleteTusUpload(uploadID string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) error {
|
func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string) error {
|
||||||
find := &models.TusUpload{UploadID: uploadID}
|
find := &models.TusUpload{UploadID: uploadID}
|
||||||
|
|
||||||
var upload models.TusUpload
|
var upload models.TusUpload
|
||||||
|
@ -476,26 +475,22 @@ func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) err
|
||||||
return errors.New("upload not found")
|
return errors.New("upload not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
job, task := s.buildNewTusUploadTask(&upload)
|
task := s.cron.RetryableTask(cron.RetryableTaskParams{
|
||||||
|
Name: "tusUpload",
|
||||||
if attempt > 0 {
|
Function: s.tusUploadTask,
|
||||||
job = gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(time.Now().Add(time.Duration(attempt) * time.Minute)))
|
Args: []interface{}{&upload},
|
||||||
}
|
Attempt: 0,
|
||||||
|
Limit: 0,
|
||||||
_, err := s.cron.Scheduler().NewJob(job, task, gocron.WithEventListeners(gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {
|
After: func(jobID uuid.UUID, jobName string) {
|
||||||
s.logger.Error("Error running job", zap.Error(err))
|
|
||||||
err = s.ScheduleTusUpload(uploadID, attempt+1)
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Error("Error rescheduling job", zap.Error(err))
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
gocron.AfterJobRuns(func(jobID uuid.UUID, jobName string) {
|
|
||||||
s.logger.Info("Job finished", zap.String("jobName", jobName), zap.String("uploadID", uploadID))
|
s.logger.Info("Job finished", zap.String("jobName", jobName), zap.String("uploadID", uploadID))
|
||||||
err := s.DeleteTusUpload(uploadID)
|
err := s.DeleteTusUpload(uploadID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Error deleting tus upload", zap.Error(err))
|
s.logger.Error("Error deleting tus upload", zap.Error(err))
|
||||||
}
|
}
|
||||||
})))
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
_, err := s.cron.CreateJob(task)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -503,11 +498,7 @@ func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) err
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (job gocron.JobDefinition, task gocron.Task) {
|
func (s *StorageServiceImpl) tusUploadTask(upload *models.TusUpload) error {
|
||||||
job = gocron.OneTimeJob(gocron.OneTimeJobStartImmediately())
|
|
||||||
|
|
||||||
task = gocron.NewTask(
|
|
||||||
func(upload *models.TusUpload) error {
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tusUpload, err := s.tusStore.GetUpload(ctx, upload.UploadID)
|
tusUpload, err := s.tusStore.GetUpload(ctx, upload.UploadID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -612,9 +603,6 @@ func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (jo
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}, upload)
|
|
||||||
|
|
||||||
return job, task
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StorageServiceImpl) getPrefixedHash(hash []byte) []byte {
|
func (s *StorageServiceImpl) getPrefixedHash(hash []byte) []byte {
|
||||||
|
|
Loading…
Reference in New Issue