feat: 实现客户端注册与任务领取接口
Admin 的第一个业务功能。选它打头是因为它是穿透所有层的最薄一条竖切
(HTTP → handler/api → service → repository → SQLite → handler/web → 页面),
一个工单把分层模式立起来,后面四个模块照抄;同时它是与 Client 联调的接口,
能解锁另一条并行的工作线。
实现
- POST /tasks/claim:注册 + 领取。注册就在这里做,没有单独的注册接口,
也没有心跳(理由见 docs/admin/04-client-api.md §3)
- 领取用条件更新 + 检查影响行数防并发,SQLite 没有 SELECT FOR UPDATE
- 客户端列表页:查询、按名称搜索、批量删除
- 在线状态是**算出来的**(last_seen_at 在 10 分钟内),数据库里没有该字段
- CSRF 中间件:双提交 Cookie,手写 82 行不引依赖。
**只挂页面路由**,/api/v1/client/* 不能加——Client 不是浏览器、没有 Cookie
- 14 个单元测试
修复一个真 bug:PRAGMA 必须写进 DSN
并发领取测试报 database is locked (SQLITE_BUSY)。根因是
PRAGMA busy_timeout 每连接生效,而 database/sql 是连接池——
db.Exec("PRAGMA ...") 只作用于当时那条连接,池子新开的连接没执行过。
单线程正常、一并发就炸。改成 DSN 传参后并发测试跑 20 次全过。
这个坑已写进 docs/admin/03-data-model.md §2.1。
与工单的两处差异
- 去掉 name_is_custom 列后,"人工改的名字不被覆盖"改用更简单的做法:
ON CONFLICT DO UPDATE SET 里不含 name,即只在首次注册时写入。
效果相同,零额外字段、零迁移。已同步 04 §3
- 验收项"不向 dry_run 客户端分配真实下单任务"**未实现**:
tasks 表没有字段标记任务是否需要真实下单。当前真实下单开关默认关闭、
MVP 全是演练模式,暂不出问题,但开真实下单前必须补该字段,需另开工单
已验证(Go 1.23.0)
- go vet / gofmt / go test 全过,并发测试重复 20 次稳定通过
- 端到端:无任务 claim 204;插入任务后 claim 200 且 payload 含
goods_url/goods_id/options/quantity/max_price_cent、无租约无 Admin 状态;
重复 claim 204;缺 X-Client-Id 400;POST 无 CSRF token 403;
列表页两台客户端在线状态与统计正确
说明:Gitea 尚未配置,本次无对应工单号。
submit_result / submit_failure 及其幂等处理留给下一个工单。
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,10 @@ package service
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cmautobuy/admin/model"
|
||||
"cmautobuy/admin/repository"
|
||||
)
|
||||
|
||||
// ErrNotImplemented 表示该功能还没实现。
|
||||
@@ -114,13 +118,11 @@ func CreatePurchaseTasks(db *sql.DB, sybIDs []string, clientID string) (created
|
||||
// **没有单独的注册接口,也没有心跳**——注册就在 claim 里做,
|
||||
// 理由见 docs/admin/04-client-api.md §3。
|
||||
//
|
||||
// 规则:
|
||||
// - 新 client_id 就新增,已有就更新 device/capabilities/last_seen_at;
|
||||
// - name 若已被人工改过,**不要用客户端上报的覆盖**;
|
||||
// - 客户端没上报 name 时,用 client_id 当显示名。
|
||||
func RegisterClient(db *sql.DB, clientID, name, address, platform, pddPackage, capabilitiesJSON string) error {
|
||||
// TODO(骨架): upsert clients
|
||||
return ErrNotImplemented
|
||||
// 关于 name:**只在第一次注册时写入,之后不再更新**。
|
||||
// 这样操作员在界面上改成好记的名字后,客户端每次 claim
|
||||
// 都不会把它覆盖回去。客户端没上报 name 时用 clientID 当显示名。
|
||||
func RegisterClient(db *sql.DB, c model.Client) error {
|
||||
return repository.UpsertClient(db, c)
|
||||
}
|
||||
|
||||
// TouchClient 刷新 last_seen_at。
|
||||
@@ -129,6 +131,44 @@ func RegisterClient(db *sql.DB, clientID, name, address, platform, pddPackage, c
|
||||
// 只在 claim 里调的话,客户端执行长任务期间不调 claim,
|
||||
// 会被误判成离线。
|
||||
func TouchClient(db *sql.DB, clientID string) error {
|
||||
// TODO(骨架)
|
||||
return ErrNotImplemented
|
||||
return repository.TouchClient(db, clientID)
|
||||
}
|
||||
|
||||
// ClientView 是客户端列表页要显示的一行。
|
||||
// Status 是**算出来的**,数据库里没有这个字段。
|
||||
type ClientView struct {
|
||||
model.Client
|
||||
Status string
|
||||
}
|
||||
|
||||
// ListClientViews 查客户端列表,并把在线状态算出来。
|
||||
func ListClientViews(db *sql.DB, keyword string, threshold time.Duration) ([]ClientView, error) {
|
||||
clients, err := repository.ListClients(db, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
|
||||
views := make([]ClientView, 0, len(clients))
|
||||
for _, c := range clients {
|
||||
views = append(views, ClientView{
|
||||
Client: c,
|
||||
Status: c.StatusText(now, threshold),
|
||||
})
|
||||
}
|
||||
return views, nil
|
||||
}
|
||||
|
||||
// DeleteClients 批量删除,返回实际删除条数。
|
||||
func DeleteClients(db *sql.DB, clientIDs []string) (int64, error) {
|
||||
return repository.DeleteClients(db, clientIDs)
|
||||
}
|
||||
|
||||
// ---------- 领取任务 ----------
|
||||
|
||||
// ClaimNextTask 为客户端领取一个任务,没有可领的返回 (nil, nil)。
|
||||
//
|
||||
// 调用方拿到 nil 要返回 204 No Content,**不是 200 加空对象**。
|
||||
func ClaimNextTask(db *sql.DB, clientID string, supportedTypes []string) (*model.Task, error) {
|
||||
return repository.ClaimNextTask(db, clientID, supportedTypes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user