feat(monitoring): dispatch monitoring tasks to desktop via AMQP outbox
Replace SSE /desktop/events with priority AMQP dispatch for monitoring
runs, and add phase1/phase2 infrastructure so desktop workers can lease,
resume, report, skip, and cancel monitoring tasks over the existing
dispatch WebSocket.
- Add monitoring collect outbox worker and phase2 desktop task fields
- Add /desktop/monitoring/tasks/{lease,resume,result,skip,cancel} routes
- Introduce execution-devtools and network-observer on desktop runtime
- Refactor runtime-controller, LoginView, and doubao adapter for the new flow
- Add channelName column to tracking views
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
||||
"github.com/geo-platform/tenant-api/internal/shared/response"
|
||||
"github.com/geo-platform/tenant-api/internal/tenant/app"
|
||||
)
|
||||
|
||||
type DesktopMonitoringHandler struct {
|
||||
svc *app.MonitoringCallbackService
|
||||
}
|
||||
|
||||
func NewDesktopMonitoringHandler(a *bootstrap.App) *DesktopMonitoringHandler {
|
||||
return &DesktopMonitoringHandler{
|
||||
svc: app.NewMonitoringCallbackService(a.DB, a.MonitoringDB, a.RabbitMQ, a.LLM, a.Logger),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *DesktopMonitoringHandler) Lease(c *gin.Context) {
|
||||
var req app.MonitoringLeaseTasksRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_lease_request", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
data, err := h.svc.LeaseTasksForDesktopClient(c.Request.Context(), MustDesktopClient(c.Request.Context()), req)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *DesktopMonitoringHandler) Resume(c *gin.Context) {
|
||||
var req app.MonitoringResumeTasksRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_resume_request", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
data, err := h.svc.ResumeTasksForDesktopClient(c.Request.Context(), MustDesktopClient(c.Request.Context()), req)
|
||||
if err != nil {
|
||||
response.Error(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *DesktopMonitoringHandler) Result(c *gin.Context) {
|
||||
taskID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_task_id", "monitoring task id must be an integer"))
|
||||
return
|
||||
}
|
||||
|
||||
var req app.MonitoringTaskResultRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_result_request", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
data, svcErr := h.svc.HandleTaskResultForDesktopClient(c.Request.Context(), MustDesktopClient(c.Request.Context()), taskID, req)
|
||||
if svcErr != nil {
|
||||
response.Error(c, svcErr)
|
||||
return
|
||||
}
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *DesktopMonitoringHandler) Skip(c *gin.Context) {
|
||||
taskID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_task_id", "monitoring task id must be an integer"))
|
||||
return
|
||||
}
|
||||
|
||||
var req app.MonitoringSkipTaskRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_skip_request", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
data, svcErr := h.svc.SkipTaskForDesktopClient(c.Request.Context(), MustDesktopClient(c.Request.Context()), taskID, req)
|
||||
if svcErr != nil {
|
||||
response.Error(c, svcErr)
|
||||
return
|
||||
}
|
||||
response.Success(c, data)
|
||||
}
|
||||
|
||||
func (h *DesktopMonitoringHandler) Cancel(c *gin.Context) {
|
||||
taskID, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_task_id", "monitoring task id must be an integer"))
|
||||
return
|
||||
}
|
||||
|
||||
var req app.MonitoringCancelTaskRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.Error(c, response.ErrBadRequest(40041, "invalid_monitoring_cancel_request", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
data, svcErr := h.svc.CancelTaskForDesktopClient(c.Request.Context(), MustDesktopClient(c.Request.Context()), taskID, req)
|
||||
if svcErr != nil {
|
||||
response.Error(c, svcErr)
|
||||
return
|
||||
}
|
||||
response.Success(c, data)
|
||||
}
|
||||
@@ -24,6 +24,7 @@ func NewDesktopTaskHandler(a *bootstrap.App) *DesktopTaskHandler {
|
||||
return &DesktopTaskHandler{
|
||||
svc: app.NewDesktopTaskService(
|
||||
a.DB,
|
||||
a.MonitoringDB,
|
||||
a.RabbitMQ,
|
||||
a.Logger,
|
||||
).WithCache(a.Cache),
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
||||
"github.com/geo-platform/tenant-api/internal/shared/response"
|
||||
@@ -16,12 +17,16 @@ type MonitoringHandler struct {
|
||||
}
|
||||
|
||||
type monitoringCollectNowRequest struct {
|
||||
KeywordID *int64 `json:"keyword_id"`
|
||||
KeywordID *int64 `json:"keyword_id"`
|
||||
PlatformIDs []string `json:"platform_ids"`
|
||||
Preempt *bool `json:"preempt"`
|
||||
WaitForFirstDispatch *bool `json:"wait_for_first_dispatch"`
|
||||
TargetClientID *string `json:"target_client_id"`
|
||||
}
|
||||
|
||||
func NewMonitoringHandler(a *bootstrap.App) *MonitoringHandler {
|
||||
return &MonitoringHandler{
|
||||
svc: app.NewMonitoringService(a.DB, a.MonitoringDB),
|
||||
svc: app.NewMonitoringService(a.DB, a.MonitoringDB, a.RabbitMQ, a.Config.MonitoringDispatch, a.Logger),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +103,22 @@ func (h *MonitoringHandler) CollectNow(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
data, svcErr := h.svc.CollectNow(c.Request.Context(), brandID, req.KeywordID)
|
||||
var targetClientID *uuid.UUID
|
||||
if req.TargetClientID != nil && strings.TrimSpace(*req.TargetClientID) != "" {
|
||||
parsed, parseErr := uuid.Parse(strings.TrimSpace(*req.TargetClientID))
|
||||
if parseErr != nil {
|
||||
response.Error(c, response.ErrBadRequest(40031, "invalid_target_client_id", "target_client_id must be a uuid"))
|
||||
return
|
||||
}
|
||||
targetClientID = &parsed
|
||||
}
|
||||
|
||||
data, svcErr := h.svc.CollectNow(c.Request.Context(), brandID, req.KeywordID, app.MonitoringCollectNowOptions{
|
||||
PlatformIDs: req.PlatformIDs,
|
||||
Preempt: req.Preempt == nil || *req.Preempt,
|
||||
WaitForFirstDispatch: req.WaitForFirstDispatch != nil && *req.WaitForFirstDispatch,
|
||||
TargetClientID: targetClientID,
|
||||
})
|
||||
if svcErr != nil {
|
||||
response.Error(c, svcErr)
|
||||
return
|
||||
|
||||
@@ -28,8 +28,8 @@ func RegisterRoutes(app *bootstrap.App) {
|
||||
desktopClientHandler := NewDesktopClientHandler(app)
|
||||
desktopAccountHandler := NewDesktopAccountHandler(app)
|
||||
desktopTaskHandler := NewDesktopTaskHandler(app)
|
||||
desktopEventsHandler := NewDesktopEventsHandler(app)
|
||||
desktopDispatchHandler := NewDesktopDispatchHandler(app)
|
||||
desktopMonitoringHandler := NewDesktopMonitoringHandler(app)
|
||||
desktopContentHandler := NewDesktopContentHandler(app)
|
||||
publishJobHandler := NewPublishJobHandler(app)
|
||||
protected.POST("/desktop/clients/register", desktopClientHandler.Register)
|
||||
@@ -40,7 +40,6 @@ func RegisterRoutes(app *bootstrap.App) {
|
||||
desktopAuth.POST("/clients/heartbeat", desktopClientHandler.Heartbeat)
|
||||
desktopAuth.POST("/clients/offline", desktopClientHandler.Offline)
|
||||
desktopAuth.POST("/clients/revoke", desktopClientHandler.Revoke)
|
||||
desktopAuth.GET("/events", desktopEventsHandler.Stream)
|
||||
desktopAuth.GET("/dispatch", desktopDispatchHandler.Dispatch)
|
||||
desktopAuth.GET("/accounts", desktopAccountHandler.List)
|
||||
desktopAuth.GET("/content/articles/:id", desktopContentHandler.Article)
|
||||
@@ -54,6 +53,11 @@ func RegisterRoutes(app *bootstrap.App) {
|
||||
desktopAuth.POST("/tasks/:id/cancel", desktopTaskHandler.Cancel)
|
||||
desktopAuth.POST("/tasks/:id/result", desktopTaskHandler.Result)
|
||||
desktopAuth.POST("/publish-tasks/:id/retry", desktopTaskHandler.RetryPublish)
|
||||
desktopAuth.POST("/monitoring/tasks/lease", desktopMonitoringHandler.Lease)
|
||||
desktopAuth.POST("/monitoring/tasks/resume", desktopMonitoringHandler.Resume)
|
||||
desktopAuth.POST("/monitoring/tasks/:id/result", desktopMonitoringHandler.Result)
|
||||
desktopAuth.POST("/monitoring/tasks/:id/skip", desktopMonitoringHandler.Skip)
|
||||
desktopAuth.POST("/monitoring/tasks/:id/cancel", desktopMonitoringHandler.Cancel)
|
||||
|
||||
tenantProtected := protected.Group("/tenant")
|
||||
tenantProtected.Use(RequireActiveTenantSubscription(repository.NewTenantPlanRepository(app.DB)))
|
||||
|
||||
@@ -75,11 +75,16 @@ func TestDesktopClientRoutes_ClientTokenAuth(t *testing.T) {
|
||||
method string
|
||||
path string
|
||||
}{
|
||||
{http.MethodGet, "/api/desktop/events"},
|
||||
{http.MethodGet, "/api/desktop/dispatch"},
|
||||
{http.MethodGet, "/api/desktop/accounts"},
|
||||
{http.MethodPost, "/api/desktop/clients/offline"},
|
||||
{http.MethodPost, "/api/desktop/tasks/lease"},
|
||||
{http.MethodPost, "/api/desktop/tasks/:id/result"},
|
||||
{http.MethodPost, "/api/desktop/monitoring/tasks/lease"},
|
||||
{http.MethodPost, "/api/desktop/monitoring/tasks/resume"},
|
||||
{http.MethodPost, "/api/desktop/monitoring/tasks/:id/result"},
|
||||
{http.MethodPost, "/api/desktop/monitoring/tasks/:id/skip"},
|
||||
{http.MethodPost, "/api/desktop/monitoring/tasks/:id/cancel"},
|
||||
}
|
||||
|
||||
for _, rt := range routes {
|
||||
|
||||
Reference in New Issue
Block a user