Skip to content

Commit

Permalink
feat: impl interface ProcessHook to custom start hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zaigie committed Mar 31, 2024
1 parent 48c0c1b commit 5b83c7c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
28 changes: 26 additions & 2 deletions cmd/gosup/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"fmt"
"log"
"os"
Expand All @@ -10,22 +11,45 @@ import (
"github.com/zaigie/gosup/process"
)

type MyProcessHook struct {
}

func (hook MyProcessHook) BeforeWait(ctx process.HookContext) {
go func() {
scanner := bufio.NewScanner(ctx.Stdout)
for scanner.Scan() {
fmt.Printf("[%s]STDOUT: %s\n", ctx.Params["prefix"], scanner.Text())
}
}()

go func() {
scanner := bufio.NewScanner(ctx.Stderr)
for scanner.Scan() {
fmt.Printf("[%s]STDERR: %s\n", ctx.Params["prefix"], scanner.Text())
}
}()
}

func main() {
pm := process.NewManager()
// defer pm.KillAll()

wd, _ := os.Getwd()
scriptPath := filepath.Join(wd, "test/run.py")
args1 := []string{"-u", scriptPath}
_, err := pm.Start("python", args1, nil)
hook := MyProcessHook{}
hookParams := map[string]interface{}{
"prefix": "hello",
}
_, err := pm.Start("python", args1, hook, hookParams)
if err != nil {
fmt.Println(err)
}

time.Sleep(2 * time.Second)

args2 := []string{"-u", scriptPath}
_, err = pm.Start("python", args2, nil)
_, err = pm.Start("python", args2, nil, nil)
if err != nil {
fmt.Println(err)
}
Expand Down
25 changes: 20 additions & 5 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ func NewManager() *ProcessManager {
}
}

type HookContext struct {
Stdout io.ReadCloser
Stderr io.ReadCloser
Params map[string]interface{}
}

type ProcessHook interface {
BeforeWait(ctx HookContext)
}

type Process struct {
Cmd *exec.Cmd
Before func(stdout, stderr io.ReadCloser)
Hook ProcessHook
Stdout io.ReadCloser
Stderr io.ReadCloser
}
Expand Down Expand Up @@ -59,7 +69,7 @@ func (pm *ProcessManager) Get(id int) (*Process, error) {
return process, nil
}

func (pm *ProcessManager) Start(name string, args []string, beforeWait func(stdout, stderr io.ReadCloser)) (int, error) {
func (pm *ProcessManager) Start(name string, args []string, hook ProcessHook, hookParams map[string]interface{}) (int, error) {
pm.mu.Lock()
id := pm.nextID
pm.nextID++
Expand All @@ -81,15 +91,20 @@ func (pm *ProcessManager) Start(name string, args []string, beforeWait func(stdo
}

pm.mu.Lock()
pm.Process[id] = &Process{Cmd: cmd, Before: beforeWait, Stdout: stdoutPipe, Stderr: stderrPipe}
pm.Process[id] = &Process{Cmd: cmd, Hook: hook, Stdout: stdoutPipe, Stderr: stderrPipe}
pm.wg.Add(1)
pm.mu.Unlock()

go func() {
defer stdoutPipe.Close()
defer stderrPipe.Close()
if beforeWait != nil {
beforeWait(stdoutPipe, stderrPipe)
if pm.Process[id].Hook != nil {
hookCtx := HookContext{
Stdout: stdoutPipe,
Stderr: stderrPipe,
Params: hookParams,
}
pm.Process[id].Hook.BeforeWait(hookCtx)
} else {
handlePipe(id, stdoutPipe, stderrPipe)
}
Expand Down

0 comments on commit 5b83c7c

Please sign in to comment.