Skip to content

Commit

Permalink
Fix race condition (#552)
Browse files Browse the repository at this point in the history
* Avoid data race
  • Loading branch information
yalosev authored Nov 9, 2023
1 parent 4d76b7a commit fc5d159
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package task

import (
"fmt"
"sync"
"time"

uuid "github.com/gofrs/uuid/v5"
Expand Down Expand Up @@ -41,8 +42,10 @@ type BaseTask struct {
QueueName string
QueuedAt time.Time

Props map[string]interface{}

lock sync.RWMutex
Metadata interface{}
Props map[string]interface{}
}

func NewTask(taskType TaskType) *BaseTask {
Expand All @@ -67,7 +70,9 @@ func (t *BaseTask) WithQueueName(name string) *BaseTask {
}

func (t *BaseTask) WithMetadata(metadata interface{}) *BaseTask {
t.lock.Lock()
t.Metadata = metadata
t.lock.Unlock()
return t
}

Expand Down Expand Up @@ -97,11 +102,15 @@ func (t *BaseTask) WithQueuedAt(queuedAt time.Time) Task {
}

func (t *BaseTask) GetMetadata() interface{} {
t.lock.RLock()
defer t.lock.RUnlock()
return t.Metadata
}

func (t *BaseTask) UpdateMetadata(meta interface{}) {
t.lock.Lock()
t.Metadata = meta
t.lock.Unlock()
}

func (t *BaseTask) GetProp(key string) interface{} {
Expand All @@ -126,9 +135,11 @@ func (t *BaseTask) UpdateFailureMessage(msg string) {

func (t *BaseTask) GetDescription() string {
metaDescription := ""
t.lock.RLock()
if descriptor, ok := t.Metadata.(MetadataDescriptable); ok {
metaDescription = ":" + descriptor.GetDescription()
}
t.lock.RUnlock()
failDescription := ""
if t.FailureCount > 0 {
failDescription = fmt.Sprintf(":failures %d", t.FailureCount)
Expand Down

0 comments on commit fc5d159

Please sign in to comment.