package tasks_utils import ( "time" . "git.andr3h3nriqu3s.com/andr3/fyp/logic/db_types" "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"` ExtraTaskInfo string `db:"extra_task_info" json:"extra_task_info"` 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 + iota TASK_TYPE_TRAINING TASK_TYPE_RETRAINING ) type TaskAgreement int const ( TASK_AGREE_DISAGRE TaskAgreement = -1 + iota TASK_AGREE_NONE TASK_AGREE_AGREE ) func (t Task) UpdateStatus(base BasePack, status TaskStatus, message string) (err error) { return UpdateTaskStatus(base, t.Id, status, message) } func (t Task) SetAgreement(base BasePack, agreement TaskAgreement) (err error) { _, err = base.GetDb().Exec("update tasks set user_confirmed=$1 where id=$2", agreement, t.Id) return } /** * 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 }