|
| 1 | +package tasks |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/ansible-semaphore/semaphore/pkg/task_logger" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/ansible-semaphore/semaphore/api/helpers" |
| 8 | + "github.com/ansible-semaphore/semaphore/db" |
| 9 | + task2 "github.com/ansible-semaphore/semaphore/services/tasks" |
| 10 | + "github.com/gorilla/context" |
| 11 | +) |
| 12 | + |
| 13 | +func TaskMiddleware(next http.Handler) http.Handler { |
| 14 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 15 | + taskID, err := helpers.GetIntParam("task_id", w, r) |
| 16 | + if err != nil { |
| 17 | + helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest) |
| 18 | + } |
| 19 | + |
| 20 | + context.Set(r, "task_id", taskID) |
| 21 | + next.ServeHTTP(w, r) |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +type taskLocation string |
| 26 | + |
| 27 | +const ( |
| 28 | + taskQueue taskLocation = "queue" |
| 29 | + taskRunning taskLocation = "running" |
| 30 | +) |
| 31 | + |
| 32 | +type taskRes struct { |
| 33 | + TaskID int `json:"task_id"` |
| 34 | + ProjectID int `json:"project_id"` |
| 35 | + Username string `json:"username,omitempty"` |
| 36 | + RunnerID int `json:"runner_id,omitempty"` |
| 37 | + Status task_logger.TaskStatus `json:"status"` |
| 38 | + Location taskLocation `json:"location"` |
| 39 | + RunnerName string `json:"runner_name,omitempty"` |
| 40 | + ProjectName string `json:"project_name,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +func GetTasks(w http.ResponseWriter, r *http.Request) { |
| 44 | + pool := context.Get(r, "task_pool").(*task2.TaskPool) |
| 45 | + |
| 46 | + res := []taskRes{} |
| 47 | + |
| 48 | + for _, task := range pool.Queue { |
| 49 | + res = append(res, taskRes{ |
| 50 | + TaskID: task.Task.ID, |
| 51 | + ProjectID: task.Task.ProjectID, |
| 52 | + RunnerID: task.RunnerID, |
| 53 | + Username: task.Username, |
| 54 | + Status: task.Task.Status, |
| 55 | + Location: taskQueue, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + for _, task := range pool.RunningTasks { |
| 60 | + res = append(res, taskRes{ |
| 61 | + TaskID: task.Task.ID, |
| 62 | + ProjectID: task.Task.ProjectID, |
| 63 | + RunnerID: task.RunnerID, |
| 64 | + Username: task.Username, |
| 65 | + Status: task.Task.Status, |
| 66 | + Location: taskRunning, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + helpers.WriteJSON(w, http.StatusOK, res) |
| 71 | +} |
| 72 | + |
| 73 | +func DeleteTask(w http.ResponseWriter, r *http.Request) { |
| 74 | + |
| 75 | + taskID := context.Get(r, "task_id").(int) |
| 76 | + |
| 77 | + pool := context.Get(r, "task_pool").(*task2.TaskPool) |
| 78 | + |
| 79 | + var task *db.Task |
| 80 | + |
| 81 | + for _, t := range pool.Queue { |
| 82 | + if t.Task.ID == taskID { |
| 83 | + task = &t.Task |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if task == nil { |
| 89 | + for _, t := range pool.RunningTasks { |
| 90 | + if t.Task.ID == taskID { |
| 91 | + task = &t.Task |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if task != nil { |
| 98 | + err := pool.StopTask(*task, false) |
| 99 | + if err != nil { |
| 100 | + helpers.WriteErrorStatus(w, err.Error(), http.StatusInternalServerError) |
| 101 | + return |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + helpers.WriteJSON(w, http.StatusNoContent, nil) |
| 106 | +} |
0 commit comments