Skip to content

Commit

Permalink
Kill all left behinds processes (#20)
Browse files Browse the repository at this point in the history
* Feature/save and kill processes (#18)

* Update threadfin.go

* Fix banner bug
  • Loading branch information
marcelGoerentz authored Aug 17, 2024
1 parent b676e0e commit bf11e6a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ VOLUME $THREADFIN_TEMP
EXPOSE $THREADFIN_PORT

# Run the Threadfin executable
ENTRYPOINT ${THREADFIN_BIN}/threadfin -port=${THREADFIN_PORT} -config=${THREADFIN_CONF} -debug=${THREADFIN_DEBUG}
ENTRYPOINT ${THREADFIN_BIN}/threadfin -port=${THREADFIN_PORT} -config=${THREADFIN_CONF} -debug=${THREADFIN_DEBUG} -branch=${BRANCH}
1 change: 0 additions & 1 deletion html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
<body onload="javascript: PageReady();">

<div class="banner">
New Version available! Click <a href="https://github.com/marcelGoerentz/Threadfin/releases/latest">here</a> to download.
</div>

<div id="loading" class="modal fade">
Expand Down
77 changes: 60 additions & 17 deletions src/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"log"
"net/http"
"runtime"

//"net/url"
"os"
Expand Down Expand Up @@ -1067,10 +1068,11 @@ func thirdPartyBuffer(streamID int, playlistID string, useBackup bool, backupNum

cmd.Start()
defer cmd.Wait()
writePIDtoDisc(fmt.Sprintf("%d", cmd.Process.Pid))

go func() {

// Log Daten vom Prozess im Dubug Mode 1 anzeigen.
// Log Daten vom Prozess im Debug Mode 1 anzeigen.
scanner := bufio.NewScanner(logOut)
scanner.Split(bufio.ScanLines)

Expand Down Expand Up @@ -1126,11 +1128,10 @@ func thirdPartyBuffer(streamID int, playlistID string, useBackup bool, backupNum
select {
case timeout := <-t:
if timeout >= 20 && tmpSegment == 1 {
cmd.Process.Kill()
err = errors.New("Timout")
terminateProcessGracefully(cmd)
err = errors.New("Timeout")
ShowError(err, 4006)
addErrorToStream(err)
cmd.Wait()
f.Close()
return
}
Expand All @@ -1144,9 +1145,8 @@ func thirdPartyBuffer(streamID int, playlistID string, useBackup bool, backupNum
}

if !clientConnection(stream) {
cmd.Process.Kill()
terminateProcessGracefully(cmd)
f.Close()
cmd.Wait()
return
}

Expand All @@ -1158,10 +1158,9 @@ func thirdPartyBuffer(streamID int, playlistID string, useBackup bool, backupNum
fileSize = fileSize + len(buffer[:n])

if _, err := f.Write(buffer[:n]); err != nil {
cmd.Process.Kill()
terminateProcessGracefully(cmd)
ShowError(err, 0)
addErrorToStream(err)
cmd.Wait()
return
}

Expand Down Expand Up @@ -1192,19 +1191,17 @@ func thirdPartyBuffer(streamID int, playlistID string, useBackup bool, backupNum
_, errCreate = bufferVFS.Create(tmpFile)
f, errOpen = bufferVFS.OpenFile(tmpFile, os.O_APPEND|os.O_WRONLY, 0600)
if errCreate != nil || errOpen != nil {
cmd.Process.Kill()
terminateProcessGracefully(cmd)
ShowError(err, 0)
addErrorToStream(err)
cmd.Wait()
return
}

}

}

cmd.Process.Kill()
cmd.Wait()
terminateProcessGracefully(cmd)

err = errors.New(bufferType + " error")

Expand Down Expand Up @@ -1337,15 +1334,21 @@ func debugResponse(resp *http.Response) {

func terminateProcessGracefully(cmd *exec.Cmd) {
if cmd.Process != nil {

// Send a SIGTERM to the process
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
// If an error occurred while trying to send the SIGTERM, you might resort to a SIGKILL.
ShowError(err, 0)
if runtime.GOOS == "windows" {
cmd.Process.Kill()
} else {
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
// If an error occurred while trying to send the SIGTERM, use SIGKILL.
ShowError(err, 0)
cmd.Process.Signal(syscall.SIGKILL)
}
}

// Optionally, you can wait for the process to finish too
cmd.Wait()
deletPIDfromDisc(fmt.Sprintf("%d", cmd.Process.Pid))
}
}

Expand All @@ -1364,6 +1367,46 @@ func writePIDtoDisc(pid string) {
}
}

func deletPIDfromDisc(pid string) {
log.Fatal("Nothing")
func deletPIDfromDisc(delete_pid string) (error){
file, err := os.OpenFile(System.Folder.Temp + "PIDs", os.O_RDWR, 0660)
if err != nil {
return err
}
// Create a scanner
scanner := bufio.NewScanner(file)

// Read line by line
pids := []string{}
for scanner.Scan() {
line := scanner.Text()
pids = append(pids, line)
}

// Rewind the file to the beginning
_, err = file.Seek(0, 0)
if err != nil {
return err
}

updatedPIDs := []string{}
for index, pid := range pids {
if pid != delete_pid {
// Create a new slice by excluding the element at the specified index
_, err = file.WriteString(pid + "\n")
if err != nil {
return err
}
} else {
updatedPIDs = append(pids[:index], pids[:index+1]...)
}
}

// Truncate any remaining content (if the new slice is shorter)
if len(updatedPIDs) < len(pids) {
err = file.Truncate(int64(len(updatedPIDs)))
if err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit bf11e6a

Please sign in to comment.