feat: 换到 Gin 主流栈并补齐运维能力
HTTP 用 Gin,配置用 Viper,日志用 zap,数据用 sqlx;保留原有接口与会商行为。 顺带补上就绪探活、请求号、滚动日志、systemd 与 Dockerfile。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
0c45b86d67
commit
bd427b57a1
26 files changed
+1213
-302
No files matched your search
@@ -2,7 +2,6 @@ package worker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -10,24 +9,30 @@ import (
|
||||
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/deerflow"
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/domain"
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/logging"
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/repo"
|
||||
)
|
||||
|
||||
// LiveChainRunner 真模型按链条顺序执行(被 LiveWorker 异步调用)。
|
||||
type LiveChainRunner struct {
|
||||
DF *deerflow.Client
|
||||
Repo *repo.JobRepo
|
||||
DF *deerflow.Client
|
||||
Repo *repo.JobRepo
|
||||
Coord string
|
||||
|
||||
mu sync.Mutex
|
||||
lastFlush time.Time
|
||||
mu sync.Mutex
|
||||
lastFlush time.Time
|
||||
}
|
||||
|
||||
// Run 跑完一场真模型会商。调用方已经把状态改成 running,这里不再抢队列。
|
||||
//
|
||||
// 顺序:席位不够直接失败 → 校验模型(禁止网关静默换模)→ 没有 thread 就 init →
|
||||
// 总控派活、席位发言,人工干预开着则每席结束后暂停 → 收口写成 done。
|
||||
// 边界:任一步 panic 记失败,不当成功;取消看库里的 cancel_flag,不靠内存标记。
|
||||
func (r *LiveChainRunner) Run(job *domain.AnalysisJob) {
|
||||
jobID := job.ID
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
log.Printf("真模型会商 panic jobId=%s: %v", jobID, rec)
|
||||
logging.L.Errorw("真模型会商 panic", logging.KeyJobID, jobID, "err", rec)
|
||||
r.fail(job, fmt.Sprint(rec))
|
||||
}
|
||||
}()
|
||||
@@ -285,9 +290,15 @@ func (r *LiveChainRunner) Run(job *domain.AnalysisJob) {
|
||||
plan["awaitReason"] = nil
|
||||
job.Plan = plan
|
||||
r.flush(job)
|
||||
log.Printf("真模型会商完成 jobId=%s", jobID)
|
||||
logging.L.Infow("真模型会商完成",
|
||||
logging.KeyJobID, jobID,
|
||||
logging.KeyStatus, domain.StatusDone,
|
||||
logging.KeyModel, job.Model,
|
||||
)
|
||||
}
|
||||
|
||||
// pauseForInput 把作业停在 awaiting_input。seatIndex 记下一席,等人或等确认后再从这里继续。
|
||||
// reason 只有两类约定:澄清(用户要补一句)和 manual_continue(人工确认再往下走)。
|
||||
func (r *LiveChainRunner) pauseForInput(job *domain.AnalysisJob, plan map[string]interface{}, next int, reason, detail string) {
|
||||
plan["seatIndex"] = next
|
||||
plan["awaitReason"] = reason
|
||||
@@ -301,7 +312,11 @@ func (r *LiveChainRunner) pauseForInput(job *domain.AnalysisJob, plan map[string
|
||||
}
|
||||
putProgress(job, job.ConsensusPercentage, msg)
|
||||
r.flush(job)
|
||||
log.Printf("作业暂停等待输入 jobId=%s reason=%s", job.ID, reason)
|
||||
logging.L.Infow("作业暂停等待输入",
|
||||
logging.KeyJobID, job.ID,
|
||||
logging.KeyStatus, domain.StatusAwaitingInput,
|
||||
logging.KeyPhase, reason,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *LiveChainRunner) deltaHandler(job *domain.AnalysisJob, bubbleID string) deerflow.TextDeltaHandler {
|
||||
@@ -326,6 +341,8 @@ func (r *LiveChainRunner) flush(job *domain.AnalysisJob) {
|
||||
r.flushLocked(job)
|
||||
}
|
||||
|
||||
// flushLocked 把内存里的作业写回表。写之前从库里拉模型、人工开关和尚未消费的注入,
|
||||
// 这样页面上改设置不会被本轮旧内存盖掉。调用方必须已持有 r.mu。
|
||||
func (r *LiveChainRunner) flushLocked(job *domain.AnalysisJob) {
|
||||
plan := ensurePlan(job)
|
||||
r.pullManualFromDB(job, plan)
|
||||
@@ -353,6 +370,7 @@ func (r *LiveChainRunner) markCancelled(job *domain.AnalysisJob) {
|
||||
job.Error = "cancelled"
|
||||
putProgress(job, job.ConsensusPercentage, "cancelled")
|
||||
r.flush(job)
|
||||
logging.L.Infow("真模型会商已取消", logging.KeyJobID, job.ID, logging.KeyStatus, domain.StatusCancelled)
|
||||
}
|
||||
|
||||
func (r *LiveChainRunner) fail(job *domain.AnalysisJob, message string) {
|
||||
@@ -367,6 +385,17 @@ func (r *LiveChainRunner) fail(job *domain.AnalysisJob, message string) {
|
||||
}
|
||||
putProgress(job, 0, "error")
|
||||
r.flush(job)
|
||||
// 详情截断,避免把整段模型正文打进日志
|
||||
detail := message
|
||||
if rs := []rune(detail); len(rs) > 200 {
|
||||
detail = string(rs[:200]) + "…"
|
||||
}
|
||||
logging.L.Warnw("真模型会商失败",
|
||||
logging.KeyJobID, job.ID,
|
||||
logging.KeyStatus, domain.StatusError,
|
||||
logging.KeyModel, job.Model,
|
||||
"detail", detail,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *LiveChainRunner) pullModelFromDB(job *domain.AnalysisJob) {
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/domain"
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/logging"
|
||||
"git.gozeros.com/wsk/eightbf-analysis-go/internal/repo"
|
||||
)
|
||||
|
||||
// LiveWorker 定时扫 queued,最多并发 2 场交给 LiveChainRunner。
|
||||
// LiveWorker 定时扫 queued,最多并发 MaxInflight 场交给 LiveChainRunner。
|
||||
type LiveWorker struct {
|
||||
Repo *repo.JobRepo
|
||||
Runner *LiveChainRunner
|
||||
PollEvery time.Duration
|
||||
inflight int32
|
||||
Repo *repo.JobRepo
|
||||
Runner *LiveChainRunner
|
||||
PollEvery time.Duration
|
||||
MaxInflight int32
|
||||
inflight int32
|
||||
}
|
||||
|
||||
func (w *LiveWorker) Start() {
|
||||
@@ -31,22 +32,26 @@ func (w *LiveWorker) Start() {
|
||||
}
|
||||
|
||||
func (w *LiveWorker) poll() {
|
||||
if atomic.LoadInt32(&w.inflight) >= 2 {
|
||||
max := w.MaxInflight
|
||||
if max < 1 {
|
||||
max = 2
|
||||
}
|
||||
if atomic.LoadInt32(&w.inflight) >= max {
|
||||
return
|
||||
}
|
||||
queued, err := w.Repo.FindQueuedJobs()
|
||||
if err != nil {
|
||||
log.Printf("LiveWorker 扫队列失败: %v", err)
|
||||
logging.L.Warnw("LiveWorker 扫队列失败", "err", err)
|
||||
return
|
||||
}
|
||||
for _, job := range queued {
|
||||
if atomic.LoadInt32(&w.inflight) >= 2 {
|
||||
if atomic.LoadInt32(&w.inflight) >= max {
|
||||
break
|
||||
}
|
||||
job.Status = domain.StatusRunning
|
||||
job.Phase = "claimed"
|
||||
if err := w.Repo.UpdateProgress(job); err != nil {
|
||||
log.Printf("LiveWorker 占坑失败 jobId=%s: %v", job.ID, err)
|
||||
logging.L.Warnw("LiveWorker 占坑失败", logging.KeyJobID, job.ID, "err", err)
|
||||
continue
|
||||
}
|
||||
atomic.AddInt32(&w.inflight, 1)
|
||||
@@ -57,7 +62,13 @@ func (w *LiveWorker) poll() {
|
||||
if err != nil || fresh == nil {
|
||||
return
|
||||
}
|
||||
log.Printf("开始真模型会商 jobId=%s", jobID)
|
||||
logging.L.Infow("开始真模型会商",
|
||||
logging.KeyJobID, jobID,
|
||||
logging.KeyRequestID, fresh.RequestID,
|
||||
logging.KeyTaskID, fresh.TaskID,
|
||||
logging.KeyModel, fresh.Model,
|
||||
logging.KeyStatus, fresh.Status,
|
||||
)
|
||||
w.Runner.Run(fresh)
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user