2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 11:27:17 +08:00
Files
panel/internal/service/task.go
2024-09-29 03:08:26 +08:00

85 lines
1.6 KiB
Go

package service
import (
"net/http"
"github.com/go-rat/chix"
"github.com/TheTNB/panel/internal/biz"
"github.com/TheTNB/panel/internal/data"
"github.com/TheTNB/panel/internal/http/request"
"github.com/TheTNB/panel/pkg/shell"
)
type TaskService struct {
taskRepo biz.TaskRepo
}
func NewTaskService() *TaskService {
return &TaskService{
taskRepo: data.NewTaskRepo(),
}
}
func (s *TaskService) Status(w http.ResponseWriter, r *http.Request) {
Success(w, chix.M{
"task": s.taskRepo.HasRunningTask(),
})
}
func (s *TaskService) List(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.Paginate](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}
tasks, total, err := s.taskRepo.List(req.Page, req.Limit)
if err != nil {
Error(w, http.StatusInternalServerError, err.Error())
return
}
Success(w, chix.M{
"total": total,
"items": tasks,
})
}
func (s *TaskService) Get(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ID](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}
task, err := s.taskRepo.Get(req.ID)
if err != nil {
Error(w, http.StatusInternalServerError, err.Error())
return
}
log, err := shell.Execf(`tail -n 500 '%s'`, task.Log)
if err == nil {
task.Log = log
}
Success(w, task)
}
func (s *TaskService) Delete(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ID](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, err.Error())
return
}
err = s.taskRepo.Delete(req.ID)
if err != nil {
Error(w, http.StatusInternalServerError, err.Error())
return
}
Success(w, nil)
}