From 37e5085dbe0e1074b348b319912381dfcfa0c4a6 Mon Sep 17 00:00:00 2001 From: Robert Jandow Date: Wed, 7 Aug 2024 16:10:07 +0200 Subject: [PATCH] Add endpoint to get taskInfo via REST --- HadesAPI/router.go | 27 ++++++++++++- docs/api/Get Status of Build Job.bru | 57 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 docs/api/Get Status of Build Job.bru diff --git a/HadesAPI/router.go b/HadesAPI/router.go index e1b8b53..517b326 100644 --- a/HadesAPI/router.go +++ b/HadesAPI/router.go @@ -27,6 +27,7 @@ func setupRouter(auth_key string) *gin.Engine { r.GET("/ping", ping) r.POST("/build", AddBuildToQueue) + r.GET("/task/:id", GetTaskState) return r } @@ -80,6 +81,30 @@ func AddBuildToQueue(c *gin.Context) { log.Printf(" [*] Successfully enqueued job: %+v", info.ID) c.JSON(http.StatusOK, gin.H{ "message": "Successfully enqueued job", - "job_id": payload.QueuePayload.ID.String(), + "task_id": info.ID, + "job_id": payload.QueuePayload.ID, }) } + +func GetTaskState(c *gin.Context) { + id := c.Param("id") + if id == "" { + c.String(http.StatusBadRequest, "No task ID provided") + return + } + inspector := asynq.NewInspector(asynq.RedisClientOpt{Addr: cfg.RedisConfig.Addr, Password: cfg.RedisConfig.Pwd}) + queues, err := inspector.Queues() + if err != nil { + log.WithError(err).Error("Failed to get queues") + c.String(http.StatusInternalServerError, "Failed to get queues") + return + } + for _, q := range queues { + taskInfo, err := inspector.GetTaskInfo(q, id) + if err == nil { + c.JSON(http.StatusOK, taskInfo.State.String()) + return + } + } + c.String(http.StatusNotFound, "Task not found") +} diff --git a/docs/api/Get Status of Build Job.bru b/docs/api/Get Status of Build Job.bru new file mode 100644 index 0000000..c847ec7 --- /dev/null +++ b/docs/api/Get Status of Build Job.bru @@ -0,0 +1,57 @@ +meta { + name: Get Status of Build Job + type: http + seq: 2 +} + +get { + url: http://{{hostname}}/task/:id + body: json + auth: none +} + +params:path { + id: 08df9951-1a86-4ac4-ac7a-7c3ec5d322af +} + +body:json { + { + "name": "Example Job", + "metadata": {}, // global metadata + "timestamp": "2021-01-01T00:00:00.000Z", + "priority": 3, // optional, default 3 + "steps": [ + { + "id": 1, // mandatory to declare the order of execution + "name": "Clone", + "image": "ghcr.io/ls1intum/hades/hades-clone-container:latest", // mandatory + "metadata": { + "REPOSITORY_DIR": "/shared", + "HADES_TEST_USERNAME": "{{user}}", + "HADES_TEST_PASSWORD": "{{password}}", + "HADES_TEST_URL": "{{test_repo}}", + "HADES_TEST_PATH": "./example", + "HADES_TEST_ORDER": "1", + "HADES_ASSIGNMENT_USERNAME": "{{user}}", + "HADES_ASSIGNMENT_PASSWORD": "{{password}}", + "HADES_ASSIGNMENT_URL": "{{assignment_repo}}", + "HADES_ASSIGNMENT_PATH": "./example/assignment", + "HADES_ASSIGNMENT_ORDER": "2" + } + }, + { + "id": 2, // mandatory to declare the order of execution + "name": "Execute", + "image": "ls1tum/artemis-maven-template:java17-18", // mandatory + "script": "cd ./example && ./gradlew --status && ./gradlew clean test" + } + ] + } +} + +vars:pre-request { + user: {{process.env.USERNAME}} + password: {{process.env.PASSWORD}} + test_repo: {{process.env.TEST_REPO}} + assignment_repo: {{process.env.ASSIGNMENT_REPO}} +}