- R4:封存子进程 SIGTERM 宽限停、libraryctl 信号转 ctx;失败/超时后 agent 再做 有界 recover,恢复失败即降级阻断封存并给出原因;启动先恢复、关闭等恢复完成 - R5:封存闸口以已存 payload 为准,同作业号换范围 409 且不入新队;重放回显真实状态 - R6:作业列表按类型查询(ListByType),不再先取全局再过滤 - R7:根目录 libraryagent 二进制移出版本库,保留运行产物并精确忽略 - UI:发布管理只保留对内/对外两目标,整合快照/完整性/作业/存储;链路未接线项禁用 并标原因;整库版本独立导航并入;文档级旧流程折叠保留
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -61,8 +62,13 @@ func libraryCaptureEnqueue(w http.ResponseWriter, r *http.Request, config Config
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
requestedID := fmt.Sprintf("library-capture-%s-%d", request.Scope, now.UnixNano())
|
||||
// The idempotency key is the capture job id: the same logical capture must map
|
||||
// to one queue row. Binding the scope into the key would silently create a
|
||||
// second job instead of reporting a conflicting request, so the conflict is
|
||||
// detected by comparing the stored payload below.
|
||||
job, err := config.Store.Enqueue(r.Context(), jobs.EnqueueRequest{
|
||||
ID: fmt.Sprintf("library-capture-%s-%d", request.Scope, now.UnixNano()),
|
||||
ID: requestedID,
|
||||
Type: libraryCaptureJobType,
|
||||
Payload: payload,
|
||||
AvailableAt: now,
|
||||
@@ -73,24 +79,55 @@ func libraryCaptureEnqueue(w http.ResponseWriter, r *http.Request, config Config
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot enqueue capture job"})
|
||||
return
|
||||
}
|
||||
// Enqueue returns the existing row for a reused key. Answer from that row, not
|
||||
// from the current request: a reused job id with another scope must be refused
|
||||
// instead of being reported as a job for the new scope.
|
||||
var stored struct {
|
||||
Scope string `json:"scope"`
|
||||
CaptureJob string `json:"capture_job"`
|
||||
RequestedBy string `json:"requested_by"`
|
||||
}
|
||||
if err = json.Unmarshal(job.Payload, &stored); err != nil || stored.CaptureJob != captureJob {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{
|
||||
"code": "library_capture_conflict", "error": "该作业号已被另一个封存请求占用",
|
||||
"existing_scope": stored.Scope, "requested_scope": request.Scope,
|
||||
})
|
||||
return
|
||||
}
|
||||
if stored.Scope != request.Scope {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{
|
||||
"code": "library_capture_scope_conflict", "error": "同一作业号已按另一个范围投递",
|
||||
"existing_scope": stored.Scope, "requested_scope": request.Scope,
|
||||
})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{
|
||||
"job_id": job.ID, "capture_job": captureJob, "scope": request.Scope, "state": job.State,
|
||||
"job_id": job.ID, "capture_job": stored.CaptureJob, "scope": stored.Scope,
|
||||
"state": job.State, "reused": job.ID != requestedID, "requested_by": stored.RequestedBy,
|
||||
})
|
||||
}
|
||||
|
||||
// libraryCaptureJobs lists recent capture jobs so the page can show real state
|
||||
// instead of an optimistic spinner. It reads the queue, not the archive.
|
||||
func libraryCaptureJobs(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
queue, err := config.Store.List(r.Context(), 200)
|
||||
// Ask the store for this type only. Reading the global queue and filtering in
|
||||
// the handler would hide capture jobs behind unrelated traffic and make the
|
||||
// page claim "idle" while a capture is still queued.
|
||||
byType, ok := config.Store.(interface {
|
||||
ListByType(ctx context.Context, jobType string, limit int) ([]jobs.Job, error)
|
||||
})
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"available": false,
|
||||
"reason": "任务存储不支持按类型查询,页面不会据此判断空闲", "jobs": []any{}})
|
||||
return
|
||||
}
|
||||
queue, err := byType.ListByType(r.Context(), libraryCaptureJobType, 20)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read capture jobs"})
|
||||
return
|
||||
}
|
||||
views := make([]map[string]any, 0, 20)
|
||||
views := make([]map[string]any, 0, len(queue))
|
||||
for _, job := range queue {
|
||||
if job.Type != libraryCaptureJobType || len(views) >= 20 {
|
||||
continue
|
||||
}
|
||||
var payload struct {
|
||||
Scope string `json:"scope"`
|
||||
CaptureJob string `json:"capture_job"`
|
||||
|
||||
@@ -86,18 +86,25 @@ func TestLibraryCaptureRejectsInvalidRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureJobsListsOnlyCaptureJobs(t *testing.T) {
|
||||
store := &fakeJobStore{listResult: []jobs.Job{
|
||||
{ID: "a", Type: libraryCaptureJobType, State: "completed",
|
||||
Payload: json.RawMessage(`{"scope":"internal","capture_job":"lib-internal-1","requested_by":"yaozhanghao"}`),
|
||||
CreatedAt: time.Date(2026, 10, 28, 3, 0, 0, 0, time.UTC)},
|
||||
{ID: "b", Type: "artifact.fetch", State: "completed"},
|
||||
}}
|
||||
func TestLibraryCaptureJobsAsksStoreForItsOwnType(t *testing.T) {
|
||||
// The global queue is busy with unrelated traffic; the capture job must still
|
||||
// be visible because the query is type-scoped, not filtered after the fact.
|
||||
store := &fakeJobStore{
|
||||
listResult: []jobs.Job{{ID: "busy", Type: "artifact.fetch", State: "running"}},
|
||||
listByTypeResult: []jobs.Job{
|
||||
{ID: "a", Type: libraryCaptureJobType, State: "completed",
|
||||
Payload: json.RawMessage(`{"scope":"internal","capture_job":"lib-internal-1","requested_by":"yaozhanghao"}`),
|
||||
CreatedAt: time.Date(2026, 10, 28, 3, 0, 0, 0, time.UTC)},
|
||||
},
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryCaptureJobs(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/library/captures", nil), Config{Store: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d", recorder.Code)
|
||||
}
|
||||
if store.listType != libraryCaptureJobType {
|
||||
t.Fatalf("store queried with type %q", store.listType)
|
||||
}
|
||||
var body struct {
|
||||
Available bool `json:"available"`
|
||||
Jobs []map[string]any `json:"jobs"`
|
||||
@@ -109,3 +116,44 @@ func TestLibraryCaptureJobsListsOnlyCaptureJobs(t *testing.T) {
|
||||
t.Fatalf("unexpected body %+v", body)
|
||||
}
|
||||
}
|
||||
|
||||
// Reusing a capture job id with another scope must be refused, and the response
|
||||
// must report the scope that was actually stored.
|
||||
func TestLibraryCaptureRejectsScopeConflictForReusedJob(t *testing.T) {
|
||||
store := &fakeJobStore{enqueueResult: &jobs.Job{ID: "existing-job", Type: libraryCaptureJobType,
|
||||
State: jobs.StateCompleted,
|
||||
Payload: json.RawMessage(`{"scope":"internal","capture_job":"lib-shared","requested_by":"yaozhanghao"}`)}}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryCaptureEnqueue(recorder, captureRequest(`{"scope":"external","job":"lib-shared"}`),
|
||||
Config{Store: store, LibraryAgentSocket: "/run/library-agent/agent.sock"})
|
||||
if recorder.Code != http.StatusConflict {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["code"] != "library_capture_scope_conflict" || body["existing_scope"] != "internal" || body["requested_scope"] != "external" {
|
||||
t.Fatalf("unexpected conflict body %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
// A reused job id with an identical request is answered from the stored row.
|
||||
func TestLibraryCaptureReplayReportsStoredJob(t *testing.T) {
|
||||
store := &fakeJobStore{enqueueResult: &jobs.Job{ID: "existing-job", Type: libraryCaptureJobType,
|
||||
State: jobs.StateCompleted,
|
||||
Payload: json.RawMessage(`{"scope":"internal","capture_job":"lib-shared","requested_by":"yaozhanghao"}`)}}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryCaptureEnqueue(recorder, captureRequest(`{"scope":"internal","job":"lib-shared"}`),
|
||||
Config{Store: store, LibraryAgentSocket: "/run/library-agent/agent.sock"})
|
||||
if recorder.Code != http.StatusAccepted {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["scope"] != "internal" || body["capture_job"] != "lib-shared" || body["reused"] != true || body["job_id"] != "existing-job" {
|
||||
t.Fatalf("unexpected replay body %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1526,6 +1526,10 @@ type fakeJobStore struct {
|
||||
enqueued jobs.EnqueueRequest
|
||||
enqueueResult *jobs.Job
|
||||
listResult []jobs.Job
|
||||
listType string
|
||||
listByTypeResult []jobs.Job
|
||||
listByTypeLimit int
|
||||
listByTypeErr error
|
||||
listLimit int
|
||||
assetResult governance.SourceAssetPage
|
||||
assetFilter governance.SourceAssetFilter
|
||||
@@ -1871,7 +1875,9 @@ func (s *fakeJobStore) ActivateUISettingsWithOperation(_ context.Context, revisi
|
||||
func (s *fakeJobStore) Enqueue(_ context.Context, request jobs.EnqueueRequest, _ time.Time) (*jobs.Job, error) {
|
||||
s.enqueued = request
|
||||
if s.enqueueEchoRequestedID {
|
||||
return &jobs.Job{ID: request.ID, Type: request.Type, State: jobs.StatePending}, nil
|
||||
// The real store returns the stored row, including its payload: a reused
|
||||
// idempotency key must be judged by that payload, not by the request.
|
||||
return &jobs.Job{ID: request.ID, Type: request.Type, State: jobs.StatePending, Payload: request.Payload}, nil
|
||||
}
|
||||
return s.enqueueResult, nil
|
||||
}
|
||||
@@ -1881,6 +1887,12 @@ func (s *fakeJobStore) List(_ context.Context, limit int) ([]jobs.Job, error) {
|
||||
return s.listResult, nil
|
||||
}
|
||||
|
||||
func (s *fakeJobStore) ListByType(_ context.Context, jobType string, limit int) ([]jobs.Job, error) {
|
||||
s.listType = jobType
|
||||
s.listByTypeLimit = limit
|
||||
return s.listByTypeResult, s.listByTypeErr
|
||||
}
|
||||
|
||||
func (s *fakeJobStore) ListTestPublications(_ context.Context, filter governance.TestPublicationFilter) (governance.TestPublicationPage, error) {
|
||||
s.publicationFilter = filter
|
||||
return s.publicationResult, nil
|
||||
|
||||
Reference in New Issue
Block a user