~james/core

core/spindle/mill/executor/observe.go -rw-r--r-- 7.9 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package executor

import (
	"context"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"sync"
	"time"

	"github.com/hpcloud/tail"
	"tangled.org/core/spindle/db"
	"tangled.org/core/spindle/engine"
	millproto "tangled.org/core/spindle/mill/proto"
	millv1 "tangled.org/core/spindle/mill/proto/gen"
	"tangled.org/core/spindle/models"
	"tangled.org/core/spindle/secrets"
)

func (e *Executor) observeLoop(ctx context.Context, sub <-chan struct{}, cursor int64) {
	ticker := time.NewTicker(5 * time.Second)
	defer ticker.Stop()

	for {
		select {
		case <-ctx.Done():
			return
		case <-sub:
		case <-ticker.C:
		}
		e.drainEvents(&cursor)
	}
}

func (e *Executor) drainEvents(cursor *int64) {
	events, err := e.db.GetEvents(*cursor, 128)
	if err != nil {
		e.l.Error("drain status events failed", "err", err)
		return
	}
	for _, event := range events {
		if event.Id > *cursor {
			*cursor = event.Id
		}
		if err := e.onStatusRow(&event); err != nil {
			e.l.Error("process status row failed", "err", err)
		}
	}
}

func (e *Executor) onStatusRow(st *db.StatusRow) error {
	res := e.reservationFor(st.Pipeline, st.Workflow)
	if res == nil {
		return nil
	}

	if models.StatusKind(st.Status).IsFinish() {
		reported, err := e.reportOutcome(res, st)
		if err != nil {
			e.l.Error("report outcome failed", "lease", res.leaseID, "err", err)
		}
		if !reported {
			return e.finishJob(res, st)
		}
		// a debug shell can hold the VM for minutes, and waiting here stalls every other job's status
		e.finishWG.Add(1)
		go func() {
			defer e.finishWG.Done()
			if err := e.finishJob(res, st); err != nil {
				e.l.Error("finish job failed", "lease", res.leaseID, "err", err)
			}
		}()
		return nil
	}

	return e.appendStatus(res.leaseID, st)
}

// false when the run already ended or the mill predates outcomes
func (e *Executor) reportOutcome(res *reservation, st *db.StatusRow) (bool, error) {
	e.mu.Lock()
	if e.active[res.leaseID] != res || !res.millAcceptsOutcome || res.runDone == nil {
		e.mu.Unlock()
		return false, nil
	}
	select {
	case <-res.runDone:
		e.mu.Unlock()
		return false, nil
	default:
	}
	status := st.Status
	if res.cancelled {
		status = string(models.StatusKindCancelled)
	}
	res.reportedStatus = status
	e.mu.Unlock()

	// every log line has to reach the mill before the status does
	if res.stopTail != nil {
		res.stopTail()
	}
	return true, e.appendOutcome(res.leaseID, status, st)
}

func (e *Executor) finishJob(res *reservation, st *db.StatusRow) error {
	e.mu.Lock()
	if e.active[res.leaseID] != res {
		e.mu.Unlock()
		return nil
	}
	cancelled := res.cancelled
	reported := res.reportedStatus
	runDone := res.runDone
	e.mu.Unlock()

	if runDone != nil {
		<-runDone
	}
	e.mu.Lock()
	failureClass := res.failureClass
	failureReason := res.failureReason
	millRecordsTerminalMetrics := res.millRecordsTerminalMetrics
	e.mu.Unlock()

	// the log tail finalizes first so all log lines precede the terminal event
	if res.stopTail != nil {
		res.stopTail()
	}

	terminalStatus := st.Status
	if cancelled {
		terminalStatus = string(models.StatusKindCancelled)
	}
	if reported != "" {
		// the mill already published it, so a late cancel can't change it
		terminalStatus = reported
	}
	if cancelled && terminalStatus == string(models.StatusKindCancelled) {
		failureClass = string(engine.FailureClassUser)
		failureReason = string(engine.FailureReasonCancelled)
	}
	if failureClass == "" || failureReason == "" {
		failureClass, failureReason = defaultTerminalAttribution(terminalStatus)
	}
	var logDir string
	if e.cfg != nil {
		logDir = e.cfg.Server.LogDir
	}
	logPath := models.LogFilePath(logDir, res.wid)

	var errStr string
	if st != nil && st.Error != nil {
		errStr = *st.Error
	}
	var exitCode int64
	if st != nil && st.ExitCode != nil {
		exitCode = *st.ExitCode
	}

	// sha256 of the log file
	hash := ""
	if f, err := os.Open(logPath); err == nil {
		h := sha256.New()
		if _, err := io.Copy(h, f); err == nil {
			hash = "sha256:" + hex.EncodeToString(h.Sum(nil))
		}
		_ = f.Close()
	}

	// refs are opaque keys interpreted by the configured artifact store
	ref := "logs/" + res.leaseID + ".log"

	// persist pending artifact state so a restart can retry the upload
	if e.db != nil {
		_ = e.db.SavePendingArtifact(
			res.leaseID, res.wid, terminalStatus, errStr, exitCode, ref, hash,
			failureClass, failureReason, millRecordsTerminalMetrics,
		)
	}

	// upload with a context that survives job cancellation
	cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(context.Background()), 2*time.Minute)
	defer cancel()

	if e.writer != nil {
		if f, err := os.Open(logPath); err == nil {
			defer f.Close()
			if uploadErr := e.writer.Put(cleanupCtx, ref, f); uploadErr != nil {
				e.l.Error("artifact upload failed", "lease", res.leaseID, "err", uploadErr)
				return fmt.Errorf("artifact upload: %w", uploadErr)
			}
		}
	}

	// append the terminal event with the LogArtifact ref
	if err := e.appendTerminalWithArtifact(
		res.leaseID, terminalStatus, st, ref, hash,
		failureClass, failureReason, millRecordsTerminalMetrics,
	); err != nil {
		return err
	}

	// only drop the pending state once the terminal event is in the outbox
	if e.db != nil {
		_ = e.db.RemovePendingArtifact(res.leaseID)
	}
	e.mu.Lock()
	cleanup := e.removeReservationLocked(res, false)
	e.mu.Unlock()

	cleanup()

	e.pushSnapshot()
	return nil
}

func (e *Executor) reservationFor(pipelineID, workflow string) *reservation {
	e.mu.Lock()
	defer e.mu.Unlock()
	for _, res := range e.active {
		if res.wid.PipelineId == models.PipelineId(pipelineID) && res.wid.Name == workflow {
			return res
		}
	}
	return nil
}

func (e *Executor) maskSecrets(res *reservation, text string) string {
	if res == nil || res.mask == nil {
		return text
	}
	return res.mask.Mask(text)
}

func (e *Executor) SendLiveLog(leaseID string, raw []byte) error {
	e.connMu.Lock()
	enc := e.enc
	e.connMu.Unlock()
	if enc == nil {
		return nil
	}
	return enc.Encode(&millproto.Message{
		LiveLog: &millv1.LiveLog{
			LeaseId: leaseID,
			RawJson: raw,
		},
	})
}

func (e *Executor) startTail(res *reservation) {
	path := models.LogFilePath(e.cfg.Server.LogDir, res.wid)
	t, err := tail.TailFile(path, tail.Config{
		Follow:    true,
		ReOpen:    true,
		MustExist: false,
		Location:  &tail.SeekInfo{Offset: 0, Whence: io.SeekStart},
		Logger:    tail.DiscardingLogger,
	})
	if err != nil {
		e.l.Error("tail log file failed", "wid", res.wid, "err", err)
		return
	}

	done := make(chan struct{})
	go func() {
		defer close(done)
		for line := range t.Lines {
			if line == nil || line.Err != nil {
				continue
			}
			masked := e.maskSecrets(res, line.Text)
			_ = e.SendLiveLog(res.leaseID, []byte(masked+"\n"))
		}
	}()

	var once sync.Once
	res.stopTail = func() {
		once.Do(func() {
			_ = t.StopAtEOF()
			<-done
		})
	}
}

type memVault struct {
	secrets []secrets.UnlockedSecret
}

// the live stream must hide exactly what the persisted log hides, so it goes
// through the same masker: encoded and line-split variants included
func newSecretMask(pb []*millv1.Secret) *models.SecretMask {
	values := make([]string, 0, len(pb))
	for _, s := range pb {
		values = append(values, s.Value)
	}
	return models.NewSecretMask(values)
}

func newMemVault(pb []*millv1.Secret) *memVault {
	v := &memVault{secrets: make([]secrets.UnlockedSecret, 0, len(pb))}
	for _, s := range pb {
		v.secrets = append(v.secrets, secrets.UnlockedSecret{
			Key:   s.Key,
			Value: s.Value,
		})
	}
	return v
}

func (v *memVault) GetSecretsUnlocked(ctx context.Context, repo secrets.RepoIdentifier) ([]secrets.UnlockedSecret, error) {
	return v.secrets, nil
}
func (v *memVault) GetSecretsLocked(ctx context.Context, repo secrets.RepoIdentifier) ([]secrets.LockedSecret, error) {
	return nil, nil
}
func (v *memVault) AddSecret(ctx context.Context, s secrets.UnlockedSecret) error { return nil }
func (v *memVault) RemoveSecret(ctx context.Context, s secrets.Secret[any]) error { return nil }
func (v *memVault) RemoveAllSecrets(ctx context.Context, repo secrets.RepoIdentifier) error {
	return nil
}

var _ secrets.Manager = (*memVault)(nil)