Skip to content

Commit 1993eef

Browse files
authored
Merge pull request #100 from skip-mev/mergify/bp/release/v1.x.x/pr-99
fix(core/provider): add RWMutex to task operations (backport #99)
2 parents 2f025de + 34aba5e commit 1993eef

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

core/provider/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"go.uber.org/zap"
6+
"sync"
67
)
78

89
// TaskStatus defines the status of a task's underlying workload
@@ -23,8 +24,8 @@ type Task struct {
2324
Definition TaskDefinition
2425
Sidecars []*Task
2526

26-
logger *zap.Logger
27-
27+
logger *zap.Logger
28+
mu sync.RWMutex
2829
PreStart func(context.Context, *Task) error
2930
PostStop func(context.Context, *Task) error
3031
}

core/provider/task.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ import (
44
"context"
55
"errors"
66
"go.uber.org/zap"
7+
"sync"
78
)
89

910
// CreateTask creates a task structure and sets up its underlying workload on a provider, including sidecars if there are any in the definition
1011
func CreateTask(ctx context.Context, logger *zap.Logger, provider Provider, definition TaskDefinition) (*Task, error) {
1112
task := &Task{
1213
Provider: provider,
1314
Definition: definition,
15+
mu: sync.RWMutex{},
1416
logger: logger.Named("task"),
1517
}
1618

19+
task.mu.Lock()
20+
defer task.mu.Unlock()
21+
1722
sidecarTasks := make([]*Task, 0)
1823

1924
for _, sidecar := range definition.Sidecars {
@@ -49,6 +54,9 @@ func CreateTask(ctx context.Context, logger *zap.Logger, provider Provider, defi
4954

5055
// Start starts the underlying task's workload including its sidecars if startSidecars is set to true
5156
func (t *Task) Start(ctx context.Context, startSidecars bool) error {
57+
t.mu.Lock()
58+
defer t.mu.Unlock()
59+
5260
if startSidecars {
5361
for _, sidecar := range t.Sidecars {
5462
err := sidecar.Start(ctx, startSidecars)
@@ -75,6 +83,9 @@ func (t *Task) Start(ctx context.Context, startSidecars bool) error {
7583

7684
// Stop stops the underlying task's workload including its sidecars if stopSidecars is set to true
7785
func (t *Task) Stop(ctx context.Context, stopSidecars bool) error {
86+
t.mu.Lock()
87+
defer t.mu.Unlock()
88+
7889
if stopSidecars {
7990
for _, sidecar := range t.Sidecars {
8091
err := sidecar.Stop(ctx, stopSidecars)
@@ -102,27 +113,42 @@ func (t *Task) Stop(ctx context.Context, stopSidecars bool) error {
102113

103114
// WriteFile writes to a file in the task's volume at a relative path
104115
func (t *Task) WriteFile(ctx context.Context, path string, bz []byte) error {
116+
t.mu.Lock()
117+
defer t.mu.Unlock()
118+
105119
return t.Provider.WriteFile(ctx, t.ID, path, bz)
106120
}
107121

108122
// ReadFile returns a file's contents in the task's volume at a relative path
109123
func (t *Task) ReadFile(ctx context.Context, path string) ([]byte, error) {
124+
t.mu.RLock()
125+
defer t.mu.RUnlock()
126+
110127
return t.Provider.ReadFile(ctx, t.ID, path)
111128
}
112129

113130
// DownloadDir downloads a directory from the task's volume at path relPath to a local path localPath
114131
func (t *Task) DownloadDir(ctx context.Context, relPath, localPath string) error {
132+
t.mu.RLock()
133+
defer t.mu.RUnlock()
134+
115135
return t.Provider.DownloadDir(ctx, t.ID, relPath, localPath)
116136
}
117137

118138
// GetIP returns the task's IP
119139
func (t *Task) GetIP(ctx context.Context) (string, error) {
140+
t.mu.RLock()
141+
defer t.mu.RUnlock()
142+
120143
return t.Provider.GetIP(ctx, t.ID)
121144
}
122145

123146
// GetExternalAddress returns the external address for a specific task port in format host:port.
124147
// Providers choose the protocol to return the port for themselves.
125148
func (t *Task) GetExternalAddress(ctx context.Context, port string) (string, error) {
149+
t.mu.RLock()
150+
defer t.mu.RUnlock()
151+
126152
return t.Provider.GetExternalAddress(ctx, t.ID, port)
127153
}
128154

@@ -134,19 +160,30 @@ func (t *Task) RunCommand(ctx context.Context, command []string) (string, string
134160
}
135161

136162
if status == TASK_RUNNING {
163+
t.mu.Lock()
164+
defer t.mu.Unlock()
137165
return t.Provider.RunCommand(ctx, t.ID, command)
138166
}
139167

168+
t.mu.Lock()
169+
defer t.mu.Unlock()
170+
140171
return t.Provider.RunCommandWhileStopped(ctx, t.ID, t.Definition, command)
141172
}
142173

143174
// GetStatus returns the task's underlying workload's status
144175
func (t *Task) GetStatus(ctx context.Context) (TaskStatus, error) {
176+
t.mu.RLock()
177+
defer t.mu.RUnlock()
178+
145179
return t.Provider.GetTaskStatus(ctx, t.ID)
146180
}
147181

148182
// Destroy destroys the task's underlying workload, including it's sidecars if destroySidecars is set to true
149183
func (t *Task) Destroy(ctx context.Context, destroySidecars bool) error {
184+
t.mu.Lock()
185+
defer t.mu.Unlock()
186+
150187
if destroySidecars {
151188
for _, sidecar := range t.Sidecars {
152189
err := sidecar.Destroy(ctx, destroySidecars)
@@ -166,10 +203,16 @@ func (t *Task) Destroy(ctx context.Context, destroySidecars bool) error {
166203

167204
// SetPreStart sets a task's hook function that gets called right before the task's underlying workload is about to be started
168205
func (t *Task) SetPreStart(f func(context.Context, *Task) error) {
206+
t.mu.Lock()
207+
defer t.mu.Unlock()
208+
169209
t.PreStart = f
170210
}
171211

172212
// SetPostStop sets a task's hook function that gets called right after the task's underlying workload is stopped
173213
func (t *Task) SetPostStop(f func(context.Context, *Task) error) {
214+
t.mu.Lock()
215+
defer t.mu.Unlock()
216+
174217
t.PostStop = f
175218
}

0 commit comments

Comments
 (0)