Transcode Task Handling
This commit is contained in:
parent
fea955fb79
commit
13ea1cb755
6 changed files with 164 additions and 20 deletions
23
task/task.go
23
task/task.go
|
@ -49,7 +49,28 @@ func StartTask(conf config.Config, data types.TaskStart) error {
|
|||
}()
|
||||
return nil
|
||||
case constants.TASK_TYPE_TRANSCODE:
|
||||
return fmt.Errorf("Transcode Task Not Implemented")
|
||||
var tData types.TranscodeData
|
||||
err := json.Unmarshal(data.Data, &tData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unmarshal Transcode Data: %w", err)
|
||||
}
|
||||
|
||||
tasks[data.ID].Status = constants.TASK_STATUS_RUNNING
|
||||
go func() {
|
||||
defer func() {
|
||||
taskMutex.Lock()
|
||||
defer taskMutex.Unlock()
|
||||
t, ok := tasks[data.ID]
|
||||
if ok {
|
||||
if t.Status == constants.TASK_STATUS_RUNNING {
|
||||
t.Status = constants.TASK_STATUS_FAILED
|
||||
t.Log = append(t.Log, "Task Status Set to Failed by defer")
|
||||
}
|
||||
}
|
||||
}()
|
||||
RunTranscode(conf, tasks[data.ID], tData)
|
||||
}()
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("Unknown Task Type %v", data.Type)
|
||||
}
|
||||
|
|
79
task/transcode.go
Normal file
79
task/transcode.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/config"
|
||||
"git.lastassault.de/speatzle/morffix/constants"
|
||||
"git.lastassault.de/speatzle/morffix/task/log"
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
)
|
||||
|
||||
func RunTranscode(conf config.Config, t *types.Task, data types.TranscodeData) {
|
||||
ctx := context.TODO()
|
||||
l := log.GetTaskLogger(t)
|
||||
|
||||
// TODO Figure out how to get correct file ending
|
||||
src_path := filepath.Join(conf.Worker.TempDir, fmt.Sprintf("src-%v-%v.mkv", t.ID, t.FileID))
|
||||
dst_path := filepath.Join(conf.Worker.TempDir, fmt.Sprintf("dst-%v-%v.mkv", t.ID, t.FileID))
|
||||
|
||||
// Set ffmpeg input path
|
||||
if len(data.Command.InputFiles) == 0 {
|
||||
l.ErrorContext(ctx, "FFmpeg Command has no input files")
|
||||
return
|
||||
}
|
||||
|
||||
data.Command.InputFiles[0].Path = src_path
|
||||
|
||||
// Set ffmpeg output path
|
||||
if len(data.Command.OutputFiles) == 0 {
|
||||
l.ErrorContext(ctx, "FFmpeg Command has no output files")
|
||||
return
|
||||
}
|
||||
|
||||
data.Command.OutputFiles[0].Path = dst_path
|
||||
|
||||
// TODO cleanup file when done
|
||||
defer func() {
|
||||
err := os.Remove(src_path)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "Removing Source File", "err", err, "path", src_path)
|
||||
} else {
|
||||
l.InfoContext(ctx, "Source File Removed Succesfully", "path", src_path)
|
||||
}
|
||||
}()
|
||||
|
||||
err := downloadFile(ctx, l, conf.Worker.Address, src_path, t)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "Source File Download Failed", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO cleanup file when done
|
||||
defer func() {
|
||||
err := os.Remove(dst_path)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "Removing Destination File", "err", err, "path", dst_path)
|
||||
} else {
|
||||
l.InfoContext(ctx, "File Destination Removed Succesfully", "path", dst_path)
|
||||
}
|
||||
}()
|
||||
|
||||
err = runFfmpegCommand(ctx, l, conf, data.Command)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "FFmpeg Failed", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = uploadFile(ctx, l, conf.Worker.Address, dst_path, t)
|
||||
if err != nil {
|
||||
l.ErrorContext(ctx, "File Upload Failed", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
l.InfoContext(ctx, "Task Success")
|
||||
t.Status = constants.TASK_STATUS_SUCCESS
|
||||
}
|
13
task/upload.go
Normal file
13
task/upload.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"git.lastassault.de/speatzle/morffix/types"
|
||||
)
|
||||
|
||||
func uploadFile(ctx context.Context, l *slog.Logger, address string, path string, t *types.Task) error {
|
||||
return fmt.Errorf("File Upload not Implemented")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue