Made the create command require a port number
This commit is contained in:
51
process.go
51
process.go
@@ -15,12 +15,20 @@ type ProcessManager struct {
|
||||
sync.RWMutex
|
||||
ActiveInstances map[string]*exec.Cmd
|
||||
StdinPipes map[string]io.WriteCloser
|
||||
LogBuffers map[string]*InstanceLogBuffer
|
||||
}
|
||||
|
||||
type InstanceLogBuffer struct {
|
||||
sync.RWMutex
|
||||
Lines []string
|
||||
MaxLines int
|
||||
}
|
||||
|
||||
func NewProcessManager() *ProcessManager {
|
||||
return &ProcessManager{
|
||||
ActiveInstances: make(map[string]*exec.Cmd),
|
||||
StdinPipes: make(map[string]io.WriteCloser),
|
||||
LogBuffers: map[string]*InstanceLogBuffer{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +74,7 @@ func (pm *ProcessManager) StartInstance(name string, version string, options VsS
|
||||
|
||||
pm.ActiveInstances[name] = cmd
|
||||
pm.StdinPipes[name] = stdinPipe
|
||||
pm.LogBuffers[name] = NewInstanceLogBuffer(200)
|
||||
|
||||
go pm.streamLogs(name, stdoutPipe)
|
||||
go pm.watchProcessExit(name, cmd)
|
||||
@@ -79,9 +88,17 @@ func (pm *ProcessManager) streamLogs(name string, stdout io.ReadCloser) {
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
|
||||
for scanner.Scan() {
|
||||
//stream straight to the manager console terminal,
|
||||
// but later dispatch payloads to our web UI or a log file
|
||||
fmt.Printf("[%s]: %s\n", name, scanner.Text())
|
||||
line := scanner.Text()
|
||||
|
||||
fmt.Printf("[%s]: %s\n", name, line)
|
||||
|
||||
pm.RLock()
|
||||
buf, exists := pm.LogBuffers[name]
|
||||
pm.RUnlock()
|
||||
|
||||
if exists {
|
||||
buf.Append(line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,3 +136,31 @@ func (pm *ProcessManager) SendCommand(name string, command string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewInstanceLogBuffer(maxLines int) *InstanceLogBuffer {
|
||||
return &InstanceLogBuffer{
|
||||
Lines: make([]string, 0, maxLines),
|
||||
MaxLines: maxLines,
|
||||
}
|
||||
}
|
||||
|
||||
func (lb *InstanceLogBuffer) Append(line string) {
|
||||
lb.Lock()
|
||||
defer lb.Unlock()
|
||||
|
||||
if len(lb.Lines) >= lb.MaxLines {
|
||||
// Shift array out by dropping index 0
|
||||
lb.Lines = lb.Lines[1:]
|
||||
}
|
||||
lb.Lines = append(lb.Lines, line)
|
||||
}
|
||||
|
||||
func (lb *InstanceLogBuffer) GetSnapshot() []string {
|
||||
lb.RLock()
|
||||
defer lb.RUnlock()
|
||||
|
||||
// Return a copy so the caller can safely iterate or serialize to JSON without lock conflicts
|
||||
snapshot := make([]string, len(lb.Lines))
|
||||
copy(snapshot, lb.Lines)
|
||||
return snapshot
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user