mirror of
https://github.com/dreamstarsky/runbin.git
synced 2026-05-15 14:23:07 +00:00
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
func main() {
|
||||
// Load configuration
|
||||
cfg := config.LoadApi("config/server.yaml")
|
||||
cfg := config.LoadApi("config/api.yaml")
|
||||
|
||||
if cfg.App.Env == "release" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
@@ -4,11 +4,13 @@ storage:
|
||||
dsn: "host=localhost port=54320 user=postgres password=password dbname=postgres sslmode=disable"
|
||||
|
||||
limit:
|
||||
time: 10
|
||||
cpu: 1
|
||||
memory: 512
|
||||
time: 10.0 # s
|
||||
cpu: 1.0
|
||||
memory: 512 # MB
|
||||
size: 1024 # KB
|
||||
|
||||
process: 4
|
||||
process: 1
|
||||
|
||||
name: "猫猫"
|
||||
name: "default name"
|
||||
|
||||
compilerimage: "cpp_gcc-latest:latest"
|
||||
@@ -10,6 +10,7 @@ type LimitConfig struct {
|
||||
Cpu float32
|
||||
Memory int
|
||||
Time float32
|
||||
Size int
|
||||
}
|
||||
|
||||
type WorkerConfig struct {
|
||||
@@ -28,6 +29,7 @@ func LoadWorker(configFile string) *WorkerConfig {
|
||||
v.SetDefault("limit.cpu", 1.0)
|
||||
v.SetDefault("limit.time", 10.0)
|
||||
v.SetDefault("limit.memory", 512*1024)
|
||||
v.SetDefault("limit.size", 1024)
|
||||
v.SetDefault("process", 1)
|
||||
v.SetDefault("name", "default name")
|
||||
v.SetDefault("compilerimage", "cpp_gcc-latest:latest")
|
||||
|
||||
@@ -64,6 +64,7 @@ func compileCpp(ctx context.Context, task *model.Paste, cli *client.Client, tmpD
|
||||
|
||||
// Read compilation log
|
||||
if logData, err := os.ReadFile(filepath.Join(tmpDir, "compile.txt")); err == nil {
|
||||
logData = logData[:min(len(logData), cfg.Limit.Size)]
|
||||
task.CompileLog = string(logData)
|
||||
}
|
||||
|
||||
@@ -144,24 +145,26 @@ func runCpp(ctx context.Context, task *model.Paste, cli *client.Client, tmpDir s
|
||||
usagePath := filepath.Join(tmpDir, "usage.json")
|
||||
|
||||
// Read program output
|
||||
if outData, err := os.ReadFile(stdoutPath); err == nil {
|
||||
task.Stdout = string(outData)
|
||||
if stdoutData, err := os.ReadFile(stdoutPath); err == nil {
|
||||
stdoutData = stdoutData[:min(len(stdoutData), cfg.Limit.Size)]
|
||||
task.Stdout = string(stdoutData)
|
||||
}
|
||||
if outData, err := os.ReadFile(stderrPath); err == nil {
|
||||
task.Stderr = string(outData)
|
||||
if stderrData, err := os.ReadFile(stderrPath); err == nil {
|
||||
stderrData = stderrData[:min(len(stderrData), cfg.Limit.Size)]
|
||||
task.Stderr = string(stderrData)
|
||||
}
|
||||
if usageData, err := os.ReadFile(usagePath); err == nil {
|
||||
var usage Usage
|
||||
json.Unmarshal(usageData, &usage)
|
||||
task.MemoryUsageKb = int(usage.MaxMemory)
|
||||
task.ExecutionTimeMs = int(usage.RealTime * 1000)
|
||||
task.ExecutionTimeMs = int(usage.RealTime * 1000)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Worker) RunCppTask(ctx context.Context, task *model.Paste, cli *client.Client) error {
|
||||
// 临时文件夹
|
||||
tmpDir, err := os.MkdirTemp("", "cpp_compile_")
|
||||
tmpDir, err := os.MkdirTemp("/dev/shm/", "cpp_compile_")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create temp dir error: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user