Skip to content

Commit

Permalink
Allow package users to set the child process priority (coder#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoog authored Oct 24, 2020
1 parent f67973e commit 44f52eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions localexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

// LocalExecer executes command on the local system.
type LocalExecer struct {
// ChildProcessPriority overrides the default niceness of all child processes launch by LocalExecer.
ChildProcessPriority *int
}

func (l *localProcess) Stdin() io.WriteCloser {
Expand Down
9 changes: 9 additions & 0 deletions localexec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ func (l LocalExecer) Start(ctx context.Context, c Command) (Process, error) {
if err != nil {
return nil, xerrors.Errorf("start command: %w", err)
}

if l.ChildProcessPriority != nil {
pid := process.cmd.Process.Pid
niceness := *l.ChildProcessPriority
err := syscall.Setpriority(syscall.PRIO_PROCESS, pid, niceness)
if err != nil {
return nil, xerrors.Errorf("set process (pid: %d) priority to (niceness: %d): %w", pid, niceness, err)
}
}
}

return &process, nil
Expand Down

0 comments on commit 44f52eb

Please sign in to comment.