@@ -3,6 +3,7 @@ package core
3
3
import (
4
4
"fmt"
5
5
"strconv"
6
+ "sync"
6
7
"time"
7
8
8
9
docker "github.com/fsouza/go-dockerclient"
@@ -38,12 +39,25 @@ type RunJob struct {
38
39
Environment []string
39
40
40
41
containerID string
42
+ mu sync.RWMutex // Protect containerID access
41
43
}
42
44
43
45
func NewRunJob (c * docker.Client ) * RunJob {
44
46
return & RunJob {Client : c }
45
47
}
46
48
49
+ func (j * RunJob ) setContainerID (id string ) {
50
+ j .mu .Lock ()
51
+ j .containerID = id
52
+ j .mu .Unlock ()
53
+ }
54
+
55
+ func (j * RunJob ) getContainerID () string {
56
+ j .mu .RLock ()
57
+ defer j .mu .RUnlock ()
58
+ return j .containerID
59
+ }
60
+
47
61
func (j * RunJob ) Run (ctx * Context ) error {
48
62
var container * docker.Container
49
63
var err error
@@ -103,7 +117,7 @@ func (j *RunJob) Run(ctx *Context) error {
103
117
}
104
118
105
119
if container != nil {
106
- j .containerID = container .ID
120
+ j .setContainerID ( container .ID )
107
121
}
108
122
109
123
// cleanup container if it is a created one
@@ -126,7 +140,7 @@ func (j *RunJob) Run(ctx *Context) error {
126
140
}
127
141
128
142
if logsErr := j .Client .Logs (docker.LogsOptions {
129
- Container : container . ID ,
143
+ Container : j . getContainerID () ,
130
144
OutputStream : ctx .Execution .OutputStream ,
131
145
ErrorStream : ctx .Execution .ErrorStream ,
132
146
Stdout : true ,
@@ -205,19 +219,16 @@ func (j *RunJob) buildContainer() (*docker.Container, error) {
205
219
}
206
220
207
221
func (j * RunJob ) startContainer () error {
208
- return j .Client .StartContainer (j .containerID , & docker.HostConfig {})
222
+ return j .Client .StartContainer (j .getContainerID () , & docker.HostConfig {})
209
223
}
210
224
211
225
func (j * RunJob ) stopContainer (timeout uint ) error {
212
- return j .Client .StopContainer (j .containerID , timeout )
226
+ return j .Client .StopContainer (j .getContainerID () , timeout )
213
227
}
214
228
215
229
func (j * RunJob ) getContainer () (* docker.Container , error ) {
216
- container , err := j .Client .InspectContainer (j .containerID )
217
- if err != nil {
218
- return nil , err
219
- }
220
- return container , nil
230
+ id := j .getContainerID ()
231
+ return j .Client .InspectContainer (id )
221
232
}
222
233
223
234
const (
@@ -236,7 +247,7 @@ func (j *RunJob) watchContainer() error {
236
247
return ErrMaxTimeRunning
237
248
}
238
249
239
- c , err := j .Client .InspectContainer (j .containerID )
250
+ c , err := j .Client .InspectContainer (j .getContainerID () )
240
251
if err != nil {
241
252
return err
242
253
}
@@ -261,8 +272,7 @@ func (j *RunJob) deleteContainer() error {
261
272
if delete , _ := strconv .ParseBool (j .Delete ); ! delete {
262
273
return nil
263
274
}
264
-
265
275
return j .Client .RemoveContainer (docker.RemoveContainerOptions {
266
- ID : j .containerID ,
276
+ ID : j .getContainerID () ,
267
277
})
268
278
}
0 commit comments