Monorepo for Tangled
tangled.org
1package engine
2
3import (
4 "context"
5 "errors"
6 "log/slog"
7 "sync"
8
9 securejoin "github.com/cyphar/filepath-securejoin"
10 "tangled.org/core/notifier"
11 "tangled.org/core/spindle/config"
12 "tangled.org/core/spindle/db"
13 "tangled.org/core/spindle/models"
14 "tangled.org/core/spindle/secrets"
15)
16
17var (
18 ErrTimedOut = errors.New("timed out")
19 ErrWorkflowFailed = errors.New("workflow failed")
20)
21
22func StartWorkflows(l *slog.Logger, vault secrets.Manager, cfg *config.Config, db *db.DB, n *notifier.Notifier, ctx context.Context, pipeline *models.Pipeline, pipelineId models.PipelineId) {
23 l.Info("starting all workflows in parallel", "pipeline", pipelineId)
24
25 // extract secrets
26 var allSecrets []secrets.UnlockedSecret
27 if didSlashRepo, err := securejoin.SecureJoin(pipeline.RepoOwner, pipeline.RepoName); err == nil {
28 if res, err := vault.GetSecretsUnlocked(ctx, secrets.DidSlashRepo(didSlashRepo)); err == nil {
29 allSecrets = res
30 }
31 }
32
33 var wg sync.WaitGroup
34 for eng, wfs := range pipeline.Workflows {
35 workflowTimeout := eng.WorkflowTimeout()
36 l.Info("using workflow timeout", "timeout", workflowTimeout)
37
38 for _, w := range wfs {
39 wg.Go(func() {
40 wid := models.WorkflowId{
41 PipelineId: pipelineId,
42 Name: w.Name,
43 }
44
45 err := db.StatusRunning(wid, n)
46 if err != nil {
47 l.Error("failed to set workflow status to running", "wid", wid, "err", err)
48 return
49 }
50
51 err = eng.SetupWorkflow(ctx, wid, &w)
52 if err != nil {
53 // TODO(winter): Should this always set StatusFailed?
54 // In the original, we only do in a subset of cases.
55 l.Error("setting up worklow", "wid", wid, "err", err)
56
57 destroyErr := eng.DestroyWorkflow(ctx, wid)
58 if destroyErr != nil {
59 l.Error("failed to destroy workflow after setup failure", "error", destroyErr)
60 }
61
62 dbErr := db.StatusFailed(wid, err.Error(), -1, n)
63 if dbErr != nil {
64 l.Error("failed to set workflow status to failed", "wid", wid, "err", dbErr)
65 }
66 return
67 }
68 defer eng.DestroyWorkflow(ctx, wid)
69
70 wfLogger, err := models.NewWorkflowLogger(cfg.Server.LogDir, wid)
71 if err != nil {
72 l.Warn("failed to setup step logger; logs will not be persisted", "error", err)
73 wfLogger = nil
74 } else {
75 defer wfLogger.Close()
76 }
77
78 ctx, cancel := context.WithTimeout(ctx, workflowTimeout)
79 defer cancel()
80
81 for stepIdx, step := range w.Steps {
82 // log start of step
83 if wfLogger != nil {
84 wfLogger.
85 ControlWriter(stepIdx, step, models.StepStatusStart).
86 Write([]byte{0})
87 }
88
89 err = eng.RunStep(ctx, wid, &w, stepIdx, allSecrets, wfLogger)
90
91 // log end of step
92 if wfLogger != nil {
93 wfLogger.
94 ControlWriter(stepIdx, step, models.StepStatusEnd).
95 Write([]byte{0})
96 }
97
98 if err != nil {
99 if errors.Is(err, ErrTimedOut) {
100 dbErr := db.StatusTimeout(wid, n)
101 if dbErr != nil {
102 l.Error("failed to set workflow status to timeout", "wid", wid, "err", dbErr)
103 }
104 } else {
105 dbErr := db.StatusFailed(wid, err.Error(), -1, n)
106 if dbErr != nil {
107 l.Error("failed to set workflow status to failed", "wid", wid, "err", dbErr)
108 }
109 }
110 return
111 }
112 }
113
114 err = db.StatusSuccess(wid, n)
115 if err != nil {
116 l.Error("failed to set workflow status to success", "wid", wid, "err", err)
117 }
118 })
119 }
120 }
121
122 wg.Wait()
123 l.Info("all workflows completed")
124}