~james/core

core/spindle/db/orphans.go -rw-r--r-- 1.8 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 17 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
package db

import (
	"context"
	"database/sql"
	"time"

	"tangled.org/core/notifier"
	"tangled.org/core/spindle/models"
)

// no row and no lease means a dead process, because workers delete the row once they take a job
// run this before the workers start, or live ones look orphaned
func (d *DB) FailOrphanedWorkflows(n *notifier.Notifier) (int64, error) {
	res, err := d.Exec(`
		insert into workflow_statuses (pipeline_id, workflow, status, error, exit_code, created_at)
		select ws.pipeline_id, ws.workflow, ?, ?, -1, ?
		from workflow_statuses ws
		where ws.status in ('pending', 'running')
		  and ws.id = (
			select max(latest.id) from workflow_statuses latest
			where latest.pipeline_id = ws.pipeline_id and latest.workflow = ws.workflow
		  )
		  and ws.pipeline_id not in (select pipeline_id from jobs)
		  and (ws.pipeline_id, ws.workflow) not in (select pipeline_id, workflow from mill_leases)`,
		string(models.StatusKindFailed), "spindle restarted before this workflow finished", time.Now().Format(time.RFC3339Nano),
	)
	if err != nil {
		return 0, err
	}
	failed, err := res.RowsAffected()
	if err == nil && failed > 0 {
		n.NotifyAll()
	}
	return failed, err
}

func failPipelineTx(ctx context.Context, tx *sql.Tx, pipelineID, reason string) error {
	_, err := tx.ExecContext(ctx, `
		insert into workflow_statuses (pipeline_id, workflow, status, error, exit_code, created_at)
		select ws.pipeline_id, ws.workflow, ?, ?, -1, ?
		from workflow_statuses ws
		where ws.pipeline_id = ?
		  and ws.status in ('pending', 'running')
		  and ws.id = (
			select max(latest.id) from workflow_statuses latest
			where latest.pipeline_id = ws.pipeline_id and latest.workflow = ws.workflow
		  )`,
		string(models.StatusKindFailed), reason, time.Now().Format(time.RFC3339Nano), pipelineID,
	)
	return err
}