-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: program executor extracted out to a separate package
- Loading branch information
1 parent
61bca9c
commit 66bc757
Showing
2 changed files
with
177 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package executor | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
"os/exec" | ||
"os/signal" | ||
"strings" | ||
"sync" | ||
"syscall" | ||
) | ||
|
||
type Executor struct { | ||
ctx context.Context | ||
done chan os.Signal | ||
logger *slog.Logger | ||
|
||
newCmd func(context.Context) *exec.Cmd | ||
mu sync.Mutex | ||
} | ||
|
||
type ExecutorArgs struct { | ||
Logger *slog.Logger | ||
Command func(context.Context) *exec.Cmd | ||
} | ||
|
||
func NewExecutor(ctx context.Context, args ExecutorArgs) *Executor { | ||
done := make(chan os.Signal, 1) | ||
signal.Notify(done, syscall.SIGTERM) | ||
|
||
if args.Logger == nil { | ||
args.Logger = slog.Default() | ||
} | ||
|
||
return &Executor{ | ||
done: done, | ||
logger: args.Logger, | ||
ctx: ctx, | ||
newCmd: args.Command, | ||
mu: sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (ex *Executor) Exec() error { | ||
ex.mu.Lock() | ||
defer ex.mu.Unlock() | ||
|
||
ex.logger.Debug("[exec] starting process") | ||
|
||
ctx, cf := context.WithCancel(ex.ctx) | ||
defer cf() | ||
|
||
cmd := ex.newCmd(ctx) | ||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} | ||
|
||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
select { | ||
case <-ctx.Done(): | ||
ex.logger.Debug("process terminated", "pid", cmd.Process.Pid) | ||
case <-ex.done: | ||
ex.logger.Debug("executor terminated", "pid", cmd.Process.Pid) | ||
} | ||
ex.logger.Debug("[exec] killing process", "pid", cmd.Process.Pid) | ||
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil { | ||
ex.logger.Error("failed to kill process", "pid", cmd.Process.Pid, "err", err) | ||
} | ||
}() | ||
|
||
if err := cmd.Wait(); err != nil { | ||
if strings.HasPrefix(err.Error(), "signal:") { | ||
ex.logger.Debug("wait terminated, received", "signal", err.Error()) | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ex *Executor) Kill() { | ||
ex.done <- os.Signal(syscall.SIGTERM) | ||
} |