Skip to content

Commit

Permalink
feat: add JobService
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Martins dos Santos committed Jan 21, 2024
1 parent 42f7d52 commit 3a22a8e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
117 changes: 117 additions & 0 deletions application/services/job_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package services

import (
"errors"
"github.com/olukkas/go-encoder/application/repositories"
"github.com/olukkas/go-encoder/domain"
"os"
"strconv"
)

type JobService struct {
Job *domain.Job
JobsRepository repositories.JobRepository
VideoService VideoService
}

func (j *JobService) Start() error {
err := j.changeJobStatus(domain.JobDownloading)
if err != nil {
return j.failJob(err)
}

err = j.VideoService.Download(os.Getenv("INPUT_BUCKET_NAME"))
if err != nil {
return j.failJob(err)
}

err = j.changeJobStatus(domain.JobFragmenting)
if err != nil {
return j.failJob(err)
}

err = j.VideoService.Fragment()
if err != nil {
return j.failJob(err)
}

err = j.changeJobStatus(domain.JobEncoding)
if err != nil {
return j.failJob(err)
}

err = j.VideoService.Encode()
if err != nil {
return j.failJob(err)
}

err = j.changeJobStatus(domain.JobFinishing)
if err != nil {
return j.failJob(err)
}

err = j.performUpload()
if err != nil {
return j.failJob(err)
}

err = j.VideoService.Finish()
if err != nil {
return j.failJob(err)
}

err = j.changeJobStatus(domain.JobCompleted)
if err != nil {
return j.failJob(err)
}

return nil
}

func (j *JobService) performUpload() error {
err := j.changeJobStatus(domain.JobUploading)
if err != nil {
return j.failJob(err)
}

vu := NewVideoUpload()
vu.OutputBucket = os.Getenv("OUTPUT_BUCKET_NAME")
vu.VideoPath = os.Getenv("LOCAL_STORAGE_PATH") + "/" + j.VideoService.Video.ID
concurrency, _ := strconv.Atoi(os.Getenv("CONCURRENCY_UPLOAD"))
doneUpload := make(chan string)

//goland:noinspection GoUnhandledErrorResult
go vu.ProcessUpload(concurrency, doneUpload)

uploadResult := <-doneUpload

if uploadResult != "upload completed" {
return j.failJob(errors.New(uploadResult))
}

return nil
}

func (j *JobService) changeJobStatus(status domain.JobStatus) error {
var err error

j.Job.Status = status
j.Job, err = j.JobsRepository.Update(j.Job)
if err != nil {
return j.failJob(err)
}

return nil
}

func (j *JobService) failJob(error error) error {
j.Job.Status = domain.JobFailed
j.Job.Error = error.Error()

_, err := j.JobsRepository.Update(j.Job)
if error != nil {
return err
}

return error
}
6 changes: 6 additions & 0 deletions domain/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type JobStatus string
const (
JobDownloading JobStatus = "DOWNLOADING"
JobPending JobStatus = "PENDING"
JobFailed JobStatus = "FAILED"
JobFragmenting JobStatus = "FRAGMENTING"
JobEncoding JobStatus = "ENCODING"
JobFinishing JobStatus = "FINISHING"
JobUploading JobStatus = "UPLOADING"
JobCompleted JobStatus = "COMPLETED"
)

type Job struct {
Expand Down

0 comments on commit 3a22a8e

Please sign in to comment.