Skip to content

Commit

Permalink
build and run (VOD only) on Windows systems.
Browse files Browse the repository at this point in the history
Really the only platform-dependent bits of code were the process group
management. I've replaced those with Windows-specific versions, although
I haven't fully tested them, because I'm not entirely sure how they're
used.
Also, the build configuration for Linux and other non-Windows platforms
is completely untested.
  • Loading branch information
hheimbuerger committed Nov 17, 2022
1 parent 954fd53 commit 608aff1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
21 changes: 1 addition & 20 deletions hls/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (m *ManagerCtx) Start() error {
m.cmd.Stdout = write

// create a new process group
m.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
m.cmd.SysProcAttr = ConfigureAsProcessGroup()

m.active = false
m.lastRequest = time.Now()
Expand Down Expand Up @@ -195,25 +195,6 @@ func (m *ManagerCtx) Start() error {
return err
}

func (m *ManagerCtx) Stop() {
m.mu.Lock()
defer m.mu.Unlock()

if m.cmd != nil && m.cmd.Process != nil {
m.logger.Debug().Msg("performing stop")

pgid, err := syscall.Getpgid(m.cmd.Process.Pid)
if err == nil {
err := syscall.Kill(-pgid, syscall.SIGKILL)
m.logger.Err(err).Msg("killing process group")
} else {
m.logger.Err(err).Msg("could not get process group id")
err := m.cmd.Process.Kill()
m.logger.Err(err).Msg("killing process")
}
}
}

func (m *ManagerCtx) Cleanup() {
m.mu.Lock()
diff := time.Since(m.lastRequest)
Expand Down
29 changes: 29 additions & 0 deletions hls/processgroup_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !windows
// +build !windows

package hls

import "syscall"

func ConfigureAsProcessGroup() *syscall.SysProcAttr {
return &syscall.SysProcAttr{Setpgid: true}
}

func (m *ManagerCtx) Stop() {
m.mu.Lock()
defer m.mu.Unlock()

if m.cmd != nil && m.cmd.Process != nil {
m.logger.Debug().Msg("performing stop")

pgid, err := syscall.Getpgid(m.cmd.Process.Pid)
if err == nil {
err := syscall.Kill(-pgid, syscall.SIGKILL)
m.logger.Err(err).Msg("killing process group")
} else {
m.logger.Err(err).Msg("could not get process group id")
err := m.cmd.Process.Kill()
m.logger.Err(err).Msg("killing process")
}
}
}
41 changes: 41 additions & 0 deletions hls/processgroup_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build windows
// +build windows

package hls

import (
"os"
"os/exec"
"strconv"
"syscall"
)

func ConfigureAsProcessGroup() *syscall.SysProcAttr {
return &syscall.SysProcAttr{CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP}
}

func TaskkillWithChildrenWindows(cmd *exec.Cmd) error {
// Function adopted from: https://stackoverflow.com/a/44551450/6278
// Taskkill command documentation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill

kill := exec.Command("TASKKILL", "/T", "/PID", strconv.Itoa(cmd.Process.Pid))
kill.Stderr = os.Stderr
kill.Stdout = os.Stdout
return kill.Run()
}

func (m *ManagerCtx) Stop() {
m.mu.Lock()
defer m.mu.Unlock()

if m.cmd != nil && m.cmd.Process != nil {
m.logger.Debug().Msg("performing stop")

err := TaskkillWithChildrenWindows(m.cmd)
if err == nil {
m.logger.Debug().Msg("killing process group")
} else {
m.logger.Err(err).Msg("failed to kill process group")
}
}
}
2 changes: 1 addition & 1 deletion internal/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (a *ApiManagerCtx) Http(r chi.Router) {
// dummy input for testing purposes
file := a.config.AbsPath("profiles", "http-test.sh")
cmd := exec.Command(file)
logger.Info().Msg("command startred")
logger.Info().Msg("command started")

read, write := io.Pipe()
cmd.Stdout = write
Expand Down
2 changes: 1 addition & 1 deletion internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ func (a *ApiManagerCtx) transcodeStart(profilePath string, input string) (*exec.
return nil, fmt.Errorf("stream not found")
}

log.Info().Str("profilePath", profilePath).Str("url", url).Msg("command startred")
log.Info().Str("profilePath", profilePath).Str("url", url).Msg("command started")
return exec.Command(profilePath, url), nil
}

0 comments on commit 608aff1

Please sign in to comment.