~sircmpwn/sr.ht-dev

builds.sr.ht: api: pass job owner to StartJobGroupUnsafe() v1 APPLIED

Conrad Hoffmann: 1
 api: pass job owner to StartJobGroupUnsafe()

 2 files changed, 7 insertions(+), 5 deletions(-)
#891120 running alpine.yml
#891121 running archlinux.yml
#891122 running debian.yml
Thanks!

To git@git.sr.ht:~sircmpwn/builds.sr.ht
   eaf435b..292dc73  master -> master
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/37132/mbox | git am -3
Learn more about email & git

[PATCH builds.sr.ht] api: pass job owner to StartJobGroupUnsafe() Export this patch

This allows it to make use of the index on `owner_id` in the `job`
table. Without this, the query it makes is prohibitively expensive. A
sample query analysis went from > 1800 ms without the index to < 1ms
when using the index.
---
 api/graph/resolver.go         | 8 +++++---
 api/graph/schema.resolvers.go | 4 ++--
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/api/graph/resolver.go b/api/graph/resolver.go
index ad9020c..9e30065 100644
--- a/api/graph/resolver.go
+++ b/api/graph/resolver.go
@@ -66,7 +66,7 @@ func FetchLogs(ctx context.Context, url string) (*model.Log, error) {
}

// Starts a job group. Does not authenticate the user.
func StartJobGroupUnsafe(ctx context.Context, tx *sql.Tx, id int) error {
func StartJobGroupUnsafe(ctx context.Context, tx *sql.Tx, id, ownerID int) error {
	var manifests []struct {
		ID       int
		Manifest *Manifest
@@ -74,9 +74,11 @@ func StartJobGroupUnsafe(ctx context.Context, tx *sql.Tx, id int) error {

	rows, err := tx.QueryContext(ctx, `
		UPDATE job SET status = 'queued'
		WHERE job_group_id = $1
		WHERE
			job_group_id = $1 AND
			owner_id = $2
		RETURNING id, manifest;
	`, id)
	`, id, ownerID)
	if err != nil {
		return err
	}
diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go
index 41541cb..23b4d1e 100644
--- a/api/graph/schema.resolvers.go
+++ b/api/graph/schema.resolvers.go
@@ -540,7 +540,7 @@ func (r *mutationResolver) CreateGroup(ctx context.Context, jobIds []int, trigge
			return nil
		}

		return StartJobGroupUnsafe(ctx, tx, group.ID)
		return StartJobGroupUnsafe(ctx, tx, group.ID, group.OwnerID)
	}); err != nil {
		return nil, err
	}
@@ -566,7 +566,7 @@ func (r *mutationResolver) StartGroup(ctx context.Context, groupID int) (*model.
			return err
		}

		return StartJobGroupUnsafe(ctx, tx, groupID)
		return StartJobGroupUnsafe(ctx, tx, groupID, group.OwnerID)
	}); err != nil {
		return nil, err
	}
-- 
2.38.1
Looks good.

Are there other places where we select on job_group_id? Maybe it would
be a good idea to create an index for this column regardless.