-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathdelegate.go
191 lines (161 loc) · 4.96 KB
/
delegate.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package webhook
import (
"context"
"sync"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/utils/jsonserializable"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
)
type (
Delegate struct {
webhookJobRunner *webhookJobRunner
externalInitiatorManager ExternalInitiatorManager
lggr logger.Logger
stopCh services.StopChan
}
JobRunner interface {
RunJob(ctx context.Context, jobUUID uuid.UUID, requestBody string, meta jsonserializable.JSONSerializable) (int64, error)
}
)
var _ job.Delegate = (*Delegate)(nil)
func NewDelegate(runner pipeline.Runner, externalInitiatorManager ExternalInitiatorManager, lggr logger.Logger) *Delegate {
lggr = lggr.Named("Webhook")
return &Delegate{
externalInitiatorManager: externalInitiatorManager,
webhookJobRunner: newWebhookJobRunner(runner, lggr),
lggr: lggr,
stopCh: make(services.StopChan),
}
}
func (d *Delegate) WebhookJobRunner() JobRunner {
return d.webhookJobRunner
}
func (d *Delegate) JobType() job.Type {
return job.Webhook
}
func (d *Delegate) BeforeJobCreated(spec job.Job) {}
func (d *Delegate) AfterJobCreated(jb job.Job) {
ctx, cancel := d.stopCh.NewCtx()
defer cancel()
err := d.externalInitiatorManager.Notify(ctx, *jb.WebhookSpecID)
if err != nil {
d.lggr.Errorw("Webhook delegate AfterJobCreated errored",
"err", err,
"jobID", jb.ID,
)
}
}
func (d *Delegate) BeforeJobDeleted(spec job.Job) {
ctx, cancel := d.stopCh.NewCtx()
defer cancel()
err := d.externalInitiatorManager.DeleteJob(ctx, *spec.WebhookSpecID)
if err != nil {
d.lggr.Errorw("Webhook delegate OnDeleteJob errored",
"err", err,
"jobID", spec.ID,
)
}
}
func (d *Delegate) OnDeleteJob(context.Context, job.Job) error { return nil }
// ServicesForSpec satisfies the job.Delegate interface.
func (d *Delegate) ServicesForSpec(ctx context.Context, spec job.Job) ([]job.ServiceCtx, error) {
service := &pseudoService{
spec: spec,
webhookJobRunner: d.webhookJobRunner,
}
return []job.ServiceCtx{service}, nil
}
type pseudoService struct {
spec job.Job
webhookJobRunner *webhookJobRunner
}
// Start starts PseudoService.
func (s pseudoService) Start(context.Context) error {
// add the spec to the webhookJobRunner
return s.webhookJobRunner.addSpec(s.spec)
}
func (s pseudoService) Close() error {
// remove the spec from the webhookJobRunner
s.webhookJobRunner.rmSpec(s.spec)
return nil
}
type webhookJobRunner struct {
specsByUUID map[uuid.UUID]registeredJob
muSpecsByUUID sync.RWMutex
runner pipeline.Runner
lggr logger.Logger
}
func newWebhookJobRunner(runner pipeline.Runner, lggr logger.Logger) *webhookJobRunner {
return &webhookJobRunner{
specsByUUID: make(map[uuid.UUID]registeredJob),
runner: runner,
lggr: lggr.Named("JobRunner"),
}
}
type registeredJob struct {
job.Job
chRemove services.StopChan
}
func (r *webhookJobRunner) addSpec(spec job.Job) error {
r.muSpecsByUUID.Lock()
defer r.muSpecsByUUID.Unlock()
_, exists := r.specsByUUID[spec.ExternalJobID]
if exists {
return errors.Errorf("a webhook job with that UUID already exists (uuid: %v)", spec.ExternalJobID)
}
r.specsByUUID[spec.ExternalJobID] = registeredJob{spec, make(chan struct{})}
return nil
}
func (r *webhookJobRunner) rmSpec(spec job.Job) {
r.muSpecsByUUID.Lock()
defer r.muSpecsByUUID.Unlock()
j, exists := r.specsByUUID[spec.ExternalJobID]
if exists {
close(j.chRemove)
delete(r.specsByUUID, spec.ExternalJobID)
}
}
func (r *webhookJobRunner) spec(externalJobID uuid.UUID) (registeredJob, bool) {
r.muSpecsByUUID.RLock()
defer r.muSpecsByUUID.RUnlock()
spec, exists := r.specsByUUID[externalJobID]
return spec, exists
}
var ErrJobNotExists = errors.New("job does not exist")
func (r *webhookJobRunner) RunJob(ctx context.Context, jobUUID uuid.UUID, requestBody string, meta jsonserializable.JSONSerializable) (int64, error) {
spec, exists := r.spec(jobUUID)
if !exists {
return 0, ErrJobNotExists
}
jobLggr := r.lggr.With(
"jobID", spec.ID,
"uuid", spec.ExternalJobID,
)
ctx, cancel := spec.chRemove.Ctx(ctx)
defer cancel()
vars := pipeline.NewVarsFrom(map[string]interface{}{
"jobSpec": map[string]interface{}{
"databaseID": spec.ID,
"externalJobID": spec.ExternalJobID,
"name": spec.Name.ValueOrZero(),
},
"jobRun": map[string]interface{}{
"requestBody": requestBody,
"meta": meta.Val,
},
})
run := pipeline.NewRun(*spec.PipelineSpec, vars)
_, err := r.runner.Run(ctx, run, jobLggr, true, nil)
if err != nil {
jobLggr.Errorw("Error running pipeline for webhook job", "err", err)
return 0, err
}
if run.ID == 0 {
panic("expected run to have non-zero id")
}
return run.ID, nil
}