初始化
This commit is contained in:
60
server/runner/node_runner.go
Normal file
60
server/runner/node_runner.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type NodeRunner struct {
|
||||
TempDir string
|
||||
}
|
||||
|
||||
func (r *NodeRunner) Run(ctx context.Context, code string) (*ExecutionResult, error) {
|
||||
workDir := filepath.Join(r.TempDir, uuid.New().String())
|
||||
if err := os.MkdirAll(workDir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(workDir)
|
||||
|
||||
filePath := filepath.Join(workDir, "script.js")
|
||||
if err := ioutil.WriteFile(filePath, []byte(code), 0644); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, "node", "script.js")
|
||||
cmd.Dir = workDir
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
startTime := time.Now()
|
||||
err := cmd.Run()
|
||||
duration := time.Since(startTime).Milliseconds()
|
||||
|
||||
result := &ExecutionResult{
|
||||
Output: stdout.String(),
|
||||
Error: stderr.String(),
|
||||
Duration: duration,
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||
result.ExitCode = exitErr.ExitCode()
|
||||
} else {
|
||||
result.ExitCode = -1
|
||||
}
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
result.Error += "\nExecution timed out"
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user