fyp/logic/tasks/utils/utils.go

69 lines
2.0 KiB
Go
Raw Normal View History

2024-04-12 20:36:23 +01:00
package tasks_utils
import (
"time"
2024-04-14 14:51:16 +01:00
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types"
2024-04-12 20:36:23 +01:00
"github.com/goccy/go-json"
)
type Task struct {
Id string `db:"id" json:"id"`
UserId string `db:"user_id" json:"user_id"`
ModelId string `db:"model_id" json:"model_id"`
Status int `db:"status" json:"status"`
StatusMessage string `db:"status_message" json:"status_message"`
UserConfirmed int `db:"user_confirmed" json:"user_confirmed"`
Compacted int `db:"compacted" json:"compacted"`
TaskType int `db:"task_type" json:"type"`
Result string `db:"result" json:"result"`
CreatedOn time.Time `db:"created_on" json:"created"`
}
type TaskStatus int
const (
TASK_FAILED_RUNNING TaskStatus = -2
TASK_FAILED_CREATION = -1
TASK_PREPARING = 0
TASK_TODO = 1
TASK_PICKED_UP = 2
TASK_RUNNING = 3
TASK_DONE = 4
)
type TaskType int
const (
TASK_TYPE_CLASSIFICATION TaskType = 1
)
func (t Task) UpdateStatus(base BasePack, status TaskStatus, message string) (err error) {
return UpdateTaskStatus(base, t.Id, status, message)
}
/**
* Call the UpdateStatus function and logs on the case of failure!
* This varient does not return any error message
*/
func (t Task) UpdateStatusLog(base BasePack, status TaskStatus, message string) {
err := t.UpdateStatus(base, status, message)
if err != nil {
base.GetLogger().Error("Failed to update task status", "error", err, "task", t.Id)
}
}
func UpdateTaskStatus(base BasePack, id string, status TaskStatus, message string) (err error) {
_, err = base.GetDb().Exec("update tasks set status=$1, status_message=$2 where id=$3", status, message, id)
return
}
func (t Task) SetResult(base BasePack, result any) (err error) {
text, err := json.Marshal(result)
if err != nil {
return
}
_, err = base.GetDb().Exec("update tasks set result=$1 where id=$2", text, t.Id)
return
}