diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-07-14 22:15:55 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-07-14 22:15:55 (GMT) |
commit | 79dfe23a8cfabe4688a3b05998178a11ea51150c (patch) | |
tree | 9bab4e5317ea4efcd19f32113f3ec861b7b0603a /rhimport | |
parent | 09d298877fdf473ebe5a997b20a42b4e69355c67 (diff) |
improved canceling
Diffstat (limited to 'rhimport')
-rw-r--r-- | rhimport/converter.go | 53 | ||||
-rw-r--r-- | rhimport/fetcher.go | 40 |
2 files changed, 70 insertions, 23 deletions
diff --git a/rhimport/converter.go b/rhimport/converter.go index 0546b97..7c35c07 100644 --- a/rhimport/converter.go +++ b/rhimport/converter.go @@ -29,6 +29,7 @@ import ( "errors" "fmt" "io" + "net/http" "os" "os/exec" "path/filepath" @@ -133,9 +134,16 @@ func (ff *FFMpegFetchConverter) Close() (err error) { func (ff *FFMpegFetchConverter) GetResult(ctx *Context, res *Result) (result string, err error) { if ff.result != nil { - r := <-ff.result - ctx.LoudnessCorr = r.loudnessCorr - return r.output, r.err + select { + case r := <-ff.result: + ctx.LoudnessCorr = r.loudnessCorr + return r.output, r.err + case <-ctx.Cancel: + ff.cmd.Process.Kill() + res.ResponseCode = http.StatusNoContent + res.ErrorString = "canceled" + return "", errors.New("canceled") + } } return "", nil } @@ -201,9 +209,16 @@ func (bs *BS1770FetchConverter) Close() (err error) { func (bs *BS1770FetchConverter) GetResult(ctx *Context, res *Result) (result string, err error) { if bs.result != nil { - r := <-bs.result - ctx.LoudnessCorr = r.loudnessCorr - return r.output, r.err + select { + case r := <-bs.result: + ctx.LoudnessCorr = r.loudnessCorr + return r.output, r.err + case <-ctx.Cancel: + bs.cmd.Process.Kill() + res.ResponseCode = http.StatusNoContent + res.ErrorString = "canceled" + return "", errors.New("canceled") + } } return "", nil } @@ -284,13 +299,29 @@ func (ff *FFMpegBS1770FetchConverter) Close() (err error) { } func (ff *FFMpegBS1770FetchConverter) GetResult(ctx *Context, res *Result) (result string, err error) { - var rff, rbs FetchConverterResult - if ff.resultFF != nil { - rff = <-ff.resultFF + if ff.resultFF == nil || ff.resultBS == nil { + return "", nil } - if ff.resultBS != nil { - rbs = <-ff.resultBS + + var rff, rbs *FetchConverterResult + for { + select { + case r := <-ff.resultFF: + rff = &r + case r := <-ff.resultBS: + rbs = &r + case <-ctx.Cancel: + ff.ffmpeg.Process.Kill() + ff.bs1770.Process.Kill() + res.ResponseCode = http.StatusNoContent + res.ErrorString = "canceled" + return "", errors.New("canceled") + } + if rff != nil && rbs != nil { + break + } } + if rff.err != nil { return rff.output, fmt.Errorf("ffmpeg: %v", rff.err) } diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 4d942fa..1a957c3 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -364,6 +364,7 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { rhdl.Printf("importing archiv file from %s", scpuri) err = easy.Perform() + var convOut string var convErr error if cbdata.conv != nil { @@ -412,7 +413,6 @@ func fetchFileTmp(ctx *Context, res *Result, uri *url.URL) (err error) { } func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal int64) (err error) { - basepath, err := ioutil.TempDir(ctx.conf.TempDir, "rhimportd-") if err != nil { return err @@ -452,14 +452,28 @@ func fetchFileDirConvert(ctx *Context, res *Result, origSrc *os.File, sizeTotal ctx.ProgressCallBack = nil } } + + if ctx.Cancel != nil && len(ctx.Cancel) > 0 { + res.ResponseCode = http.StatusNoContent + res.ErrorString = "canceled" + break + } } conv.Close() + if res.ResponseCode == http.StatusNoContent { + rhl.Printf("converting of '%s' got canceled", ctx.SourceUri) + return nil + } + rhl.Printf("waiting for converter to finish...") - if convOut, convErr := conv.GetResult(ctx, res); convErr != nil { - if convOut != "" { - rhl.Printf("converter error: %v; converter output: %s", convErr, convOut) + convOut, convErr := conv.GetResult(ctx, res) + if convErr != nil { + if res.ResponseCode == http.StatusNoContent { + rhl.Printf("converting of '%s' got canceled", ctx.SourceUri) + return nil } + rhl.Printf("converter error: %v; converter output: %s", convErr, convOut) return fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut) } rhdl.Printf("converter: loudness correction = %.2f dB", ctx.LoudnessCorr) @@ -583,15 +597,10 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { } func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv FetchConverter) error { - cancel := ctx.Cancel - if cancel == nil { - cancel = make(<-chan bool) - } - written := uint64(0) for { select { - case <-cancel: + case <-ctx.Cancel: rhl.Printf("receiving attachment '%s' got canceled", ctx.SourceFile) res.ResponseCode = http.StatusNoContent res.ErrorString = "canceled" @@ -671,15 +680,22 @@ func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { err = writeAttachmentFile(ctx, res, sizeTotal, conv) conv.Close() + if res.ResponseCode == http.StatusNoContent { + rhl.Printf("download of '%s' got canceled", ctx.SourceUri) + return nil + } + rhl.Printf("waiting for converter to finish...") convOut, convErr := conv.GetResult(ctx, res) if err != nil { return err } if convErr != nil { - if convOut != "" { - rhl.Printf("converter error: %v; converter output: %s", convErr, convOut) + if res.ResponseCode == http.StatusNoContent { + rhl.Printf("download of '%s' got canceled", ctx.SourceUri) + return nil } + rhl.Printf("converter error: %v; converter output: %s", convErr, convOut) return fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut) } rhdl.Printf("converter: loudness correction = %.2f dB", ctx.LoudnessCorr) |