delivery

package
v0.1.40 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartWorker

func StartWorker(ctx context.Context, svc *Service)

StartWorker 后台重试 worker(main.go 调,应在 goroutine 中)。

每 workerInterval 扫一次 run_deliveries:status='pending' AND next_retry_at <= NOW() 第 1 次投递在 enqueue → attemptInBackground 已立即触发;这里只负责重试调度。

Types

type CreateTargetRequest

type CreateTargetRequest struct {
	Name       string   `json:"name" validate:"required,min=1,max=80"`
	Type       string   `json:"type" validate:"required,oneof=webhook slack"`
	URL        string   `json:"url" validate:"required,url,max=500"`
	EventTypes []string `json:"event_types,omitempty" validate:"omitempty,dive,oneof=run.completed run.failed run.canceled"`
	IsDefault  bool     `json:"is_default"`
}

CreateTargetRequest POST /api/v1/delivery-targets。

仅支持 webhook / slack 两种 type。URL 默认必须 HTTPS; 本地开发可由服务端配置允许 loopback HTTP。

type DeliverRequest

type DeliverRequest struct {
	TargetID string `json:"target_id,omitempty"`
}

DeliverRequest POST /api/v1/runs/:id/deliver。

TargetID 为空时使用用户的 is_default target;都没有则 400。

type DeliveryItem

type DeliveryItem struct {
	ID             string  `json:"id"`
	RunID          string  `json:"run_id"`
	TargetID       string  `json:"target_id"`
	TargetType     string  `json:"target_type"`
	TargetURL      string  `json:"target_url"`
	Status         string  `json:"status"`
	ResponseStatus *int32  `json:"response_status,omitempty"`
	ErrorMessage   *string `json:"error_message,omitempty"`
	AttemptCount   int32   `json:"attempt_count"`
	NextRetryAt    *string `json:"next_retry_at,omitempty"`
	CreatedAt      string  `json:"created_at"`
	UpdatedAt      string  `json:"updated_at"`
}

DeliveryItem 投递历史项(不含 payload / response_body,避免巨大响应)。

type DeliveryListFilter

type DeliveryListFilter struct {
	AgentID *string
	RunID   *string
	Status  string
	Limit   int32
}

DeliveryListFilter GET /api/v1/deliveries 查询条件。

只列出当前用户自己的外部投递记录;AgentID / RunID / Status 都是可选过滤。

type DeliveryPayload

type DeliveryPayload struct {
	Event      string                 `json:"event"`
	RunID      string                 `json:"run_id"`
	AgentID    string                 `json:"agent_id"`
	AgentSlug  string                 `json:"agent_slug"`
	AgentName  string                 `json:"agent_name"`
	Status     string                 `json:"status"`
	Input      map[string]interface{} `json:"input"`
	Output     map[string]interface{} `json:"output,omitempty"`
	CostCents  int32                  `json:"cost_cents"`
	DurationMs int32                  `json:"duration_ms"`
	StartedAt  string                 `json:"started_at"`
	FinishedAt string                 `json:"finished_at"`
}

DeliveryPayload Run 完成后投递给用户 target 的 body。

webhook target 直接收到该 JSON;slack target 由 service 层重新打包成 text。

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler Output Delivery HTTP 路由(用户侧)。

func NewHandler

func NewHandler(svc deliveryService) *Handler

func (*Handler) CreateTarget

func (h *Handler) CreateTarget(c echo.Context) error

func (*Handler) DeleteTarget

func (h *Handler) DeleteTarget(c echo.Context) error

func (*Handler) DeliverRun

func (h *Handler) DeliverRun(c echo.Context) error

func (*Handler) ListAllDeliveries

func (h *Handler) ListAllDeliveries(c echo.Context) error

func (*Handler) ListDeliveries

func (h *Handler) ListDeliveries(c echo.Context) error

func (*Handler) ListTargets

func (h *Handler) ListTargets(c echo.Context) error

func (*Handler) RegisterProtected

func (h *Handler) RegisterProtected(api *echo.Group, jwtMiddleware echo.MiddlewareFunc)

RegisterProtected 注册需要 JWT 的用户路由。

POST   /delivery-targets                       创建
GET    /delivery-targets                       列表
PATCH  /delivery-targets/:id                   更新通知类型
DELETE /delivery-targets/:id                   删除
POST   /delivery-targets/:id/default           设为默认
POST   /runs/:id/deliver                       触发投递
GET    /runs/:id/deliveries                    投递历史
GET    /deliveries                             外部投递历史列表
POST   /deliveries/:id/retry                   重试 failed

func (*Handler) RetryDelivery

func (h *Handler) RetryDelivery(c echo.Context) error

func (*Handler) SetDefault

func (h *Handler) SetDefault(c echo.Context) error

func (*Handler) UpdateTarget

func (h *Handler) UpdateTarget(c echo.Context) error

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service Output Delivery 业务层。

CRUD delivery_targets + 创建/重试 run_deliveries。 HMAC 签名(webhook)/ Slack incoming webhook(slack)。

func NewService

func NewService(pool *pgxpool.Pool, cfg ...*config.Config) *Service

func (*Service) AttemptDelivery

func (s *Service) AttemptDelivery(ctx context.Context, deliveryID uuid.UUID) error

AttemptDelivery 单次投递。

func (*Service) CreateTarget

func (s *Service) CreateTarget(ctx context.Context, userID uuid.UUID, req *CreateTargetRequest) (*TargetResponse, error)

CreateTarget 用户新增投递目标。

当 IsDefault=true 时先清掉原 default 避免唯一索引冲突。

func (*Service) DeleteTarget

func (s *Service) DeleteTarget(ctx context.Context, targetID, userID uuid.UUID) error

DeleteTarget 按 id 删除(限定 user_id)。

func (*Service) DeliverRun

func (s *Service) DeliverRun(ctx context.Context, userID, runID uuid.UUID, targetID *uuid.UUID) (*DeliveryItem, error)

DeliverRun 手动触发投递(用户 /run/:id 点投递按钮)。

run 必须属于当前用户、且终态(success / failed / timeout)。 targetID 为空时取用户 is_default target;无 default 返回 400。

func (*Service) EnqueueIfDefault

func (s *Service) EnqueueIfDefault(ctx context.Context, run *db.Run) error

EnqueueIfDefault Run 终态后由 runtime 异步调用:若用户有 default target,自动投递。

无 default → 静默跳过(不报错)。这是 setter 注入的接口实现。

func (*Service) List

func (s *Service) List(ctx context.Context, userID uuid.UUID, filter DeliveryListFilter) ([]DeliveryItem, error)

List 查询当前用户的外部投递历史,可选按 Agent / Run / 状态过滤。

func (*Service) ListByRun

func (s *Service) ListByRun(ctx context.Context, runID, userID uuid.UUID) ([]DeliveryItem, error)

ListByRun 查询某 run 的投递历史。

func (*Service) ListTargets

func (s *Service) ListTargets(ctx context.Context, userID uuid.UUID) ([]TargetResponse, error)

ListTargets 列出当前用户所有 target(不返回 secret)。

func (*Service) RetryDelivery

func (s *Service) RetryDelivery(ctx context.Context, deliveryID, userID uuid.UUID) error

RetryDelivery 手动重试 failed 投递(reset 后 worker 会重新拾起)。

func (*Service) SetDefault

func (s *Service) SetDefault(ctx context.Context, targetID, userID uuid.UUID) error

SetDefault 将指定 target 设为默认(清掉原 default)。

func (*Service) UpdateTarget

func (s *Service) UpdateTarget(ctx context.Context, targetID, userID uuid.UUID, req *UpdateTargetRequest) (*TargetResponse, error)

type TargetResponse

type TargetResponse struct {
	ID         string          `json:"id"`
	Name       string          `json:"name"`
	Type       string          `json:"type"`
	URL        string          `json:"url"`
	EventTypes []string        `json:"event_types"`
	Secret     string          `json:"secret,omitempty"`
	IsDefault  bool            `json:"is_default"`
	Config     json.RawMessage `json:"config,omitempty"`
	CreatedAt  string          `json:"created_at"`
}

TargetResponse 投递目标返回体。Secret 仅 Create / Rotate 时返回一次。

type UpdateTargetRequest

type UpdateTargetRequest struct {
	EventTypes []string `json:"event_types" validate:"required,min=1,dive,oneof=run.completed run.failed run.canceled"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL