From 07c7317e0b6efbe86958ed8c44c51edca7f3421d Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sat, 10 Jan 2026 22:04:54 +0100 Subject: Avoid duplicating tasks on push --- acid.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/acid.go b/acid.go index 16322a5..edbe987 100644 --- a/acid.go +++ b/acid.go @@ -507,6 +507,28 @@ type GiteaPushEvent struct { } `json:"repository"` } +func getPendingRunnersFor(ctx context.Context, tx *sql.Tx, + owner, repo, hash string) (map[string]struct{}, error) { + rows, err := tx.QueryContext(ctx, `SELECT DISTINCT runner FROM task + WHERE owner = ? AND repo = ? AND hash = ? AND state = ?`, + owner, repo, hash, taskStateNew) + if err != nil { + return nil, err + } + defer rows.Close() + + runners := make(map[string]struct{}) + for rows.Next() { + var r string + err := rows.Scan(&r) + if err != nil { + return nil, err + } + runners[r] = struct{}{} + } + return runners, rows.Err() +} + func createTasks(ctx context.Context, owner, repo, hash string, runners []string) error { tx, err := gDB.BeginTx(ctx, nil) @@ -515,6 +537,16 @@ func createTasks(ctx context.Context, } defer tx.Rollback() + // When you push a tag for a commit that has already had its task run, + // running it anew may make it receive a new "git describe" value. + // + // But if the task hasn't managed to run yet and is currently + // still enqueued, duplicating it provides nothing. + pending, err := getPendingRunnersFor(ctx, tx, owner, repo, hash) + if err != nil { + return err + } + stmt, err := tx.Prepare( `INSERT INTO task(owner, repo, hash, runner, created, changed) VALUES (?, ?, ?, ?, unixepoch('now'), unixepoch('now'))`) @@ -523,6 +555,9 @@ func createTasks(ctx context.Context, } for _, runner := range runners { + if _, ok := pending[runner]; ok { + continue + } if _, err := stmt.Exec(owner, repo, hash, runner); err != nil { return err } -- cgit v1.2.3-70-g09d2