This commit is contained in:
speatzle 2024-05-07 18:52:06 +02:00
parent 172bf636d6
commit a040d5d56c
6 changed files with 123 additions and 3 deletions

View file

@ -7,8 +7,10 @@ type Config struct {
}
type Server struct {
Address string
Database string
Address string
Database string
HealthCheckCommand string
TranscodeCommand string
}
type Worker struct {

View file

@ -14,5 +14,13 @@ const MESSAGE_TEMPLATE_NAME = "message.tmpl"
const TASK_TEMPLATE_NAME = "tasks.tmpl"
const (
HEALTHCHECK_TASK_TYPE = iota
TASK_TYPE_HEALTHCHECK = iota
TASK_TYPE_TRANSCODE
)
const (
TASK_STATUS_UNKNOWN = iota
TASK_STATUS_RUNNING
TASK_STATUS_FAILED
TASK_STATUS_SUCCESS
)

17
task/healthcheck.go Normal file
View file

@ -0,0 +1,17 @@
package task
import "git.lastassault.de/speatzle/morffix/constants"
type HealthCheckData struct {
Command string `json:"command"`
}
func (t *Task) RunHealthCheck(data HealthCheckData) {
defer func() {
if t.Status == constants.TASK_STATUS_RUNNING {
t.Status = constants.TASK_STATUS_FAILED
t.Log = append(t.Log, "Task Status Failed by Defer")
}
}()
}

9
task/task.go Normal file
View file

@ -0,0 +1,9 @@
package task
type Task struct {
ID int `json:"id"`
FileID int `json:"file_id"`
Type int `json:"type"`
Status int `json:"status"`
Log []string `json:"log"`
}

18
types/task.go Normal file
View file

@ -0,0 +1,18 @@
package types
import (
"encoding/json"
"git.lastassault.de/speatzle/morffix/task"
)
type TaskStart struct {
ID int `json:"id"`
FileID int `json:"file_id"`
Type int `json:"type"`
Data json.RawMessage
}
type TaskStatus struct {
task.Task
}

66
worker/task.go Normal file
View file

@ -0,0 +1,66 @@
package worker
import (
"context"
"encoding/json"
"fmt"
"sync"
"git.lastassault.de/speatzle/morffix/constants"
"git.lastassault.de/speatzle/morffix/rpc"
"git.lastassault.de/speatzle/morffix/task"
"git.lastassault.de/speatzle/morffix/types"
)
var tasks map[int]*task.Task
var taskMutex sync.Mutex
func init() {
rpcServer.RegisterMethod("task-start", taskStart)
rpcServer.RegisterMethod("task-status", taskStatus)
}
func taskStart(ctx context.Context, req rpc.Request) (any, error) {
var data types.TaskStart
err := json.Unmarshal(*req.Params, &data)
if err != nil {
return nil, fmt.Errorf("Unmarshal Task Start Params: %w", err)
}
// TODO move to task module
taskMutex.Lock()
defer taskMutex.Unlock()
switch data.Type {
case constants.TASK_TYPE_HEALTHCHECK:
tasks[data.ID] = &task.Task{
ID: data.ID,
Type: data.Type,
FileID: data.FileID,
}
var hData task.HealthCheckData
err := json.Unmarshal(data.Data, &hData)
if err != nil {
return nil, fmt.Errorf("Unmarshal Healthcheck Data: %w", err)
}
tasks[data.ID].Status = constants.TASK_STATUS_RUNNING
go tasks[data.ID].RunHealthCheck(hData)
case constants.TASK_TYPE_TRANSCODE:
return nil, fmt.Errorf("Transcode Task Not Implemented")
default:
return nil, fmt.Errorf("Unknown Task Type %v", data.Type)
}
return nil, nil
}
func taskStatus(ctx context.Context, req rpc.Request) (any, error) {
var id int
err := json.Unmarshal(*req.Params, &id)
if err != nil {
return nil, fmt.Errorf("Unmarshal Task Status Params: %w", err)
}
return nil, nil
}