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

+9 -3
View File
@@ -7,6 +7,8 @@ import (
"log/slog"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
@@ -172,19 +174,23 @@ func (executor *Executor) runnerFailure(ctx context.Context, job outbound.Claime
}
func (executor *Executor) complete(ctx context.Context, job outbound.ClaimedPracticeJob, status outbound.CodeRunStatus) (ExecutionOutcome, error) {
if status.Result == nil {
// A finished run names the runner run that the stored result belongs to;
// a status without a result or a valid run ID is outside the contract.
runnerRunID, idErr := uuid.Parse(status.RunID)
if status.Result == nil || idErr != nil || runnerRunID == uuid.Nil {
return executor.runnerFailure(ctx, job, outbound.ErrCodeRunnerBadResponse)
}
storeCtx, cancel := executor.storeContext(ctx)
defer cancel()
held, err := executor.store.Complete(storeCtx, job, runResult(*status.Result))
held, err := executor.store.Complete(storeCtx, job, runnerRunID, runResult(*status.Result))
switch {
case err != nil:
return "", fmt.Errorf("Executor.complete - store result: %w", err)
case !held:
return OutcomeLeaseLost, nil
}
executor.logger.Info("practice job finished", jobAttributes(job, slog.String("outcome", string(status.Result.Outcome)))...)
executor.logger.Info("practice job finished", jobAttributes(job,
slog.String("outcome", string(status.Result.Outcome)), slog.String("runner_run_id", runnerRunID.String()))...)
return OutcomeFinished, nil
}
@@ -95,6 +95,33 @@ func TestExecutorHandlesPollFailuresAndShutdown(t *testing.T) {
}
}
func TestExecutorStoresTheRunnerRunIDWithTheResult(t *testing.T) {
now := time.Date(2026, 10, 11, 12, 0, 0, 0, time.UTC)
job := outbound.ClaimedPracticeJob{SubmissionID: uuid.New(), StartedAt: now}
store := &fakeJobStore{}
runner := &fakeRunner{submits: []outbound.CodeRunState{outbound.CodeRunFinished}}
executor := testExecutor(t, runner, store, now)
if outcome, err := executor.Execute(t.Context(), job); err != nil || outcome != OutcomeFinished {
t.Fatalf("Execute() = %q, error %v; want finished", outcome, err)
}
if store.runnerRunID != uuid.MustParse(fakeRunID) {
t.Fatalf("stored runner run ID = %s, want %s", store.runnerRunID, fakeRunID)
}
// A finished run without a valid run ID is outside the runner contract,
// so no result is stored and the try is retried on the same key.
for _, runID := range []string{"", "not-a-uuid", uuid.Nil.String()} {
store = &fakeJobStore{}
runner = &fakeRunner{submits: []outbound.CodeRunState{outbound.CodeRunFinished}, runID: &runID}
executor = testExecutor(t, runner, store, now)
outcome, err := executor.Execute(t.Context(), job)
if err != nil || outcome != OutcomeRetry || store.completed || store.requeue.Generation != job.Generation {
t.Fatalf("Execute() with run ID %q = %q, error %v, completed %v; want a retry without a result", runID, outcome, err, store.completed)
}
}
}
func testExecutor(t *testing.T, runner outbound.CodeRunner, store outbound.PracticeJobStore, now time.Time) *Executor {
t.Helper()
executor, err := NewExecutor(runner, store, func() time.Time { return now }, ExecutionSettings{
@@ -108,6 +135,8 @@ func testExecutor(t *testing.T, runner outbound.CodeRunner, store outbound.Pract
}
type fakeRunner struct {
// runID overrides the run ID of a finished status when set.
runID *string
submitErr error
pollErr error
submits []outbound.CodeRunState
@@ -121,7 +150,7 @@ func (runner *fakeRunner) Submit(_ context.Context, request outbound.CodeRunRequ
return outbound.CodeRunStatus{}, runner.submitErr
}
state := runner.submits[min(runner.submitCalls, len(runner.submits))-1]
return fakeStatus(state, request), nil
return runner.status(state, request), nil
}
func (runner *fakeRunner) Poll(_ context.Context, request outbound.CodeRunRequest) (outbound.CodeRunStatus, error) {
@@ -129,9 +158,20 @@ func (runner *fakeRunner) Poll(_ context.Context, request outbound.CodeRunReques
if runner.pollErr != nil {
return outbound.CodeRunStatus{}, runner.pollErr
}
return fakeStatus(outbound.CodeRunFinished, request), nil
return runner.status(outbound.CodeRunFinished, request), nil
}
func (runner *fakeRunner) status(state outbound.CodeRunState, request outbound.CodeRunRequest) outbound.CodeRunStatus {
status := fakeStatus(state, request)
if runner.runID != nil {
status.RunID = *runner.runID
}
return status
}
// fakeRunID is the opaque run ID that the fake runner reports.
const fakeRunID = "0a1b2c3d-4e5f-8a6b-8c7d-9e0f1a2b3c4d"
func fakeStatus(state outbound.CodeRunState, request outbound.CodeRunRequest) outbound.CodeRunStatus {
if state != outbound.CodeRunFinished {
return outbound.CodeRunStatus{State: state}
@@ -143,13 +183,15 @@ func fakeStatus(state outbound.CodeRunState, request outbound.CodeRunRequest) ou
PassedTests: len(group.Tests), TotalTests: len(group.Tests),
})
}
return outbound.CodeRunStatus{State: state, Result: &result}
return outbound.CodeRunStatus{State: state, RunID: fakeRunID, Result: &result}
}
type fakeJobStore struct {
requeue outbound.PracticeRequeue
failedWith outbound.PracticeJobErrorClass
completed bool
// runnerRunID is the run ID that Complete stored.
runnerRunID uuid.UUID
}
func (store *fakeJobStore) Claim(context.Context, outbound.PracticeJobClaim) (outbound.ClaimedPracticeJob, bool, error) {
@@ -173,8 +215,9 @@ func (store *fakeJobStore) Requeue(_ context.Context, _ outbound.ClaimedPractice
return true, nil
}
func (store *fakeJobStore) Complete(context.Context, outbound.ClaimedPracticeJob, domain.PracticeRunResult) (bool, error) {
func (store *fakeJobStore) Complete(_ context.Context, _ outbound.ClaimedPracticeJob, runnerRunID uuid.UUID, _ domain.PracticeRunResult) (bool, error) {
store.completed = true
store.runnerRunID = runnerRunID
return true, nil
}