You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.8 KiB
84 lines
1.8 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"log"
|
|
"io"
|
|
"time"
|
|
"math/rand"
|
|
"os"
|
|
"errors"
|
|
"sync"
|
|
"lcthw.dev/go/ozai/config"
|
|
)
|
|
|
|
func LaunchLogger(in io.Reader, out io.Writer, err error) {
|
|
if err != nil { log.Fatal(err) }
|
|
|
|
go func() {
|
|
if _, err := io.Copy(out, in)
|
|
|
|
err != nil { log.Printf("LOGGER: %v", err) }
|
|
}()
|
|
}
|
|
|
|
func LaunchProcess(proc *config.Process, wg sync.WaitGroup) {
|
|
defer wg.Done()
|
|
|
|
for {
|
|
proc.ExecCmd = exec.Command(proc.Command, proc.Args...)
|
|
if errors.Is(proc.ExecCmd.Err, exec.ErrDot) {
|
|
proc.ExecCmd.Err = nil
|
|
}
|
|
|
|
fmt.Println("STARTING", proc.Name)
|
|
|
|
stderr, err := proc.ExecCmd.StderrPipe();
|
|
LaunchLogger(stderr, os.Stdout, err)
|
|
|
|
stdout, err := proc.ExecCmd.StdoutPipe();
|
|
LaunchLogger(stdout, os.Stdout, err)
|
|
|
|
proc.ExecCmd.Start()
|
|
fmt.Println("WAITING for", proc.Name)
|
|
|
|
fmt.Println("SENDING READY on channel")
|
|
proc.Ready<- "ready"
|
|
proc.ExecCmd.Wait()
|
|
fmt.Println("PROCESS", proc.Name, "EXITED")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
config.Load()
|
|
|
|
fmt.Println("CONFIG:", config.Settings.ConfigPath)
|
|
fmt.Println("COMMANDs:", config.Settings.Processes)
|
|
var wg sync.WaitGroup
|
|
|
|
for name, proc := range config.Settings.Processes {
|
|
fmt.Println("PROCESS:", name)
|
|
wg.Add(1)
|
|
|
|
proc.Ready = make(chan string)
|
|
go LaunchProcess(&proc, wg)
|
|
|
|
go func() {
|
|
for {
|
|
fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!! killer runs")
|
|
is_ready := <-proc.Ready
|
|
|
|
fmt.Println("is_ready returned", is_ready)
|
|
sleep_for := rand.Int() % 10 + 1
|
|
time.Sleep(time.Duration(sleep_for) * time.Second)
|
|
fmt.Println("!!!!!!!!!!!!!!!!!!! TIME TO DIE!!!!!")
|
|
|
|
err := proc.ExecCmd.Process.Kill()
|
|
if err != nil { log.Printf("killer says: %v", err) }
|
|
}
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|