Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race Conditions #11

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions application/services/job_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func (j *JobManager) Start() {
}

func (j *JobManager) notifySuccess(result JobWorkerResult) error {
Mutex.Lock()
jobJson, err := json.Marshal(result.Job)
Mutex.Unlock()

if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions application/services/job_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
uuid "github.com/satori/go.uuid"
"github.com/streadway/amqp"
"os"
"sync"
)

type JobWorkerResult struct {
Expand All @@ -16,15 +17,19 @@ type JobWorkerResult struct {
Error error
}

var Mutex = &sync.Mutex{}

func JobWorker(messageChannel chan amqp.Delivery, returnChan chan JobWorkerResult, jobService JobService) {
for message := range messageChannel {
if !utils.IsJson(string(message.Body)) {
returnChan <- returnJobResult(domain.Job{}, message, errors.New("message is not a json"))
continue
}

Mutex.Lock()
err := json.Unmarshal(message.Body, &jobService.VideoService.Video)
jobService.VideoService.Video.ID = uuid.NewV4().String()
Mutex.Unlock()

if err != nil {
returnChan <- returnJobResult(domain.Job{}, message, err)
Expand All @@ -37,7 +42,9 @@ func JobWorker(messageChannel chan amqp.Delivery, returnChan chan JobWorkerResul
continue
}

Mutex.Lock()
err = jobService.VideoService.InsertVideo()
Mutex.Unlock()

if err != nil {
returnChan <- returnJobResult(domain.Job{}, message, err)
Expand All @@ -51,7 +58,9 @@ func JobWorker(messageChannel chan amqp.Delivery, returnChan chan JobWorkerResul
continue
}

Mutex.Lock()
_, err = jobService.JobsRepository.Insert(job)
Mutex.Unlock()

if err != nil {
returnChan <- returnJobResult(domain.Job{}, message, err)
Expand Down