feat(practice): queue practice runs with one running and ten waiting

This commit is contained in:
shira committed 2026-10-11 15:01:07 +03:00
1 parent 51770eea6c
commit 754c745fba
40 files changed
+3040 -70

No files matched your search

@@ -74,8 +74,16 @@ type AssessmentPracticeRepository interface {
FindByScope(ctx context.Context, userID uuid.UUID, scope [IdempotencyDigestSize]byte) (PracticeSubmissionReplay, bool, error)
// LatestFinal returns the latest final submission of a practice, if any.
LatestFinal(ctx context.Context, practiceID uuid.UUID) (PracticeFinalSolution, bool, error)
// LockQueue takes the transaction-scoped lock of the practice queue and
// returns the number of queued and running jobs. Admission and the claim
// of the worker take the same lock, so a count and the insert that
// depends on it are atomic. Take it after the attempt and practice locks.
LockQueue(ctx context.Context) (active int, err error)
// ActiveRuns counts the queued and running debug runs of a practice.
ActiveRuns(ctx context.Context, practiceID uuid.UUID) (int, error)
// Submit stores a submission under the idempotency scope and queues its
// execution job in the same transaction.
// execution job in the same transaction. The caller holds the queue lock
// and has checked its limits.
Submit(ctx context.Context, submission domain.PracticeSubmission, scope [IdempotencyDigestSize]byte) error
// GetRun reads a debug run of the owner's attempt with the state of its
// job. It never returns a final submission; an unknown, foreign, or final
@@ -0,0 +1,109 @@
package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
// ErrPracticeResultExists reports a completion of a submission that already
// has a result. Nothing was written.
var ErrPracticeResultExists = errors.New("practice submission already has a result")
// PracticeJobErrorClass is the bounded cause of a failed or abandoned try of
// a practice job. It never carries runner or candidate text.
type PracticeJobErrorClass string
const (
// PracticeJobRunnerUnavailable: the runner could not be reached, refused
// the token, or answered outside its contract; the try is repeated with
// the same run key.
PracticeJobRunnerUnavailable PracticeJobErrorClass = "runner_unavailable"
// PracticeJobInfrastructureFailure: the runner could not evaluate the
// source; the next try uses a new generation.
PracticeJobInfrastructureFailure PracticeJobErrorClass = "runner_infrastructure_failure"
// PracticeJobInvalidRequest: the stored task or submission cannot form a
// valid runner request, a content bug; the job fails at once.
PracticeJobInvalidRequest PracticeJobErrorClass = "invalid_request"
// PracticeJobRunTimeout: the run stayed unfinished on the runner longer
// than the run timeout.
PracticeJobRunTimeout PracticeJobErrorClass = "run_timeout"
// PracticeJobLeaseExpired: the worker of the try stopped without
// finishing it, and another claim took the job over.
PracticeJobLeaseExpired PracticeJobErrorClass = "lease_expired"
)
// PracticeJobClaim asks for the next practice job. Now is the application
// clock; the database clock never decides whether a lease expired.
type PracticeJobClaim struct {
Owner uuid.UUID
Now time.Time
Lease time.Duration
// MaxRunning bounds the running jobs whose leases have not expired.
MaxRunning int
}
// ClaimedPracticeJob is one running try of a practice job. The owner and the
// start time identify the try: every change of the job checks both, so a
// worker whose job was taken over changes nothing.
type ClaimedPracticeJob struct {
SubmissionID uuid.UUID
Mode domain.PracticeSubmissionMode
Generation uint32
Attempts int
LeaseOwner uuid.UUID
LeaseExpiresAt time.Time
StartedAt time.Time
// Reclaimed reports that the previous try's lease expired and this
// claim took the job over.
Reclaimed bool
}
// PracticeJobWork is what a claimed job runs.
type PracticeJobWork struct {
Submission domain.PracticeSubmission
Task domain.PracticeTask
}
// PracticeRequeue puts a running try back into the queue.
type PracticeRequeue struct {
AvailableAt time.Time
Generation uint32
// Counted adds the try to the attempts of the job; a busy runner and a
// shutdown do not count.
Counted bool
ErrorClass PracticeJobErrorClass
}
// PracticeJobStore keeps the practice queue for the worker. Each method is
// one short transaction; no transaction stays open while the runner works.
type PracticeJobStore interface {
// Claim takes the next job under the queue lock when fewer than
// MaxRunning jobs hold a live lease: a running job whose lease expired
// first, then the next queued job that is available, final submissions
// before debug runs. claimed is false when there is nothing to run.
Claim(ctx context.Context, claim PracticeJobClaim) (job ClaimedPracticeJob, claimed bool, err error)
// Work reads the submission and the task of a claimed job. Stored
// content that fails validation returns an error wrapping
// domain.ErrInvalidAssessmentPractice.
Work(ctx context.Context, job ClaimedPracticeJob) (PracticeJobWork, error)
// Extend renews the lease of a try until expiresAt. held is false when
// the job no longer belongs to the try.
Extend(ctx context.Context, job ClaimedPracticeJob, expiresAt time.Time) (renewed ClaimedPracticeJob, held bool, err error)
// Requeue puts a try back into the queue; held is false when the job no
// longer belongs to the try.
Requeue(ctx context.Context, job ClaimedPracticeJob, requeue PracticeRequeue) (held bool, err error)
// Complete stores the result of the submission once, finishes the job,
// and, for the latest final submission of a pending practice, finishes
// the practice, all in one transaction. held is false, with nothing
// written, when the job no longer belongs to the try.
Complete(ctx context.Context, job ClaimedPracticeJob, result domain.PracticeRunResult) (held bool, err error)
// Fail ends the job as failed with the error class and, for the latest
// final submission of a pending practice, marks the practice failed with
// sandbox_error. It never stores a verdict.
Fail(ctx context.Context, job ClaimedPracticeJob, class PracticeJobErrorClass) (held bool, err error)
}