Skip to content

Commit

Permalink
fix blocking background and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
atterpac committed Dec 13, 2023
1 parent 54481d4 commit 4be3803
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cmd/refresh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
var version string = "0.3.0"
var version string = "0.3.1"

var rootPath string
var execCommand string
Expand Down
2 changes: 1 addition & 1 deletion engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Config struct {
BackgroundExec string `toml:"background_exec"`
BackgroundStruct Execute
Ignore Ignore `toml:"ignore"`
ExecList []string `toml:"exec_lifecycle"`
ExecList []string `toml:"exec_list"`
ExecStruct []Execute
ignoreMap ignoreMap
LogLevel string `toml:"log_level"`
Expand Down
14 changes: 3 additions & 11 deletions engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package engine

import (
"fmt"
"io"
"log/slog"
"os"
Expand Down Expand Up @@ -32,17 +31,10 @@ func (engine *Engine) Start() {
engine.Config.ignoreMap.git = readGitIgnore(engine.Config.RootPath)
}
if engine.Config.BackgroundStruct.Cmd != "" {
err := execFromString(engine.Config.BackgroundStruct.Cmd)
if err != nil {
slog.Error("Running background process", "Process", engine.Config.BackgroundStruct.Cmd)
os.Exit(1)
}
}
err := execFromString(engine.Config.BackgroundExec)
if err != nil {
slog.Error(fmt.Sprintf("Running Background Process: %s", err.Error()))
os.Exit(1)
go backgroundExec(engine.Config.BackgroundStruct.Cmd)
}
go backgroundExec(engine.Config.BackgroundExec)
go engine.reloadProcess()
engine.watch()
}

Expand Down
24 changes: 14 additions & 10 deletions engine/execute.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"bytes"
"fmt"
"log/slog"
"os"
Expand Down Expand Up @@ -103,6 +104,7 @@ func (engine *Engine) execFromList() error {
return nil
}
for _, exe := range engine.Config.ExecList {
slog.Debug("is Primary?", fmt.Sprintf("%v", exe), nextPrimary)
if nextPrimary {
slog.Debug("Reloading Process")
engine.Process, err = engine.startPrimary(exe)
Expand All @@ -112,7 +114,6 @@ func (engine *Engine) execFromList() error {
}
slog.Debug("Sucessfull refresh")
nextPrimary = false
return nil
}
switch exe {
case "":
Expand Down Expand Up @@ -145,31 +146,34 @@ func (engine *Engine) execFromList() error {
}
err = cmd.Wait()
if err != nil {
slog.Error("Running Execute: %s", exe)
slog.Error("Running Execute","command:", exe)
}
slog.Debug(fmt.Sprintf("Complete Exec Command: %s", exe))
}
return nil
}
return nil
}

func backgroundExec(runString string) {
commandSlice := generateExec(runString)
cmd := exec.Command(commandSlice[0], commandSlice[1:]...)
var out, err bytes.Buffer
// Let Process run in background
cmd.Stdout = &out
cmd.Stderr = &err
cmd.Start()
}

func execFromString(runString string) error {
if runString == "" {
return nil
}
commandSlice := generateExec(runString)
cmd := exec.Command(commandSlice[0], commandSlice[1:]...)
// Let Process run in background
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}

Expand Down
18 changes: 8 additions & 10 deletions engine/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import (

func (engine *Engine) reloadProcess() {
if engine.Config.ExecList == nil && engine.Config.ExecStruct == nil {
slog.Error("No exec commands found")
return
}
if engine.Config.ExecList != nil {
engine.reloadFromList()
}
if engine.Config.ExecStruct != nil {
engine.reloadFromStruct()
return
}
err := engine.execFromList()
if err != nil {
slog.Error(fmt.Sprintf("Running from exec list: %s", err))
}

}

func (engine *Engine) reloadFromStruct() {
Expand All @@ -29,16 +33,10 @@ func (engine *Engine) reloadFromStruct() {
}
}

func (engine *Engine) reloadFromList() {
err := engine.execFromList()
if err != nil {
slog.Error(fmt.Sprintf("Running from exec list: %s", err))
}
}

// Start process with exec command and a root path to call it in
func (engine *Engine) startPrimary(runString string) (*os.Process, error) {
var err error
slog.Debug("Starting Primary")
cmdExec := generateExec(runString)
cmd := exec.Command(cmdExec[0], cmdExec[1:]...)
// If an external slog is provided do not pipe stdout to the engine
Expand Down
1 change: 1 addition & 0 deletions example/example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ label = "My Project"
# Relative to this files location
root_path = "./test"
# Execute list
background_exec = ["pwd"]
exec_list = ["go mod tidy", "go build -o ./app", "KILL_STALE", "REFRESH", "./app"]
# debug | info | warn | error | mute
# Defaults to Info if not provided
Expand Down
10 changes: 1 addition & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/lmittmann/tint v1.0.3
github.com/rjeczalik/notify v0.9.3
github.com/shirou/gopsutil v3.21.11+incompatible
)

require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/sys v0.14.0 // indirect
)
require golang.org/x/sys v0.14.0 // indirect
21 changes: 0 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/lmittmann/tint v1.0.3 h1:W5PHeA2D8bBJVvabNfQD/XW9HPLZK1XoPZH0cq8NouQ=
github.com/lmittmann/tint v1.0.3/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY=
github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 4be3803

Please sign in to comment.