Skip to content

Commit

Permalink
execute typings
Browse files Browse the repository at this point in the history
  • Loading branch information
atterpac committed May 4, 2024
1 parent ebb4621 commit c7cd902
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
30 changes: 20 additions & 10 deletions process/execute.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package process

import (
"os"
"os/exec"
"strings"
)

type Execute struct {
Cmd string `toml:"cmd" yaml:"cmd"` // Execute command
ChangeDir string `toml:"dir" yaml:"dir"` // If directory needs to be changed to call this command relative to the root path
IsBlocking bool `toml:"blocking" yaml:"blocking"` // Should the following executes wait for this one to complete
IsPrimary bool `toml:"primary" yaml:"primary"` // Only one primary command can be run at a time
DelayNext int `toml:"delay_next" yaml:"delay_next"` // Delay in milliseconds before running command
process *os.Process // Stores the Exec.Start() process
Cmd string `toml:"cmd" yaml:"cmd"` // Execute command
ChangeDir string `toml:"dir" yaml:"dir"` // If directory needs to be changed to call this command relative to the root path
DelayNext int `toml:"delay_next" yaml:"delay_next"` // Delay in milliseconds before running command
// Type can have one of a few types to define how it reacts to a file change
// background -- runs once at startup and is killed when refresh is canceled
// once -- runs once at refresh startup but is blocking
// blocking -- runs every refresh cycle as a blocking process
// primary -- Is the primary process that kills the previous processes before running
Type string `toml:"type" yaml:"type"`
}

type ExecuteType string

var (
Background ExecuteType = "background"
Once ExecuteType = "once"
Blocking ExecuteType = "blocking"
Primary ExecuteType = "primary"
)

var KILL_STALE = Execute{
Cmd: "KILL_STALE",
IsBlocking: true,
IsPrimary: false,
Cmd: "KILL_STALE",
Type: "blocking",
}

var REFRESH_EXEC = "REFRESH"
Expand Down
23 changes: 9 additions & 14 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ import (
)

type Process struct {
Exec string
Blocking bool
Background bool
Primary bool
logPipe io.ReadCloser
cmd *exec.Cmd
pid int
pgid int
Exec string
Type ExecuteType
logPipe io.ReadCloser
cmd *exec.Cmd
pid int
pgid int
}

type ProcessManager struct {
Processes []*Process
mu sync.RWMutex
Ctxs map[string]context.Context
Cancels map[string]context.CancelFunc
mainCtx context.Context
FirstRun bool
}

Expand All @@ -39,12 +36,10 @@ func NewProcessManager() *ProcessManager {
}
}

func (pm *ProcessManager) AddProcess(exec string, blocking bool, primary bool, background bool) {
func (pm *ProcessManager) AddProcess(exec string, typing string) {
pm.Processes = append(pm.Processes, &Process{
Exec: exec,
Blocking: blocking,
Primary: primary,
Background: background,
Exec: exec,
Type: typing,

Check failure on line 42 in process/process.go

View workflow job for this annotation

GitHub Actions / build (macos-latest)

cannot use typing (variable of type string) as ExecuteType value in struct literal

Check failure on line 42 in process/process.go

View workflow job for this annotation

GitHub Actions / tests

cannot use typing (variable of type string) as ExecuteType value in struct literal

Check failure on line 42 in process/process.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

cannot use typing (variable of type string) as ExecuteType value in struct literal
})
}

Expand Down
11 changes: 7 additions & 4 deletions process/process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
if p.Exec == "KILL_STALE" {
continue
}
if !pm.FirstRun && p.Background {
if !pm.FirstRun && p.Type != "background" {
continue
}
cmd := generateExec(p.Exec)
p.cmd = cmd
if p.Primary {
if p.Type == Primary {
// Ensure previous processes are killed if this isnt the first run
if !pm.FirstRun {
for _, pr := range pm.Processes {
if !pr.Background {
if p.Type != Background {
// check if pid is running
if pr.pid != 0 {
_, err := os.FindProcess(pr.pid)
Expand Down Expand Up @@ -62,7 +62,10 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
}
}
var err error
if p.Blocking {
if p.Type == Blocking || p.Type == Once {
if !pm.FirstRun && p.Type == Once {
continue
}
cmd.Stderr = os.Stderr
p.logPipe, err = cmd.StdoutPipe()
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
if p.Exec == "KILL_STALE" {
continue
}
if !pm.FirstRun && p.Background {
if !pm.FirstRun && p.Type == Background {
continue
}

cmd := generateExec(p.Exec)
p.cmd = cmd

if p.Primary {
if p.Type == Primary {
if !pm.FirstRun {
// slog.Debug("Not first run, killing processes")
for _, pr := range pm.Processes {
if !pr.Background {
if pr.Type != Background {
// check if pid is running
if pr.pid != 0 {
_, err := os.FindProcess(pr.pid)
Expand Down Expand Up @@ -75,7 +75,10 @@ func (pm *ProcessManager) StartProcess(ctx context.Context, cancel context.Cance
}

var err error
if p.Blocking {
if p.Type == Blocking || p.Type == Once {
if p.Type == Once && !pm.FirstRun {
continue
}
cmd.Stderr = os.Stderr
p.logPipe, err = cmd.StdoutPipe()
go printSubProcess(ctx, p.logPipe)
Expand Down

0 comments on commit c7cd902

Please sign in to comment.