feat(shell): add parallel bash shell subsystem auto-started per workspace
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"io"
|
||||
|
||||
"codespace/internal/shell"
|
||||
"codespace/internal/workspace"
|
||||
)
|
||||
|
||||
// ShellService manages interactive shells for workspaces.
|
||||
type ShellService struct {
|
||||
workspaces workspace.Manager
|
||||
shells shell.Manager
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewShellService creates a ShellService.
|
||||
// If lg is nil, slog.Default() is used.
|
||||
func NewShellService(workspaces workspace.Manager, shells shell.Manager, lg *slog.Logger) *ShellService {
|
||||
if lg == nil {
|
||||
lg = slog.Default()
|
||||
}
|
||||
return &ShellService{workspaces: workspaces, shells: shells, logger: lg}
|
||||
}
|
||||
|
||||
// Start starts an interactive shell for the given workspace.
|
||||
func (s *ShellService) Start(workspaceID string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.shells.Start(workspaceID, ws.Root); err != nil {
|
||||
s.logger.Error("shell start failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
}
|
||||
status := s.shells.Status(workspaceID)
|
||||
s.logger.Info("shell started", "workspace_id", workspaceID, "pid", status.PID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop stops the interactive shell for the given workspace.
|
||||
func (s *ShellService) Stop(workspaceID string) error {
|
||||
if err := s.shells.Stop(workspaceID); err != nil {
|
||||
s.logger.Error("shell stop failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
}
|
||||
s.logger.Info("shell stopped", "workspace_id", workspaceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restart restarts the interactive shell for the given workspace.
|
||||
func (s *ShellService) Restart(workspaceID string) error {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.shells.Restart(workspaceID, ws.Root); err != nil {
|
||||
s.logger.Error("shell restart failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
}
|
||||
status := s.shells.Status(workspaceID)
|
||||
s.logger.Info("shell restarted", "workspace_id", workspaceID, "pid", status.PID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Status returns the shell status for the given workspace.
|
||||
func (s *ShellService) Status(workspaceID string) (shell.Status, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return shell.Status{}, err
|
||||
}
|
||||
return s.shells.Status(workspaceID), nil
|
||||
}
|
||||
|
||||
// Subscribe subscribes to output events for the workspace shell.
|
||||
func (s *ShellService) Subscribe(workspaceID string) (shell.Subscription, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.shells.Subscribe(workspaceID)
|
||||
}
|
||||
|
||||
// Input returns the stdin writer for the workspace shell.
|
||||
func (s *ShellService) Input(workspaceID string) (io.WriteCloser, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.shells.Stdin(workspaceID)
|
||||
}
|
||||
|
||||
// ExitStatus returns the exit status for the workspace shell.
|
||||
func (s *ShellService) ExitStatus(workspaceID string) (shell.ExitInfo, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return shell.ExitInfo{}, err
|
||||
}
|
||||
return s.shells.ExitStatus(workspaceID)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"log/slog"
|
||||
|
||||
"codespace/internal/process"
|
||||
"codespace/internal/shell"
|
||||
"codespace/internal/workspace"
|
||||
)
|
||||
|
||||
@@ -11,16 +12,17 @@ import (
|
||||
type WorkspaceService struct {
|
||||
workspaces workspace.Manager
|
||||
processes process.Manager
|
||||
shells shell.Manager
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewWorkspaceService creates a WorkspaceService.
|
||||
// If lg is nil, slog.Default() is used.
|
||||
func NewWorkspaceService(workspaces workspace.Manager, processes process.Manager, lg *slog.Logger) *WorkspaceService {
|
||||
func NewWorkspaceService(workspaces workspace.Manager, processes process.Manager, shells shell.Manager, lg *slog.Logger) *WorkspaceService {
|
||||
if lg == nil {
|
||||
lg = slog.Default()
|
||||
}
|
||||
return &WorkspaceService{workspaces: workspaces, processes: processes, logger: lg}
|
||||
return &WorkspaceService{workspaces: workspaces, processes: processes, shells: shells, logger: lg}
|
||||
}
|
||||
|
||||
// Create creates a new workspace.
|
||||
@@ -30,6 +32,11 @@ func (s *WorkspaceService) Create(id string) (*workspace.Workspace, error) {
|
||||
return nil, err
|
||||
}
|
||||
s.logger.Info("workspace created", "workspace_id", id)
|
||||
|
||||
if err := s.shells.Start(ws.ID, ws.Root); err != nil {
|
||||
s.logger.Warn("failed to auto-start shell for workspace", "workspace_id", id, "error", err)
|
||||
}
|
||||
|
||||
return ws, nil
|
||||
}
|
||||
|
||||
@@ -45,6 +52,10 @@ func (s *WorkspaceService) List() ([]workspace.Workspace, error) {
|
||||
|
||||
// Delete stops any running process for the workspace, then removes it.
|
||||
func (s *WorkspaceService) Delete(id string) error {
|
||||
if err := s.shells.Stop(id); err != nil {
|
||||
s.logger.Warn("failed to stop workspace shell before delete", "workspace_id", id, "error", err)
|
||||
}
|
||||
|
||||
status := s.processes.Status(id)
|
||||
if status.Running {
|
||||
if err := s.processes.Stop(id); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user