forked from vmware-archive/atc
/
run_state.go
118 lines (91 loc) · 2.47 KB
/
run_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package exec
import (
"io"
"reflect"
"sync"
"github.com/concourse/atc"
"github.com/concourse/atc/worker"
)
type runState struct {
artifacts *worker.ArtifactRepository
results *sync.Map
inputs *sync.Map
outputs *sync.Map
}
func NewRunState() RunState {
return &runState{
artifacts: worker.NewArtifactRepository(),
results: &sync.Map{},
inputs: &sync.Map{},
outputs: &sync.Map{},
}
}
func (state *runState) Artifacts() *worker.ArtifactRepository {
return state.artifacts
}
func (state *runState) Result(id atc.PlanID, to interface{}) bool {
val, ok := state.results.Load(id)
if !ok {
return false
}
if reflect.TypeOf(val).AssignableTo(reflect.TypeOf(to).Elem()) {
reflect.ValueOf(to).Elem().Set(reflect.ValueOf(val))
return true
}
return false
}
func (state *runState) StoreResult(id atc.PlanID, val interface{}) {
state.results.Store(id, val)
}
func (state *runState) SendUserInput(id atc.PlanID, input io.ReadCloser) {
i, loaded := state.inputs.LoadOrStore(id, make(chan io.ReadCloser))
if loaded {
// clear out the channel
state.inputs.Delete(id)
}
inputs := i.(chan io.ReadCloser)
// send input (blocking) to a reader
inputs <- input
// wait for reader to close channel signifying that they're done
<-inputs
}
func (state *runState) ReadUserInput(id atc.PlanID, handler InputHandler) error {
i, loaded := state.inputs.LoadOrStore(id, make(chan io.ReadCloser))
if loaded {
// clear out the channel
state.inputs.Delete(id)
}
inputs := i.(chan io.ReadCloser)
// signal to sender that we're done
defer close(inputs)
// wait for stream from request to arrive
stream := <-inputs
// synchronously stream in
return handler(stream)
}
func (state *runState) ReadPlanOutput(id atc.PlanID, output io.Writer) {
i, loaded := state.outputs.LoadOrStore(id, make(chan io.Writer))
if loaded {
// clear out the channel
state.outputs.Delete(id)
}
outputs := i.(chan io.Writer)
// send input (blocking) to a reader
outputs <- output
// wait for reader to close channel signifying that they're done
<-outputs
}
func (state *runState) SendPlanOutput(id atc.PlanID, handler OutputHandler) error {
i, loaded := state.outputs.LoadOrStore(id, make(chan io.Writer))
if loaded {
// clear out the channel
state.outputs.Delete(id)
}
outputs := i.(chan io.Writer)
// signal to sender that we're done
defer close(outputs)
// wait for stream from request to arrive
stream := <-outputs
// synchronously stream in
return handler(stream)
}