Files

93 lines
4.2 KiB
Go

package outbound
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
)
var (
// ErrAssessmentPracticeNotFound reports a practice that is unknown or
// not a practice of the owner's attempt.
ErrAssessmentPracticeNotFound = errors.New("assessment practice not found")
// ErrPracticeRunNotFound reports a debug run that is unknown or not a
// run of the owner's attempt.
ErrPracticeRunNotFound = errors.New("practice run not found")
)
// PracticeAttemptLock is what a practice command reads about its attempt
// under the share lock.
type PracticeAttemptLock struct {
State domain.AssessmentAttemptState
ExpiresAt time.Time
// DatabaseTime is read after the lock was granted.
DatabaseTime time.Time
}
// PracticeSubmissionReplay identifies the stored submission that a replayed
// request returns.
type PracticeSubmissionReplay struct {
ID uuid.UUID
PracticeID uuid.UUID
Mode domain.PracticeSubmissionMode
}
// PracticeFinalSolution identifies the latest final submission of a practice
// without its source.
type PracticeFinalSolution struct {
SubmissionID uuid.UUID
Language domain.TaskLanguageCode
SourceDigest domain.PracticeSourceDigest
}
// AssessmentPracticeRepositories groups the practice repositories of a unit
// of work.
type AssessmentPracticeRepositories interface {
AssessmentPractices() AssessmentPracticeRepository
}
// AssessmentPracticeRepository keeps the practice items of attempts, their
// submissions, and the execution jobs of the submissions, separately from the
// attempt snapshot and the result. Every read is scoped by the owner, so an
// identifier of another candidate reads as not found.
type AssessmentPracticeRepository interface {
// Create stores a new practice of an attempt in the not_started state.
// The attempt flow that serves practice items calls it.
Create(ctx context.Context, practice domain.AssessmentPractice) error
// LockAttempt locks the owner's attempt in share mode and reads its
// state, deadline, and the database time. An unknown or foreign attempt
// returns ErrAssessmentAttemptNotFound.
LockAttempt(ctx context.Context, userID uuid.UUID, attemptID domain.AssessmentAttemptID) (PracticeAttemptLock, error)
// LockPractice locks one practice of the owner's attempt for a state
// change. An unknown or foreign practice returns
// ErrAssessmentPracticeNotFound.
LockPractice(ctx context.Context, userID uuid.UUID, attemptID domain.AssessmentAttemptID, practiceID uuid.UUID) (domain.AssessmentPractice, error)
// SaveState stores the status, failure reason, and submission time of a
// locked practice.
SaveState(ctx context.Context, practice domain.AssessmentPractice, updatedAt time.Time) error
// FindByScope returns the latest submission of the owner stored under
// the idempotency scope, if any.
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. 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
// submission returns ErrPracticeRunNotFound.
GetRun(ctx context.Context, userID uuid.UUID, attemptID domain.AssessmentAttemptID, runID uuid.UUID) (domain.PracticeRunView, error)
}