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/service/s3"
|
||||
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
"github.com/go-co-op/gocron/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/viper"
|
||||
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))
|
||||
continue
|
||||
}
|
||||
err = s.ScheduleTusUpload(info.Upload.ID, 0)
|
||||
err = s.ScheduleTusUpload(info.Upload.ID)
|
||||
if err != nil {
|
||||
s.logger.Error("Could not schedule tus upload", zap.Error(err))
|
||||
continue
|
||||
|
@ -466,7 +465,7 @@ func (s *StorageServiceImpl) DeleteTusUpload(uploadID string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) error {
|
||||
func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string) error {
|
||||
find := &models.TusUpload{UploadID: uploadID}
|
||||
|
||||
var upload models.TusUpload
|
||||
|
@ -476,26 +475,22 @@ func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) err
|
|||
return errors.New("upload not found")
|
||||
}
|
||||
|
||||
job, task := s.buildNewTusUploadTask(&upload)
|
||||
|
||||
if attempt > 0 {
|
||||
job = gocron.OneTimeJob(gocron.OneTimeJobStartDateTime(time.Now().Add(time.Duration(attempt) * time.Minute)))
|
||||
}
|
||||
|
||||
_, err := s.cron.Scheduler().NewJob(job, task, gocron.WithEventListeners(gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {
|
||||
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) {
|
||||
task := s.cron.RetryableTask(cron.RetryableTaskParams{
|
||||
Name: "tusUpload",
|
||||
Function: s.tusUploadTask,
|
||||
Args: []interface{}{&upload},
|
||||
Attempt: 0,
|
||||
Limit: 0,
|
||||
After: func(jobID uuid.UUID, jobName string) {
|
||||
s.logger.Info("Job finished", zap.String("jobName", jobName), zap.String("uploadID", uploadID))
|
||||
err := s.DeleteTusUpload(uploadID)
|
||||
if err != nil {
|
||||
s.logger.Error("Error deleting tus upload", zap.Error(err))
|
||||
}
|
||||
})))
|
||||
},
|
||||
})
|
||||
|
||||
_, err := s.cron.CreateJob(task)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -503,11 +498,7 @@ func (s *StorageServiceImpl) ScheduleTusUpload(uploadID string, attempt int) err
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (job gocron.JobDefinition, task gocron.Task) {
|
||||
job = gocron.OneTimeJob(gocron.OneTimeJobStartImmediately())
|
||||
|
||||
task = gocron.NewTask(
|
||||
func(upload *models.TusUpload) error {
|
||||
func (s *StorageServiceImpl) tusUploadTask(upload *models.TusUpload) error {
|
||||
ctx := context.Background()
|
||||
tusUpload, err := s.tusStore.GetUpload(ctx, upload.UploadID)
|
||||
if err != nil {
|
||||
|
@ -612,9 +603,6 @@ func (s *StorageServiceImpl) buildNewTusUploadTask(upload *models.TusUpload) (jo
|
|||
}
|
||||
|
||||
return nil
|
||||
}, upload)
|
||||
|
||||
return job, task
|
||||
}
|
||||
|
||||
func (s *StorageServiceImpl) getPrefixedHash(hash []byte) []byte {
|
||||
|
|
Loading…
Reference in New Issue