feat: 流水线队列引擎 — max_executors并发控制 + Jenkins风格构建队列+取消
This commit is contained in:
1 parent
be9db65a22
commit
7bc5169e8f
5 files changed
+249
-4
No files matched your search
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"ezops/internal/models"
|
||||
@@ -234,6 +235,8 @@ func (h *PipelineHandler) CreatePipeline(c *gin.Context) {
|
||||
WebhookToken: req.WebhookToken,
|
||||
// 缓存
|
||||
CachePaths: req.CachePaths,
|
||||
// 并发控制
|
||||
MaxExecutors: req.MaxExecutors,
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
@@ -315,6 +318,7 @@ func (h *PipelineHandler) UpdatePipeline(c *gin.Context) {
|
||||
if req.WebhookToken != "" { updates["webhook_token"] = req.WebhookToken }
|
||||
if req.CachePaths != "" { updates["cache_paths"] = req.CachePaths }
|
||||
if req.Enabled != nil { updates["enabled"] = req.Enabled }
|
||||
if req.MaxExecutors > 0 { updates["max_executors"] = req.MaxExecutors }
|
||||
|
||||
if err := h.db.Model(&pipeline).Updates(updates).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{
|
||||
@@ -418,8 +422,8 @@ func (h *PipelineHandler) TriggerPipeline(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 后台异步执行流水线
|
||||
go h.executePipeline(pipeline, run)
|
||||
// 通过队列引擎执行(尊重 max_executors 并发限制)
|
||||
go h.tryExecuteOrQueue(pipeline, run)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已触发", Data: run})
|
||||
}
|
||||
@@ -548,3 +552,29 @@ func (h *PipelineHandler) WebhookTrigger(c *gin.Context) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// GetPipelineQueue 查询流水线队列状态
|
||||
func (h *PipelineHandler) GetPipelineQueue(c *gin.Context) {
|
||||
id := getPipelineID(c)
|
||||
if id == 0 {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "无效的流水线编码"})
|
||||
return
|
||||
}
|
||||
status := h.GetQueueStatus(uint(id))
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Data: status})
|
||||
}
|
||||
|
||||
// CancelQueueItem 取消排队中的构建
|
||||
func (h *PipelineHandler) CancelQueueItem(c *gin.Context) {
|
||||
id := getPipelineID(c)
|
||||
runID, _ := strconv.ParseUint(c.Param("runId"), 10, 64)
|
||||
if id == 0 || runID == 0 {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "参数错误"})
|
||||
return
|
||||
}
|
||||
if err := h.CancelQueued(uint(id), uint(runID)); err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "已取消"})
|
||||
}
|
||||
Reference in new issue
Block a user