fix(jd-collect): 补「激活」这一步——页面先 PENDING->RUNNING 再推客户端队列,否则分片全被拒
真机 task 34399 实录:页面建完单直接推客户端,任务仍是 PENDING,JdResultIngestService 只收 RUNNING 的分片、心跳对非 RUNNING 回 alive=false,客户端第一个分片被「任务不在运行中(PENDING),回传被拒」后当场收手,采集白跑一轮。相似ASIN/货源/外观专利/数据采集四条链都有同名 activate 入口,京东这条在建单入口漏了。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import {
|
||||
activateJdCollectTask,
|
||||
createJdCollectTask,
|
||||
fetchJdCollectProgress,
|
||||
type JdCollectProgressVo,
|
||||
@@ -27,12 +28,13 @@ type ProgressPhase = 'idle' | 'running' | 'done' | 'failed'
|
||||
const POLL_INTERVAL_MS = 2000
|
||||
/** 一小时还没终态就停轮询(任务本身有 stale 兜底,页面不该无限烧请求)。 */
|
||||
const POLL_TIMEOUT_MS = 60 * 60 * 1000
|
||||
/** 页面不再让用户填店铺名:店铺名称由客户端从京麦后台读,建单先按此占位记录。 */
|
||||
const DEFAULT_SHOP_NAME = '京麦后台'
|
||||
|
||||
const homeHref = resolvePageHref('/new_web_source/home.html')
|
||||
const backHref = resolvePageHref('/new_web_source/jd-console.html')
|
||||
|
||||
const username = ref('admin')
|
||||
const shopName = ref('')
|
||||
const submitting = ref(false)
|
||||
const taskId = ref<number | null>(null)
|
||||
const progress = ref<JdCollectProgressVo | null>(null)
|
||||
@@ -147,6 +149,27 @@ async function pollProgress(id: number) {
|
||||
}, POLL_INTERVAL_MS)
|
||||
}
|
||||
|
||||
/** 激活任务(PENDING→RUNNING),返回 null 表示可推队列,返回 string 表示失败原因。
|
||||
*
|
||||
* 2026-11-14 真机实录:页面建完单直接推客户端,任务还是 PENDING ⇒ 客户端第一个分片被
|
||||
* 「任务不在运行中(PENDING)」拒收、心跳回 alive=false 要求客户端收手,采集白跑一轮。
|
||||
* 相似ASIN/货源/外观专利/上架四条链都有这一步,京东这条在建单入口漏了。
|
||||
*/
|
||||
async function activateTask(id: number): Promise<string | null> {
|
||||
try {
|
||||
await activateJdCollectTask(id)
|
||||
return null
|
||||
} catch (err) {
|
||||
const text = err instanceof Error ? err.message : ''
|
||||
// 客户端兜底拉取已原子认领(PENDING→RUNNING)时后端回这句:等同激活成功,不当失败展示。
|
||||
if (text.includes('已在执行中')) {
|
||||
return null
|
||||
}
|
||||
console.error('[jd-collect] 激活任务失败', err)
|
||||
return '任务激活失败,本机客户端领不到这一单,请稍后重试'
|
||||
}
|
||||
}
|
||||
|
||||
/** 建单成功后推送本机队列;返回 null 表示环境无队列能力(浏览器预览),返回 string 表示失败原因。 */
|
||||
async function pushToClientQueue(
|
||||
id: number,
|
||||
@@ -181,19 +204,26 @@ async function startCollect() {
|
||||
progress.value = null
|
||||
phase.value = 'running'
|
||||
submitting.value = true
|
||||
const shop = shopName.value.trim() || '京麦后台'
|
||||
const shop = DEFAULT_SHOP_NAME
|
||||
const tabs: JdListTab[] = ['ONSALE', 'OFFSALE']
|
||||
try {
|
||||
const vo = await createJdCollectTask(shop, tabs, 0)
|
||||
taskId.value = vo.taskId
|
||||
let pushNote = ''
|
||||
try {
|
||||
const pushError = await pushToClientQueue(vo.taskId, shop, tabs)
|
||||
if (pushError) pushNote = ` ${pushError}`
|
||||
} catch (pushErr) {
|
||||
// 推送自身异常不回滚建单:任务可由服务端兜底拉取认领。
|
||||
pushNote = ' 推送本机队列失败,任务将由服务端兜底派发给本机客户端执行'
|
||||
console.error('[jd-collect] 推送本机队列失败', pushErr)
|
||||
const activateError = await activateTask(vo.taskId)
|
||||
if (activateError) {
|
||||
// 没激活成功就不推队列:推了也是白跑一轮(分片全被拒)。任务留在 PENDING,
|
||||
// 由服务端兜底拉取认领后照样会跑,所以这里只提示、不回滚建单。
|
||||
pushNote = activateError
|
||||
} else {
|
||||
try {
|
||||
const pushError = await pushToClientQueue(vo.taskId, shop, tabs)
|
||||
if (pushError) pushNote = ` ${pushError}`
|
||||
} catch (pushErr) {
|
||||
// 推送自身异常不回滚建单:任务可由服务端兜底拉取认领。
|
||||
pushNote = ' 推送本机队列失败,任务将由服务端兜底派发给本机客户端执行'
|
||||
console.error('[jd-collect] 推送本机队列失败', pushErr)
|
||||
}
|
||||
}
|
||||
if (pushNote) {
|
||||
errorMessage.value = pushNote.trim()
|
||||
@@ -248,17 +278,6 @@ onBeforeUnmount(() => {
|
||||
|
||||
<main class="jd-body">
|
||||
<div class="collect-card">
|
||||
<div class="form-group">
|
||||
<label for="jd-shop-name">店铺名称(可选)</label>
|
||||
<input
|
||||
id="jd-shop-name"
|
||||
v-model="shopName"
|
||||
type="text"
|
||||
placeholder="留空按「京麦后台」记录;填了便于在后台「京东数据库」区分店铺"
|
||||
:disabled="isRunning"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button class="collect-btn" type="button" :disabled="isRunning" @click="startCollect">
|
||||
{{ isRunning ? '获取中…' : '一键获取店铺数据' }}
|
||||
</button>
|
||||
@@ -454,39 +473,6 @@ onBeforeUnmount(() => {
|
||||
background: #232a3b;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 8px;
|
||||
color: #c8d2e2;
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.form-group input[type='text'] {
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid #3e4a62;
|
||||
border-radius: 8px;
|
||||
background: #1a1f2e;
|
||||
color: #f5f8fc;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.form-group input[type='text']:focus {
|
||||
border-color: #e1251b;
|
||||
}
|
||||
|
||||
.form-group input[type='text']::placeholder {
|
||||
color: #6b778d;
|
||||
}
|
||||
|
||||
.collect-btn {
|
||||
width: 100%;
|
||||
padding: 13px 0;
|
||||
|
||||
@@ -207,6 +207,8 @@ export const API_ENDPOINTS = {
|
||||
/** 京东店铺商品采集(v1.4):建单走 LOCAL 通道,由本机客户端在已登录京麦的窗口内执行。 */
|
||||
jdCollect: {
|
||||
create: '/api/jd-collect/tasks',
|
||||
/** 激活(PENDING→RUNNING):推客户端队列**之前**必须调用,否则分片被拒、心跳要求客户端收手 */
|
||||
activate: '/api/jd-collect/tasks/{taskId}/activate',
|
||||
/** 采集进度(用户端「获取店铺数据」进度条数据源) */
|
||||
progress: '/api/jd-collect/tasks/{taskId}/progress',
|
||||
heartbeat: '/api/jd-collect/tasks/{taskId}/heartbeat',
|
||||
|
||||
@@ -47,11 +47,29 @@ export function fetchJdCollectProgress(taskId: number) {
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活任务(PENDING→RUNNING):推客户端队列**之前**调用。
|
||||
*
|
||||
* 2026-11-14 真机实录:漏掉这一步,客户端第一个分片会被「任务不在运行中(PENDING)」拒收、
|
||||
* 心跳回 alive=false 让客户端收手——采集白跑一轮。相似ASIN/货源等链路的同名入口即此形状。
|
||||
* `user_id` 是 @RequestParam(与进度接口同口径),不是请求体字段。
|
||||
*/
|
||||
export function activateJdCollectTask(taskId: number) {
|
||||
const path = buildJavaUrl(API_ENDPOINTS.jdCollect.activate.replace('{taskId}', String(taskId)))
|
||||
const query = `?user_id=${encodeURIComponent(String(getCurrentUserId()))}`
|
||||
return unwrapJavaResponse(post<JavaApiResponse<null>, Record<string, unknown>>(path + query, {}))
|
||||
}
|
||||
|
||||
export function createJdCollectTask(shopName: string, listTabs: JdListTab[], maxProducts = 0) {
|
||||
return unwrapJavaResponse(post<JavaApiResponse<JdCollectTaskVo>, Record<string, unknown>>(
|
||||
buildJavaUrl(API_ENDPOINTS.jdCollect.create),
|
||||
{
|
||||
user_id: getCurrentUserId(),
|
||||
// 建单请求体是 JSON(@RequestBody + JdCollectTaskCreateRequest),字段名必须是
|
||||
// camelCase `userId`:Java 侧没有开 snake_case 命名策略,传 `user_id` 会被静默丢掉,
|
||||
// 管理员(mayActForOtherUsers 走请求体值)建单时 userId 恒为 null ⇒ 后端回
|
||||
// 「user_id 不合法」,页面上表现为点了「一键获取店铺数据」立刻失败。
|
||||
// 进度接口的 `?user_id=` 是 @RequestParam,那个名字不动。
|
||||
userId: getCurrentUserId(),
|
||||
shopName,
|
||||
listTabs,
|
||||
maxProducts,
|
||||
|
||||
Reference in New Issue
Block a user