This commit is contained in:
Chiran Fernando 2025-05-18 16:11:04 +05:30
parent 68015ae8fc
commit 561b8fb637
3 changed files with 40 additions and 40 deletions

View file

@ -4,14 +4,14 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"runtime"
"strings"
"sync" "sync"
"syscall" "syscall"
"time" "time"
"strings"
"runtime"
"github.com/wso2/open-mcp-auth-proxy/internal/config" "github.com/wso2/open-mcp-auth-proxy/internal/config"
"github.com/wso2/open-mcp-auth-proxy/internal/logging" logger "github.com/wso2/open-mcp-auth-proxy/internal/logging"
) )
// Manager handles starting and graceful shutdown of subprocesses // Manager handles starting and graceful shutdown of subprocesses
@ -32,39 +32,39 @@ func NewManager() *Manager {
// EnsureDependenciesAvailable checks and installs required package executors // EnsureDependenciesAvailable checks and installs required package executors
func EnsureDependenciesAvailable(command string) error { func EnsureDependenciesAvailable(command string) error {
// Always ensure npx is available regardless of the command // Always ensure npx is available regardless of the command
if _, err := exec.LookPath("npx"); err != nil { if _, err := exec.LookPath("npx"); err != nil {
// npx is not available, check if npm is installed // npx is not available, check if npm is installed
if _, err := exec.LookPath("npm"); err != nil { if _, err := exec.LookPath("npm"); err != nil {
return fmt.Errorf("npx not found and npm not available; please install Node.js from https://nodejs.org/") return fmt.Errorf("npx not found and npm not available; please install Node.js from https://nodejs.org/")
} }
// Try to install npx using npm // Try to install npx using npm
logger.Info("npx not found, attempting to install...") logger.Info("npx not found, attempting to install...")
var cmd *exec.Cmd var cmd *exec.Cmd
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
cmd = exec.Command("npm.cmd", "install", "-g", "npx") cmd = exec.Command("npm.cmd", "install", "-g", "npx")
} else { } else {
cmd = exec.Command("npm", "install", "-g", "npx") cmd = exec.Command("npm", "install", "-g", "npx")
} }
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to install npx: %w", err) return fmt.Errorf("failed to install npx: %w", err)
} }
logger.Info("npx installed successfully") logger.Info("npx installed successfully")
} }
// Check if uv is needed based on the command // Check if uv is needed based on the command
if strings.Contains(command, "uv ") { if strings.Contains(command, "uv ") {
if _, err := exec.LookPath("uv"); err != nil { if _, err := exec.LookPath("uv"); err != nil {
return fmt.Errorf("command requires uv but it's not installed; please install it following instructions at https://github.com/astral-sh/uv") return fmt.Errorf("command requires uv but it's not installed; please install it following instructions at https://github.com/astral-sh/uv")
} }
} }
return nil return nil
} }
// SetShutdownDelay sets the maximum time to wait for graceful shutdown // SetShutdownDelay sets the maximum time to wait for graceful shutdown
@ -168,7 +168,7 @@ func (m *Manager) IsRunning() bool {
// Shutdown gracefully terminates the subprocess // Shutdown gracefully terminates the subprocess
func (m *Manager) Shutdown() { func (m *Manager) Shutdown() {
m.mutex.Lock() m.mutex.Lock()
processToTerminate := m.process // Local copy of the process reference processToTerminate := m.process // Local copy of the process reference
processGroupToTerminate := m.processGroup processGroupToTerminate := m.processGroup
m.mutex.Unlock() m.mutex.Unlock()
@ -315,4 +315,4 @@ func (m *Manager) Shutdown() {
case <-time.After(m.shutdownDelay): case <-time.After(m.shutdownDelay):
logger.Warn("Subprocess termination timed out") logger.Warn("Subprocess termination timed out")
} }
} }

View file

@ -20,4 +20,4 @@ func getProcessGroup(pid int) (int, error) {
// killProcessGroup kills a process group on Unix systems // killProcessGroup kills a process group on Unix systems
func killProcessGroup(pgid int, signal syscall.Signal) error { func killProcessGroup(pgid int, signal syscall.Signal) error {
return syscall.Kill(-pgid, signal) return syscall.Kill(-pgid, signal)
} }

View file

@ -24,4 +24,4 @@ func killProcessGroup(pgid int, signal syscall.Signal) error {
// On Windows, we'll use the process handle directly // On Windows, we'll use the process handle directly
// This function shouldn't be called on Windows, but we provide it for compatibility // This function shouldn't be called on Windows, but we provide it for compatibility
return nil return nil
} }