wrap some encode errors

This commit is contained in:
sentriz
2020-02-20 18:11:30 +00:00
committed by Serge Tkatchouk
parent c79524e44e
commit 0733117aed
2 changed files with 16 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ package encode
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"io" "io"
@@ -9,6 +10,7 @@ import (
"os/exec" "os/exec"
"github.com/cespare/xxhash" "github.com/cespare/xxhash"
"github.com/pkg/errors"
) )
type Profile struct { type Profile struct {
@@ -37,7 +39,7 @@ func copyCmdOutput(out, cache io.Writer, pipeReader io.Reader) {
// Start copying! // Start copying!
if _, err := io.Copy(w, pipeReader); err != nil { if _, err := io.Copy(w, pipeReader); err != nil {
fmt.Printf("Error while writing encoded output: %s\n", err) log.Printf("error while writing encoded output: %s\n", err)
} }
} }
@@ -55,12 +57,12 @@ func writeCmdOutput(out, cache io.Writer, pipeReader io.ReadCloser) {
data := buffer[0:n] data := buffer[0:n]
_, err = out.Write(data) _, err = out.Write(data)
if err != nil { if err != nil {
fmt.Printf("Error while writing HTTP response: %s\n", err) log.Printf("error while writing HTTP response: %s\n", err)
} }
_, err = cache.Write(data) _, err = cache.Write(data)
if err != nil { if err != nil {
fmt.Printf("Error while writing cache file: %s\n", err) log.Printf("error while writing cache file: %s\n", err)
} }
if f, ok := out.(http.Flusher); ok { if f, ok := out.(http.Flusher); ok {
@@ -106,7 +108,7 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat
// Create cache file: // Create cache file:
cacheFile, err := os.Create(cachePath) cacheFile, err := os.Create(cachePath)
if err != nil { if err != nil {
fmt.Printf("Failed to write to cache file `%s`: %s\n", cachePath, err) return errors.Wrapf(err, "writing to cache file %q: %v", cachePath)
} }
//// I'm still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput). //// I'm still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput).
@@ -116,21 +118,17 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat
go writeCmdOutput(out, cacheFile, pipeReader) go writeCmdOutput(out, cacheFile, pipeReader)
// Run FFmpeg: // Run FFmpeg:
err = cmd.Run() if err := cmd.Run(); err != nil {
if err != nil { return errors.Wrapf(err, "running ffmpeg")
fmt.Printf("Failed to encode `%s`: %s\n", trackPath, err)
} }
// Close all pipes and flush cache file: // Close all pipes and flush cache file:
pipeWriter.Close() pipeWriter.Close()
err = cacheFile.Sync() if err := cacheFile.Sync(); err != nil {
if err != nil { return errors.Wrapf(err, "flushing %q", cachePath)
fmt.Printf("Failed to flush `%s`: %s\n", cachePath, err)
} }
cacheFile.Close() cacheFile.Close()
return nil
fmt.Printf("`%s`: Encoded track to [%s/%s] successfully\n",
trackPath, profile.Format, profile.Bitrate)
} }
// Generate cache key (file name). For, you know, encoded tracks cache. // Generate cache key (file name). For, you know, encoded tracks cache.

View File

@@ -116,11 +116,14 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R
if fileExists(cacheFile) { if fileExists(cacheFile) {
log.Printf("cache [%s/%s] hit!\n", profile.Format, bitrate) log.Printf("cache [%s/%s] hit!\n", profile.Format, bitrate)
http.ServeFile(w, r, cacheFile) http.ServeFile(w, r, cacheFile)
return return nil
} }
log.Printf("cache [%s/%s] miss!\n", profile.Format, bitrate)
if err := encode.Encode(w, absPath, cacheFile, profile, bitrate); err != nil { if err := encode.Encode(w, absPath, cacheFile, profile, bitrate); err != nil {
log.Printf("cache [%s/%s] miss!\n", profile.Format, bitrate) log.Printf("error encoding %q: %v\n", absPath, err)
} }
log.Printf("track `%s` encoded to [%s/%s] successfully\n",
track.Filename, profile.Format, profile.Bitrate)
return nil return nil
} }