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 // with the opaque ID of the runner run that produced the result, 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, runnerRunID uuid.UUID, 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) }