···
7
+
"tangled.sh/tangled.sh/core/api/tangled"
8
+
"tangled.sh/tangled.sh/core/knotserver/notifier"
11
+
type PipelineStatus string
14
+
PipelinePending PipelineStatus = "pending"
15
+
PipelineRunning PipelineStatus = "running"
16
+
PipelineFailed PipelineStatus = "failed"
17
+
PipelineTimeout PipelineStatus = "timeout"
18
+
PipelineCancelled PipelineStatus = "cancelled"
19
+
PipelineSuccess PipelineStatus = "success"
23
+
Rkey string `json:"rkey"`
24
+
Knot string `json:"knot"`
25
+
Status PipelineStatus `json:"status"`
28
+
Error string `json:"error"`
29
+
ExitCode int `json:"exit_code"`
31
+
StartedAt time.Time `json:"started_at"`
32
+
UpdatedAt time.Time `json:"updated_at"`
33
+
FinishedAt time.Time `json:"finished_at"`
12
-
func (d *DB) InsertPipeline(pipeline Pipeline) error {
14
-
`insert into pipelines (rkey, nsid, event) values (?, ?, ?)`,
16
-
pipeline.PipelineJson,
36
+
func (p Pipeline) AsRecord() *tangled.PipelineStatus {
37
+
exitCode64 := int64(p.ExitCode)
38
+
finishedAt := p.FinishedAt.String()
40
+
return &tangled.PipelineStatus{
41
+
Pipeline: fmt.Sprintf("at://%s/%s", p.Knot, p.Rkey),
42
+
Status: string(p.Status),
44
+
ExitCode: &exitCode64,
47
+
StartedAt: p.StartedAt.String(),
48
+
UpdatedAt: p.UpdatedAt.String(),
49
+
FinishedAt: &finishedAt,
53
+
func pipelineAtUri(rkey, knot string) string {
54
+
return fmt.Sprintf("at://%s/did:web:%s/%s", tangled.PipelineStatusNSID, knot, rkey)
22
-
func (d *DB) GetPipeline(rkey, cursor string) (Pipeline, error) {
23
-
whereClause := "where rkey = ?"
57
+
func (db *DB) CreatePipeline(rkey, knot string, n *notifier.Notifier) error {
59
+
insert into pipelines (at_uri, status)
61
+
`, pipelineAtUri(rkey, knot), PipelinePending)
27
-
whereClause += " and rkey > ?"
28
-
args = append(args, cursor)
31
-
query := fmt.Sprintf(`
32
-
select rkey, pipeline
70
+
func (db *DB) MarkPipelineRunning(rkey, knot string, n *notifier.Notifier) error {
73
+
set status = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
75
+
`, PipelineRunning, pipelineAtUri(rkey, knot))
38
-
row := d.QueryRow(query, args...)
84
+
func (db *DB) MarkPipelineFailed(rkey, knot string, exitCode int, errorMsg string, n *notifier.Notifier) error {
90
+
updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now'),
91
+
finished_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
93
+
`, PipelineFailed, exitCode, errorMsg, pipelineAtUri(rkey, knot))
101
+
func (db *DB) MarkPipelineTimeout(rkey, knot string, n *notifier.Notifier) error {
102
+
_, err := db.Exec(`
104
+
set status = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
106
+
`, PipelineTimeout, pipelineAtUri(rkey, knot))
114
+
func (db *DB) MarkPipelineSuccess(rkey, knot string, n *notifier.Notifier) error {
115
+
_, err := db.Exec(`
117
+
set status = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now'),
118
+
finished_at = strftime('%Y-%m-%dT%H:%M:%SZ', 'now')
120
+
`, PipelineSuccess, pipelineAtUri(rkey, knot))
41
-
err := row.Scan(&p.Rkey, &p.PipelineJson)
43
-
return Pipeline{}, err
129
+
func (db *DB) GetPipeline(rkey, knot string) (Pipeline, error) {
131
+
err := db.QueryRow(`
132
+
select rkey, status, error, exit_code, started_at, updated_at, finished_at
135
+
`, pipelineAtUri(rkey, knot)).Scan(&p.Rkey, &p.Status, &p.Error, &p.ExitCode, &p.StartedAt, &p.UpdatedAt, &p.FinishedAt)
49
-
func (d *DB) GetPipelines(cursor string) ([]Pipeline, error) {
139
+
func (db *DB) GetPipelines(cursor string) ([]Pipeline, error) {
···
58
-
select rkey, nsid, pipeline
148
+
select rkey, status, error, exit_code, started_at, updated_at, finished_at
65
-
rows, err := d.Query(query, args...)
155
+
rows, err := db.Query(query, args...)
161
+
var pipelines []Pipeline
74
-
rows.Scan(&ev.Rkey, &ev.PipelineJson)
75
-
evts = append(evts, ev)
164
+
rows.Scan(&p.Rkey, &p.Status, &p.Error, &p.ExitCode, &p.StartedAt, &p.UpdatedAt, &p.FinishedAt)
165
+
pipelines = append(pipelines, p)
if err := rows.Err(); err != nil {
172
+
return pipelines, nil