Skip to content

Commit

Permalink
feat: improves executor and signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtcoder17 committed Sep 23, 2024
1 parent e8c8878 commit de95ec6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func main() {
execArgs = cctx.Args().Tail()
}

ex := executor.NewExecutor(ctx, executor.ExecutorArgs{
ex := executor.NewExecutor(executor.ExecutorArgs{
Logger: logger,
Command: func(context.Context) *exec.Cmd {
cmd := exec.Command(execCmd, execArgs...)
Expand Down
33 changes: 23 additions & 10 deletions pkg/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

type Executor struct {
ctx context.Context
done chan os.Signal
logger *slog.Logger
done chan os.Signal
logger *slog.Logger
isRunning bool

newCmd func(context.Context) *exec.Cmd
mu sync.Mutex
Expand All @@ -25,7 +25,7 @@ type ExecutorArgs struct {
Command func(context.Context) *exec.Cmd
}

func NewExecutor(ctx context.Context, args ExecutorArgs) *Executor {
func NewExecutor(args ExecutorArgs) *Executor {
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGTERM)

Expand All @@ -36,19 +36,24 @@ func NewExecutor(ctx context.Context, args ExecutorArgs) *Executor {
return &Executor{
done: done,
logger: args.Logger,
ctx: ctx,
newCmd: args.Command,
mu: sync.Mutex{},
}
}

func (ex *Executor) Exec() error {
ex.logger.Debug("[exec:pre] starting process")
ex.mu.Lock()
defer ex.mu.Unlock()
ex.isRunning = true

defer func() {
ex.isRunning = false
ex.mu.Unlock()
}()

ex.logger.Debug("[exec] starting process")

ctx, cf := context.WithCancel(ex.ctx)
ctx, cf := context.WithCancel(context.TODO())
defer cf()

cmd := ex.newCmd(ctx)
Expand All @@ -65,22 +70,30 @@ func (ex *Executor) Exec() error {
case <-ex.done:
ex.logger.Debug("executor terminated", "pid", cmd.Process.Pid)
}
for len(ex.done) > 0 {
<-ex.done
}

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.Error() != "no such process" {
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
ex.logger.Debug("while waiting, got", "err", err)
}

return nil
}

func (ex *Executor) Kill() {
ex.done <- os.Signal(syscall.SIGTERM)
if ex.isRunning {
ex.done <- os.Signal(syscall.SIGTERM)
}
}
2 changes: 1 addition & 1 deletion pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (f *fsnWatcher) WatchEvents(watcherFunc func(event Event, fp string) error)
eInfo.Counter += 1
f.eventMap[event.Name] = eInfo

if time.Since(eInfo.Time) < 1*time.Second {
if time.Since(eInfo.Time) < 100*time.Millisecond {
f.Logger.Debug("too many events under 1s, ignoring...", "counter", eInfo.Counter)
continue
}
Expand Down

0 comments on commit de95ec6

Please sign in to comment.