use stderr instead of a control socket with pluginHost
This commit is contained in:
35
main.go
35
main.go
@@ -273,43 +273,24 @@ func (gloom *GLoom) RegisterPlugin(pluginPath string, name string, domains []str
|
|||||||
|
|
||||||
pathStr := strconv.FormatUint(uint64(rand.Uint64()), 16)
|
pathStr := strconv.FormatUint(uint64(rand.Uint64()), 16)
|
||||||
socketPath := path.Join(gloom.tmpDir, pathStr+".sock")
|
socketPath := path.Join(gloom.tmpDir, pathStr+".sock")
|
||||||
controlPath := path.Join(gloom.tmpDir, pathStr+"-control.sock")
|
|
||||||
|
|
||||||
slog.Debug("Starting pluginHost", "pluginPath", pluginPath, "socketPath", socketPath, "controlPath", controlPath)
|
slog.Debug("Starting pluginHost", "pluginPath", pluginPath)
|
||||||
|
|
||||||
processPath := path.Join(gloom.gloomDir, "pluginHost")
|
processPath := path.Join(gloom.gloomDir, "pluginHost")
|
||||||
args := []string{pluginPath, socketPath, controlPath}
|
args := []string{pluginPath, socketPath}
|
||||||
|
|
||||||
cmd := exec.Command(processPath, args...)
|
cmd := exec.Command(processPath, args...)
|
||||||
|
stderrPipe, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get stderr pipe: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return fmt.Errorf("failed to start pluginHost: %w", err)
|
return fmt.Errorf("failed to start pluginHost: %w", err)
|
||||||
}
|
}
|
||||||
process := cmd.Process
|
process := cmd.Process
|
||||||
|
|
||||||
timeout := time.After(5 * time.Second)
|
reader := bufio.NewReader(stderrPipe)
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-timeout:
|
|
||||||
_ = process.Signal(os.Interrupt)
|
|
||||||
return fmt.Errorf("timed out waiting for pluginHost to start (this is likely a GLoom bug)")
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
_, err := os.Stat(controlPath)
|
|
||||||
if err == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := net.DialTimeout("unix", controlPath, 5*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
_ = process.Signal(os.Interrupt)
|
|
||||||
return fmt.Errorf("failed to connect to plugin control socket: %w", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
reader := bufio.NewReader(conn)
|
|
||||||
readTimeout := time.After(30 * time.Second)
|
readTimeout := time.After(30 * time.Second)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|||||||
@@ -19,12 +19,15 @@ var controlPath string
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if len(os.Args) < 3 {
|
if len(os.Args) < 3 {
|
||||||
fmt.Fprintf(os.Stderr, "Usage: pluginHost <pluginPath> <socketPath>")
|
fmt.Fprintf(os.Stderr, "Usage: pluginHost <pluginPath> <socketPath> [controlPath]")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginPath = os.Args[1]
|
pluginPath = os.Args[1]
|
||||||
socketPath = os.Args[2]
|
socketPath = os.Args[2]
|
||||||
|
// Idk why I originally wrote this solution when stderr is literally just the best solution for me, but this
|
||||||
|
// makes the pluginHost more generally useful outside of GLoom, so I'm keeping it
|
||||||
|
// TODO: maybe make it a compiler flag, though I'm sure its not making the binary *that* much bigger
|
||||||
if len(os.Args) > 3 {
|
if len(os.Args) > 3 {
|
||||||
controlPath = os.Args[3]
|
controlPath = os.Args[3]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user