input channel mapping update
This commit is contained in:
@@ -2,7 +2,6 @@ package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -144,6 +143,7 @@ func (h *Handler) ResultHandler(w http.ResponseWriter, r *http.Request) {
|
||||
"status": submission.Status,
|
||||
"language": submission.Language,
|
||||
"output": submission.Output,
|
||||
"input": submission.Input,
|
||||
}
|
||||
|
||||
// Add error information if available
|
||||
@@ -190,6 +190,55 @@ func (h *Handler) QueueStatsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// SubmitInputHandler handles interactive input submission
|
||||
func (h *Handler) SubmitInputHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow POST method
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse the request body
|
||||
var inputRequest struct {
|
||||
ID string `json:"id"`
|
||||
Input string `json:"input"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&inputRequest); err != nil {
|
||||
http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate the request
|
||||
if inputRequest.ID == "" {
|
||||
http.Error(w, "ID is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the submission from the map
|
||||
h.mu.Lock()
|
||||
submission, exists := h.submissions[inputRequest.ID]
|
||||
h.mu.Unlock()
|
||||
|
||||
if !exists {
|
||||
http.Error(w, "Submission not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the submission is waiting for input
|
||||
if submission.Status != "waiting_for_input" {
|
||||
http.Error(w, "Submission is not waiting for input", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Send the input to the execution service
|
||||
h.executionService.SubmitInput(submission, inputRequest.Input)
|
||||
|
||||
// Return success response
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{"status": "input_submitted"})
|
||||
}
|
||||
|
||||
// HealthCheckHandler handles health check requests
|
||||
func (h *Handler) HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Only allow GET method
|
||||
|
||||
@@ -24,6 +24,7 @@ func SetupRoutes() http.Handler {
|
||||
mux.HandleFunc("/submit", h.SubmitHandler)
|
||||
mux.HandleFunc("/status", h.StatusHandler)
|
||||
mux.HandleFunc("/result", h.ResultHandler)
|
||||
mux.HandleFunc("/submit-input", h.SubmitInputHandler)
|
||||
mux.HandleFunc("/queue-stats", h.QueueStatsHandler)
|
||||
mux.HandleFunc("/health", h.HealthCheckHandler)
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/arnab-afk/monaco/internal/models"
|
||||
@@ -19,6 +20,9 @@ import (
|
||||
// ExecutionService manages code execution
|
||||
type ExecutionService struct {
|
||||
queue *queue.JobQueue
|
||||
mu sync.Mutex
|
||||
// Map of submission ID to input channel for interactive programs
|
||||
inputChannels map[string]chan string
|
||||
}
|
||||
|
||||
// CodeExecutionJob represents a code execution job
|
||||
@@ -30,7 +34,8 @@ type CodeExecutionJob struct {
|
||||
// NewExecutionService creates a new execution service
|
||||
func NewExecutionService() *ExecutionService {
|
||||
return &ExecutionService{
|
||||
queue: queue.NewJobQueue(5), // 5 concurrent workers
|
||||
queue: queue.NewJobQueue(5), // 5 concurrent workers
|
||||
inputChannels: make(map[string]chan string),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -601,6 +606,25 @@ func (s *ExecutionService) updateSubmissionResult(submission *models.CodeSubmiss
|
||||
submission.Output = formattedOutput + rawOutput
|
||||
}
|
||||
|
||||
// SubmitInput submits input to a running interactive program
|
||||
func (s *ExecutionService) SubmitInput(submission *models.CodeSubmission, input string) {
|
||||
s.mu.Lock()
|
||||
inputChan, exists := s.inputChannels[submission.ID]
|
||||
s.mu.Unlock()
|
||||
|
||||
if !exists {
|
||||
log.Printf("[ERROR] No input channel found for submission %s", submission.ID)
|
||||
return
|
||||
}
|
||||
|
||||
// Send the input to the channel
|
||||
inputChan <- input
|
||||
|
||||
// Update the submission status
|
||||
submission.Status = "running"
|
||||
submission.Output += "[Input] " + input + "\n"
|
||||
}
|
||||
|
||||
// GetQueueStats returns statistics about the job queue
|
||||
func (s *ExecutionService) GetQueueStats() models.QueueStats {
|
||||
return s.queue.GetStats()
|
||||
|
||||
@@ -4,16 +4,18 @@ import "time"
|
||||
|
||||
// CodeSubmission represents a code submission for execution
|
||||
type CodeSubmission struct {
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Language string `json:"language"`
|
||||
Input string `json:"input"`
|
||||
Status string `json:"status"` // "pending", "queued", "running", "completed", "failed"
|
||||
QueuedAt time.Time `json:"queuedAt,omitempty"`
|
||||
StartedAt time.Time `json:"startedAt,omitempty"`
|
||||
CompletedAt time.Time `json:"completedAt,omitempty"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Language string `json:"language"`
|
||||
Input string `json:"input"`
|
||||
Status string `json:"status"` // "pending", "queued", "running", "waiting_for_input", "completed", "failed"
|
||||
QueuedAt time.Time `json:"queuedAt,omitempty"`
|
||||
StartedAt time.Time `json:"startedAt,omitempty"`
|
||||
CompletedAt time.Time `json:"completedAt,omitempty"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
IsInteractive bool `json:"isInteractive,omitempty"` // Whether the program requires interactive input
|
||||
CurrentPrompt string `json:"currentPrompt,omitempty"` // Current input prompt if waiting for input
|
||||
}
|
||||
|
||||
// ExecutionResult represents the result of code execution
|
||||
|
||||
Reference in New Issue
Block a user