abortPause #1

Merged
Joren merged 7 commits from abortPause into main 2024-09-13 22:59:31 +02:00
5 changed files with 46 additions and 10 deletions
Showing only changes of commit dfe21445e5 - Show all commits

View File

@ -18,7 +18,7 @@ func removeBOM(input []byte) []byte {
return input return input
} }
func downloadFile(item Item) error { func downloadFile(item Item, jobInfo *JobInfo) error {
fmt.Println("Downloading:", item.Filename) fmt.Println("Downloading:", item.Filename)
mpdPath := item.MPD mpdPath := item.MPD
@ -91,12 +91,33 @@ func downloadFile(item Item) error {
cmd := exec.Command("bash", "-c", command) cmd := exec.Command("bash", "-c", command)
jobsMutex.Lock()
jobInfo.Cmd = cmd
jobsMutex.Unlock()
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
err := cmd.Run() err := cmd.Start()
if err != nil { if err != nil {
return fmt.Errorf("error executing download command: %v", err) return fmt.Errorf("error starting download command: %v", err)
}
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
select {
case <-jobInfo.AbortChan:
if cmd.Process != nil {
cmd.Process.Kill()
}
return fmt.Errorf("download aborted")
case err := <-done:
if err != nil {
return fmt.Errorf("error executing download command: %v", err)
}
} }
fmt.Println("Download completed successfully") fmt.Println("Download completed successfully")

1
go.mod
View File

@ -11,6 +11,7 @@ require (
require ( require (
github.com/asticode/go-astikit v0.20.0 // indirect github.com/asticode/go-astikit v0.20.0 // indirect
github.com/asticode/go-astits v1.8.0 // indirect github.com/asticode/go-astits v1.8.0 // indirect
github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
golang.org/x/text v0.3.2 // indirect golang.org/x/text v0.3.2 // indirect
) )

2
go.sum
View File

@ -10,6 +10,8 @@ github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI=
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs= github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a h1:EN123kAtAAE2pg/+TvBsUBZfHCWNNFyL2ZBPPfNWAc0=
github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a/go.mod h1:b95YoNrAnScjaWG+asr8lxqlrsPUcT2ZEBcjvVGshMo=
github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE= github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@ -124,7 +124,7 @@ func handleAbort(w http.ResponseWriter, r *http.Request) {
} }
jobsMutex.Lock() jobsMutex.Lock()
abortChan, exists := jobs[filename] jobInfo, exists := jobs[filename]
jobsMutex.Unlock() jobsMutex.Unlock()
if !exists { if !exists {
@ -132,6 +132,10 @@ func handleAbort(w http.ResponseWriter, r *http.Request) {
return return
} }
close(abortChan) close(jobInfo.AbortChan)
if jobInfo.Cmd != nil && jobInfo.Cmd.Process != nil {
jobInfo.Cmd.Process.Kill()
}
fmt.Fprintf(w, "Abort signal sent for %s", filename) fmt.Fprintf(w, "Abort signal sent for %s", filename)
} }

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"net/url" "net/url"
"os" "os"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -14,9 +15,14 @@ import (
"github.com/beevik/etree" "github.com/beevik/etree"
) )
type JobInfo struct {
AbortChan chan struct{}
Cmd *exec.Cmd
}
var ( var (
jobsMutex sync.Mutex jobsMutex sync.Mutex
jobs = make(map[string]chan struct{}) jobs = make(map[string]*JobInfo)
) )
func sanitizeFilename(filename string) string { func sanitizeFilename(filename string) string {
@ -180,8 +186,10 @@ func filterSelectedItems(items []Item, selectedItems []string) []Item {
func processItems(filename string, items []Item) error { func processItems(filename string, items []Item) error {
jobsMutex.Lock() jobsMutex.Lock()
abortChan := make(chan struct{}) jobInfo := &JobInfo{
jobs[filename] = abortChan AbortChan: make(chan struct{}),
}
jobs[filename] = jobInfo
jobsMutex.Unlock() jobsMutex.Unlock()
defer func() { defer func() {
@ -192,12 +200,12 @@ func processItems(filename string, items []Item) error {
for i, item := range items { for i, item := range items {
select { select {
case <-abortChan: case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted") updateProgress(filename, 100, "Aborted")
return fmt.Errorf("download aborted") return fmt.Errorf("download aborted")
default: default:
updateProgress(filename, float64(i)/float64(len(items))*100, item.Filename) updateProgress(filename, float64(i)/float64(len(items))*100, item.Filename)
err := downloadFile(item) err := downloadFile(item, jobInfo)
if err != nil { if err != nil {
fmt.Printf("Error downloading file: %v\n", err) fmt.Printf("Error downloading file: %v\n", err)
} }