feat(practice): run practice jobs on the code runner with one stored result per run

This commit is contained in:
shira committed 2026-10-11 19:31:08 +03:00
1 parent 9385b2ddd4
commit 9344228e84
17 files changed
+1010 -35

No files matched your search

@@ -10,6 +10,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
@@ -153,8 +154,16 @@ func (store *PracticeJobStore) Requeue(ctx context.Context, job outbound.Claimed
return updated == 1, nil
}
func (store *PracticeJobStore) Complete(ctx context.Context, job outbound.ClaimedPracticeJob, result domain.PracticeRunResult) (bool, error) {
func (store *PracticeJobStore) Complete(
ctx context.Context,
job outbound.ClaimedPracticeJob,
runnerRunID uuid.UUID,
result domain.PracticeRunResult,
) (bool, error) {
const location = "PracticeJobStore.Complete"
if runnerRunID == uuid.Nil {
return false, errors.New(location + " - runner run ID must be set")
}
if err := result.Validate(); err != nil {
return false, fmt.Errorf("%s - validate result: %w", location, err)
}
@@ -177,7 +186,7 @@ func (store *PracticeJobStore) Complete(ctx context.Context, job outbound.Claime
if stored != 1 {
return outbound.ErrPracticeResultExists
}
if err := finishPracticeJob(ctx, queries, job, practiceJobFinished, ""); err != nil {
if err := finishPracticeJob(ctx, queries, job, practiceJobFinished, "", runnerRunID); err != nil {
return err
}
if mode == domain.PracticeModeFinal {
@@ -205,7 +214,7 @@ func (store *PracticeJobStore) Fail(ctx context.Context, job outbound.ClaimedPra
if err != nil || !locked {
return err
}
if err := finishPracticeJob(ctx, queries, job, practiceJobFailed, class); err != nil {
if err := finishPracticeJob(ctx, queries, job, practiceJobFailed, class, uuid.Nil); err != nil {
return err
}
if mode == domain.PracticeModeFinal {
@@ -247,9 +256,15 @@ func finishPracticeJob(
job outbound.ClaimedPracticeJob,
state string,
class outbound.PracticeJobErrorClass,
runnerRunID uuid.UUID,
) error {
// A failed job has no run ID: uuid.Nil stores NULL.
var runnerRun pgtype.UUID
if runnerRunID != uuid.Nil {
runnerRun = pgUUID(runnerRunID)
}
finished, err := queries.FinishAssessmentPracticeJob(ctx, sqlc.FinishAssessmentPracticeJobParams{
State: state, ErrorClass: pgOptionalText(string(class)), SubmissionID: pgUUID(job.SubmissionID),
State: state, ErrorClass: pgOptionalText(string(class)), RunnerRunID: runnerRun, SubmissionID: pgUUID(job.SubmissionID),
LeaseOwner: pgUUID(job.LeaseOwner), StartedAt: timestamp(job.StartedAt),
})
if err != nil {
@@ -170,31 +170,35 @@ const finishAssessmentPracticeJob = `-- name: FinishAssessmentPracticeJob :execr
UPDATE assessment_practice_jobs
SET
state = $1,
runner_run_id = $2,
finished_at = greatest(created_at, clock_timestamp()),
last_error_class = coalesce($2, last_error_class),
last_error_class = coalesce($3, last_error_class),
lease_owner = NULL,
lease_expires_at = NULL,
started_at = NULL,
updated_at = greatest(updated_at, clock_timestamp())
WHERE
submission_id = $3
submission_id = $4
AND state = 'running'
AND lease_owner = $4
AND started_at = $5
AND lease_owner = $5
AND started_at = $6
`
type FinishAssessmentPracticeJobParams struct {
State string
RunnerRunID pgtype.UUID
ErrorClass pgtype.Text
SubmissionID pgtype.UUID
LeaseOwner pgtype.UUID
StartedAt pgtype.Timestamptz
}
// Ends a running try as finished or failed.
// Ends a running try as finished or failed. A finished job keeps the run ID
// of the runner run that produced its result.
func (q *Queries) FinishAssessmentPracticeJob(ctx context.Context, arg FinishAssessmentPracticeJobParams) (int64, error) {
result, err := q.db.Exec(ctx, finishAssessmentPracticeJob,
arg.State,
arg.RunnerRunID,
arg.ErrorClass,
arg.SubmissionID,
arg.LeaseOwner,
@@ -256,6 +256,7 @@ type AssessmentPracticeJob struct {
LeaseExpiresAt pgtype.Timestamptz
StartedAt pgtype.Timestamptz
LastErrorClass pgtype.Text
RunnerRunID pgtype.UUID
}
type AssessmentPracticeSubmission struct {
@@ -307,7 +307,8 @@ type Querier interface {
// made at or after reserved_since. A retry of the delivery inside the
// provider's idempotency window reuses it instead of counting again.
FindOpenMailQuotaReservation(ctx context.Context, arg FindOpenMailQuotaReservationParams) (FindOpenMailQuotaReservationRow, error)
// Ends a running try as finished or failed.
// Ends a running try as finished or failed. A finished job keeps the run ID
// of the runner run that produced its result.
FinishAssessmentPracticeJob(ctx context.Context, arg FinishAssessmentPracticeJobParams) (int64, error)
// Reads the durable active deletion request when an earlier request already
// exists. A completed request does not suppress a later explicit request.