1
0
out-of-tree/daemon/db/job.go

194 lines
4.0 KiB
Go
Raw Permalink Normal View History

2024-02-20 13:25:31 +00:00
package db
import (
"bytes"
2024-02-20 13:25:31 +00:00
"database/sql"
"encoding/gob"
2024-02-27 02:00:07 +00:00
"time"
2024-02-20 13:25:31 +00:00
"code.dumpstack.io/tools/out-of-tree/api"
)
func createJobTable(db *sql.DB) (err error) {
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS job (
id INTEGER PRIMARY KEY,
updated INT,
2024-02-20 13:25:31 +00:00
uuid TEXT,
2024-02-26 08:55:27 +00:00
group_uuid TEXT,
2024-02-20 13:25:31 +00:00
repo TEXT,
"commit" TEXT,
2024-03-04 18:31:16 +00:00
description TEXT,
2024-02-20 13:25:31 +00:00
config TEXT,
target TEXT,
2024-02-27 02:00:07 +00:00
created INT,
started INT,
finished INT,
2024-02-20 13:25:31 +00:00
status TEXT DEFAULT "new"
)`)
return
}
func AddJob(db *sql.DB, job *api.Job) (err error) {
stmt, err := db.Prepare(`INSERT INTO job (updated, uuid, group_uuid, repo, "commit", ` +
2024-03-04 18:31:16 +00:00
`description, config, target, created, started, finished) ` +
`VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11);`)
2024-02-20 13:25:31 +00:00
if err != nil {
return
}
defer stmt.Close()
var abuf bytes.Buffer
err = gob.NewEncoder(&abuf).Encode(job.Artifact)
if err != nil {
return
}
config := abuf.Bytes()
var tbuf bytes.Buffer
err = gob.NewEncoder(&tbuf).Encode(job.Target)
if err != nil {
return
}
target := tbuf.Bytes()
2024-02-20 13:25:31 +00:00
res, err := stmt.Exec(time.Now().Unix(), job.UUID, job.Group,
2024-03-04 18:31:16 +00:00
job.RepoName, job.Commit, job.Description, config, target,
2024-02-27 02:00:07 +00:00
job.Created.Unix(), job.Started.Unix(),
job.Finished.Unix(),
2024-02-20 13:25:31 +00:00
)
if err != nil {
return
}
job.ID, err = res.LastInsertId()
return
}
2024-02-26 08:55:27 +00:00
func UpdateJob(db *sql.DB, job *api.Job) (err error) {
2024-02-27 02:00:07 +00:00
stmt, err := db.Prepare(`UPDATE job ` +
`SET updated=$1, uuid=$2, group_uuid=$3, repo=$4, ` +
2024-03-04 18:31:16 +00:00
`"commit"=$5, description=$6, config=$7, target=$8, ` +
`created=$9, started=$10, finished=$11, ` +
`status=$12 ` +
`WHERE id=$13`)
2024-02-20 13:25:31 +00:00
if err != nil {
return
}
defer stmt.Close()
var abuf bytes.Buffer
err = gob.NewEncoder(&abuf).Encode(job.Artifact)
if err != nil {
return
}
config := abuf.Bytes()
var tbuf bytes.Buffer
err = gob.NewEncoder(&tbuf).Encode(job.Target)
if err != nil {
return
}
target := tbuf.Bytes()
2024-02-20 13:25:31 +00:00
_, err = stmt.Exec(time.Now().Unix(), job.UUID, job.Group,
2024-03-04 18:31:16 +00:00
job.RepoName, job.Commit, job.Description,
2024-02-20 13:25:31 +00:00
config, target,
2024-02-27 02:00:07 +00:00
job.Created.Unix(), job.Started.Unix(),
job.Finished.Unix(), job.Status, job.ID)
return
}
func scanJob(scan func(dest ...any) error) (job api.Job, err error) {
var config, target []byte
var updated, created, started, finished int64
err = scan(&job.ID, &updated, &job.UUID, &job.Group,
2024-03-04 18:31:16 +00:00
&job.RepoName, &job.Commit, &job.Description,
&config, &target,
2024-02-27 02:00:07 +00:00
&created, &started, &finished, &job.Status)
if err != nil {
return
}
abuf := bytes.NewBuffer(config)
err = gob.NewDecoder(abuf).Decode(&job.Artifact)
2024-02-27 02:00:07 +00:00
if err != nil {
return
}
tbuf := bytes.NewBuffer(target)
err = gob.NewDecoder(tbuf).Decode(&job.Target)
2024-02-27 02:00:07 +00:00
if err != nil {
return
}
job.UpdatedAt = time.Unix(updated, 0)
2024-02-27 02:00:07 +00:00
job.Created = time.Unix(created, 0)
job.Started = time.Unix(started, 0)
job.Finished = time.Unix(finished, 0)
2024-02-20 13:25:31 +00:00
return
}
func Jobs(db *sql.DB, where string, args ...any) (jobs []api.Job, err error) {
q := `SELECT id, updated, uuid, group_uuid, ` +
2024-03-04 18:31:16 +00:00
`repo, "commit", description, config, target, created, ` +
`started, finished, status FROM job`
if len(where) != 0 {
q += ` WHERE ` + where
}
stmt, err := db.Prepare(q)
2024-02-20 13:25:31 +00:00
if err != nil {
return
}
defer stmt.Close()
rows, err := stmt.Query(args...)
2024-02-20 13:25:31 +00:00
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var job api.Job
2024-02-27 02:00:07 +00:00
job, err = scanJob(rows.Scan)
2024-02-20 13:25:31 +00:00
if err != nil {
return
}
jobs = append(jobs, job)
}
return
}
func Job(db *sql.DB, uuid string) (job api.Job, err error) {
stmt, err := db.Prepare(`SELECT id, updated, uuid, ` +
`group_uuid, ` +
2024-03-04 18:31:16 +00:00
`repo, "commit", description, config, target, ` +
2024-02-27 02:00:07 +00:00
`created, started, finished, status ` +
2024-02-20 13:25:31 +00:00
`FROM job WHERE uuid=$1`)
if err != nil {
return
}
defer stmt.Close()
2024-02-27 02:00:07 +00:00
return scanJob(stmt.QueryRow(uuid).Scan)
2024-02-20 13:25:31 +00:00
}
func JobStatus(db *sql.DB, uuid string) (st api.Status, err error) {
stmt, err := db.Prepare(`SELECT status FROM job ` +
`WHERE uuid=$1`)
if err != nil {
return
}
defer stmt.Close()
err = stmt.QueryRow(uuid).Scan(&st)
if err != nil {
return
}
return
}