worker

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2020 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package worker provides a job-processing interface based on a sandbox implementation.

Index

Constants

View Source
const (
	VerdictCompileError = "Compile Error"
	VerdictScored       = "Scored"
	VerdictAccepted     = "Accepted"
)
View Source
const CompareFilename = "compare"

The filename of the "compare" binary.

Variables

This section is empty.

Functions

func Compile

func Compile(c *CompileContext) (bool, error)

Compile performs compilation. Returns whether the compilation succeeds.

func MissingTests

func MissingTests(tests []*models.TestGroupWithTests, results map[int]*models.TestResult) []*models.Test

MissingTests finds all the tests that are missing a TestResult.

func Run

func Run(sandbox Sandbox, r *RunContext) error

Run runs a RunContext.

func RunCommand

func RunCommand(l models.Language) (string, []string, error)

RunnCommand returns the run command (command, args list) for the language.

func Score

func Score(s *ScoreContext) error

Score does scoring on a submission and updates the user's ProblemResult.

func UpdateVerdict

func UpdateVerdict(tests []*models.TestGroupWithTests, sub *models.Submission)

Update the submission's verdict.

Types

type CompileAction

type CompileAction struct {
	Source  string
	Command []*exec.Cmd
	Output  string
}

CompileAction is an action revolving writing the source into a file in "Source", compile it with "Command" and taking the "Output" as the result.

func CompileBatch

func CompileBatch(l models.Language) (*CompileAction, string, error)

CompileBatch returns a compile action, along with the required batch filename in order to successfully compile.

func CompileSingle

func CompileSingle(l models.Language) (*CompileAction, error)

CompileSingle creates a compilation command for a single source code file. Sometimes this is as simple as "copy".

func (*CompileAction) Perform

func (c *CompileAction) Perform(cwd string) (succeeded bool, messages []byte)

Perform performs the compile action on the given directory. The directory MUST contain all files given by the Problem, PLUS the written "Source" file.

type CompileContext

type CompileContext struct {
	DB      *sqlx.Tx
	Sub     *models.Submission
	Problem *models.Problem
}

CompileContext is the information needed to perform compilation.

type Queue

type Queue struct {
	DB      *db.DB
	Sandbox Sandbox
}

Queue implements a queue that runs each job one by one.

func (*Queue) HandleJob

func (q *Queue) HandleJob(job *models.Job) error

HandleJob dispatches a job.

func (*Queue) Start

func (q *Queue) Start()

Start starts the queue. It is blocking, so might wanna "go run" it.

type RunContext

type RunContext struct {
	DB        *sqlx.Tx
	Sub       *models.Submission
	Problem   *models.Problem
	TestGroup *models.TestGroup
	Test      *models.Test
}

RunContext is the context needed to run a test.

func (*RunContext) CompareInput

func (r *RunContext) CompareInput(submissionOutput []byte) (input *SandboxInput, useComparator bool, err error)

CompareInput creates a SandboxInput for running the comparator. Also returns whether we have diff-based or comparator-based input.

func (*RunContext) CompiledSource

func (r *RunContext) CompiledSource() (bool, []byte)

CompiledSource returns the CompiledSource. Returns false when the submission hasn't been compiled. Returns nil if the submission failed to compile.

func (*RunContext) MemoryLimit

func (r *RunContext) MemoryLimit() int

MemoryLimit returns the memory limit of the context, in Kilobytes.

func (*RunContext) RunInput

func (r *RunContext) RunInput(source []byte) (*SandboxInput, error)

RunInput creates a SandboxInput for running the submission's source.

func (*RunContext) TimeLimit

func (r *RunContext) TimeLimit() time.Duration

TimeLimit returns the time limit of the context, in time.Duration.

type Sandbox

type Sandbox interface {
	Run(*SandboxInput) (*SandboxOutput, error)
}

Sandbox provides a way to run an arbitary command within a sandbox, with configured input/outputs and proper time and memory limits.

kjudge currently implements two sandboxes, "isolate" (which requires "github.com/ioi/isolate" to be available) and "raw" (NOT RECOMMENDED, RUN AT YOUR OWN RISK). Which sandbox is used can be set at runtime with a command-line switch.

type SandboxInput

type SandboxInput struct {
	Command     string            `json:"command"`      // The passed command
	Args        []string          `json:"args"`         // any additional arguments, if needed
	Files       map[string][]byte `json:"files"`        // Any additional files needed
	TimeLimit   time.Duration     `json:"time_limit"`   // The given time-limit
	MemoryLimit int               `json:"memory_limit"` // in KBs

	CompiledSource []byte `json:"compiled_source"` // Should be written down to the CWD as a file named "code", as the command expects
	Input          []byte `json:"input"`
}

SandboxInput is the input to a sandbox.

func (*SandboxInput) CopyTo

func (input *SandboxInput) CopyTo(cwd string) error

CopyTo copies all the files it contains into cwd.

type SandboxOutput

type SandboxOutput struct {
	Success     bool          `json:"success"`      // Whether the command exited zero.
	RunningTime time.Duration `json:"running_time"` // The running time of the command.
	MemoryUsed  int           `json:"memory_used"`  // in KBs

	Stdout       []byte `json:"stdout"`
	Stderr       []byte `json:"stderr"`
	ErrorMessage string `json:"error_message,omitempty"`
}

SandboxOutput is the output which the sandbox needs to give back.

type ScoreContext

type ScoreContext struct {
	DB      *sqlx.Tx
	Sub     *models.Submission
	Problem *models.Problem
	Contest *models.Contest
}

ScoreContext is a context for calculating a submission's score and update the user's problem scores.

func (*ScoreContext) CompareScores

func (s *ScoreContext) CompareScores(subs []*models.Submission) *models.ProblemResult

CompareScores compare the submission results and return the best one. If nil is returned, then the problem result should just be removed. The submissions list passed in must be sorted in the OrderBy order.

func (*ScoreContext) CompiledSource

func (s *ScoreContext) CompiledSource() (bool, []byte)

CompiledSource returns the CompiledSource. Returns false when the submission hasn't been compiled. Returns nil if the submission failed to compile.

func (*ScoreContext) ComputePenalties

func (s *ScoreContext) ComputePenalties(sub *models.Submission) error

ComputePenalties compute penalty values for each submission, based on the PenaltyPolicy.

func (*ScoreContext) TestResults

func (s *ScoreContext) TestResults() (map[int]*models.TestResult, error)

TestResults returns the submission's test results, mapped by the test's ID.

Directories

Path Synopsis
Package isolate provides a safe sandbox on Linux using the "isolate" program.
Package isolate provides a safe sandbox on Linux using the "isolate" program.
Raw implements a "raw" sandbox.
Raw implements a "raw" sandbox.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL