feat: 爬虫页取代抓取任务/入库任务两页,抓取改全量+覆盖率跳过

一、后台只留一个「爬虫」页(/admin/spider)
抓取与入库本就是同一条流水线的上下游,分两页看谁都看不全。
新页三块:状态条(在线/卡住 + 队列数 + 待入库数)、正在抓(含进度与心跳)、
最近入库结果(品牌/场次/结果/说明)。只读,取消与重试一并下线。

二、抓取一律全量
删除品牌页的场次上限下拉与 hidden maxShows=3,入队固定 0(全量)。
crawl_tasks.max_shows 列保留,历史任务照常显示。

三、跳过判定改覆盖率
- brand_runways 增加 expected_images(spider 上报的应有张数)
- /shows/covered 返回 images + expected
- 实际张数 >= 应有 - 5 才跳过;expected=0 时回落 10 张阈值
- 实体已存在时不再直接跳过:按 key 比对把缺的图补进库(repaired)
  否则「判定不完整→重爬→后端跳过」会形成死循环,带宽白花、缺图永远补不上

四、租约 900→300 秒,且心跳算存活
只看 locked_at 会把跑了几小时的全量抓取误判成卡死;
加上 progress_at 后,真卡住 5 分钟才回收。
This commit is contained in:
toom1996
2026-10-25 23:59:54 +08:00
parent b30f18fc20
commit e930ac41c8
22 changed files with 992 additions and 527 deletions

View File

@ -32,17 +32,13 @@
</div>
{{/* 批量操作表单:勾选框用 form="bulk" 关联,避免与行内表单嵌套 */}}
<form id="bulk" method="post" action="/admin/crawl/bulk"></form>
<form id="bulk" method="post" action="/admin/spider/bulk"></form>
<div class="ops" style="margin:10px 0">
<label class="muted">每品牌场次上限
<select name="maxShows" form="bulk">
<option value="1">1 场(最快探有无数据)</option>
<option value="3" selected>3 场(最近,推荐)</option>
<option value="0">全量(所有历史场次)</option>
</select>
</label>
<button type="submit" form="bulk" class="op">把勾选的品牌加入抓取队列</button>
<span class="muted">(勾选本页任意行,先点复选框列头可全选本页)</span>
<span class="muted">
(勾选本页任意行,先点复选框列头可全选本页。抓取一律全量:
已抓全的场次会被自动跳过,不用担心重复下载。)
</span>
</div>
<table>
@ -62,8 +58,8 @@
<td class="muted">{{if $cs}}{{$cs.LastAt}}{{else}}—{{end}}</td>
<td>
<a class="view" href="/admin/brands/{{.UID}}">编辑</a>
<form class="inline" method="post" action="/admin/crawl/brands/{{.UID}}" onsubmit="return confirm('把「{{.NameEn}}」加入抓取队列?默认只抓最近 3 场。')">
<input type="hidden" name="back" value="/admin/brands"><input type="hidden" name="maxShows" value="3">
<form class="inline" method="post" action="/admin/spider/brands/{{.UID}}" onsubmit="return confirm('把「{{.NameEn}}」加入抓取队列?(全量,已抓全的场次自动跳过)')">
<input type="hidden" name="back" value="/admin/brands">
<button type="submit" class="op">加入队列</button>
</form>
<form class="inline" method="post" action="/admin/brands/{{.UID}}/delete" onsubmit="return confirm('确认删除品牌「{{.NameEn}}」?该品牌下若还有图集会拒绝删除。')">

View File

@ -1,83 +0,0 @@
{{define "title"}}抓取任务 · 管理后台{{end}}
{{define "barLabel"}}管理后台 · 品牌抓取任务{{end}}
{{define "barRight"}}<a href="/admin/brands">去品牌管理加任务</a> · <a href="/admin/logout">退出</a>{{end}}
{{define "content"}}
{{template "bar" .}}
<div class="main">
<h1>抓取任务</h1>
<p class="muted">
任务是「从品牌管理页加入」的:勾选品牌 → 选场次上限 → 加入抓取队列。
这里看的是执行状态与失败原因。真正干活的是 spider 服务(可在另一台机器)。
</p>
<p class="muted">
待领取 {{.Queued}} · 执行中 {{.Running}} · 已完成 {{.Done}} · 失败 {{.Failed}},共 {{.Total}} 条。
<strong>「已提交 0 场」不是错误</strong>——Vogue 上很多品牌没有走秀页面(实测 A 开头里约 75%)。
</p>
<table>
<thead><tr>
<th>ID</th><th>品牌</th><th>状态</th><th>进度</th><th>场次上限</th><th>已提交</th><th>尝试</th><th>Worker</th><th>更新时间</th><th>错误</th><th>操作</th>
</tr></thead>
<tbody>
{{range .Rows}}<tr>
<td><code>{{.ID}}</code></td>
<td><a class="view" href="/admin/brands/{{.BrandUID}}">{{.BrandName}}</a></td>
<td><span class="badge" data-live-status="{{.ID}}">{{.StatusLabel}}</span></td>
<td class="progress{{if .Stale}} stale{{end}}" data-live-progress="{{.ID}}">{{if .ProgressLabel}}{{.ProgressLabel}}{{else}}—{{end}}</td>
<td>{{if eq .MaxShows 0}}<strong>全量</strong>{{else}}{{.MaxShows}}{{end}}</td>
<td data-live-shows="{{.ID}}">{{.Shows}}</td>
<td>{{.Attempts}}</td>
<td class="muted">{{if .Worker}}{{.Worker}}{{else}}—{{end}}</td>
<td class="muted">{{.UpdatedAt}}</td>
<td class="err break">{{.LastError}}</td>
<td>
<div class="ops">
{{if .CanRetry}}<form class="inline" method="post" action="/admin/crawl/tasks/{{.ID}}/retry"><button class="op" type="submit">重试</button></form>{{end}}
{{if .CanCancel}}<form class="inline" method="post" action="/admin/crawl/tasks/{{.ID}}/cancel"><button class="op" type="submit">取消</button></form>{{end}}
<form class="inline" method="post" action="/admin/crawl/tasks/{{.ID}}/delete" onsubmit="return confirm('删除任务记录?已入库的数据不受影响。')"><button class="op" type="submit">删除</button></form>
</div>
</td>
</tr>{{end}}
</tbody>
</table>
{{template "pager" .Pager}}
</div>
<script>
// 抓取进度轮询:每 4 秒只刷新「状态 / 进度 / 已提交」三列。
//
// 为什么不用 WebSocket:进度是每场秀一次的低频事件,轮询足够;
// 而且局部更新避免了整页刷新——整页刷会打断滚动位置和正在填的表单。
// 为什么不用整页 meta refresh:同上,且会把「失败原因」的展开状态一起冲掉。
(function () {
var cells = document.querySelectorAll('[data-live-progress]');
if (!cells.length) return;
function paint(items) {
var byId = {};
for (var i = 0; i < items.length; i++) byId[String(items[i].id)] = items[i];
for (var j = 0; j < cells.length; j++) {
var cell = cells[j];
var id = cell.getAttribute('data-live-progress');
var it = byId[id];
if (!it) continue;
cell.textContent = it.progress || '—';
if (it.stale) cell.classList.add('stale'); else cell.classList.remove('stale');
var st = document.querySelector('[data-live-status="' + id + '"]');
if (st) st.textContent = it.status_label;
var sh = document.querySelector('[data-live-shows="' + id + '"]');
if (sh) sh.textContent = it.shows;
}
}
function tick() {
fetch('/admin/crawl/live', { credentials: 'same-origin' })
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (d) { if (d && d.items) paint(d.items); })
.catch(function () { /* 网络抖动忽略,下一轮再拉 */ });
}
setInterval(tick, 4000);
tick();
})();
</script>
{{end}}

View File

@ -1,24 +0,0 @@
{{define "title"}}入库任务 · 管理后台{{end}}
{{define "barLabel"}}管理后台 · 爬虫入库任务{{end}}
{{define "barRight"}}<a href="/admin/logout">退出</a>{{end}}
{{define "content"}}
{{template "bar" .}}
<div class="main">
<h1>爬虫入库任务</h1>
<p class="muted">worker 每 3 秒扫描一次 pending 任务并处理;failed 可一键重新入队。共 {{.Total}} 条,当前第 {{.Page}} / {{if eq .TotalPages 0}}1{{else}}{{.TotalPages}}{{end}} 页(每页 {{.PageSize}} 条)。</p>
<table>
<thead><tr><th>ID</th><th>状态</th><th>重试</th><th>错误信息</th><th>更新时间</th><th>操作</th></tr></thead>
<tbody>
{{range .Jobs}}<tr>
<td><code>{{.ID}}</code></td>
<td><span class="badge {{.Status}}">{{.Status}}</span></td>
<td>{{.Attempts}}</td>
<td class="err break">{{.LastError}}</td>
<td class="muted">{{.UpdatedAt}}</td>
<td>{{if eq .Status "failed"}}<form class="retry" method="post" action="/admin/ingest-jobs/{{.ID}}/retry"><button class="retry" type="submit">重试</button></form>{{else}}<span class="muted">—</span>{{end}}</td>
</tr>{{end}}
</tbody>
</table>
{{template "pager" .Pager}}
</div>
{{end}}

View File

@ -0,0 +1,134 @@
{{define "title"}}爬虫 · 管理后台{{end}}
{{define "barLabel"}}管理后台 · 爬虫{{end}}
{{define "barRight"}}<a href="/admin/brands">去品牌管理加任务</a> · <a href="/admin/logout">退出</a>{{end}}
{{define "content"}}
{{template "bar" .}}
<div class="main">
<h1>爬虫</h1>
<p class="muted">
这里看「爬虫现在在干什么」:<strong>正在抓</strong>是当下,
<strong>待抓队列</strong>是排队等着的品牌,
<strong>最近入库结果</strong>是后端已经处理完的场次。
</p>
<p class="muted">
入队请去<a href="/admin/brands">品牌管理</a>勾选品牌。抓取一律全量,
已经抓全的场次会被自动跳过,不会重复下载。
</p>
<p class="muted" id="spider-status">
爬虫 <span class="badge {{.StateClass}}" id="st-state">{{.State}}</span>
· 正在抓 <strong id="st-running">{{.RunningCount}}</strong> 个
· 队列 <strong id="st-queued">{{.QueuedCount}}</strong> 个
· 待入库 <strong id="st-pending">{{.PendingIngest}}</strong> 条
</p>
<h2>正在抓</h2>
<table>
<thead><tr><th>品牌</th><th>进度</th><th>心跳</th><th>Worker</th></tr></thead>
<tbody id="running-body">
{{range .Running}}<tr>
<td><strong>{{.BrandName}}</strong></td>
<td class="progress{{if .Stale}} stale{{end}}">{{if .Progress}}{{.Progress}}{{else}}刚领取,正在拉取秀列表…{{end}}</td>
<td class="muted{{if .Stale}} stale{{end}}">{{.Beat}}前</td>
<td class="muted">{{.Worker}}</td>
</tr>{{end}}
{{if not .Running}}<tr id="running-empty"><td colspan="4" class="muted">当前没有正在抓取的任务。</td></tr>{{end}}
</tbody>
</table>
<h2>待抓队列({{.QueuedCount}})</h2>
<table>
<thead><tr><th>品牌</th><th>入队时间</th><th>已等待</th></tr></thead>
<tbody>
{{range .Queued}}<tr>
<td>{{.BrandName}}</td>
<td class="muted">{{.Since}}</td>
<td class="muted">{{.Waiting}}</td>
</tr>{{end}}
{{if not .Queued}}<tr><td colspan="3" class="muted">队列为空。</td></tr>{{end}}
</tbody>
</table>
<h2>最近入库结果</h2>
<p class="muted">最近 50 条。「已存在跳过」是正常状态——说明这场秀之前抓过,没有重复写库。</p>
<table>
<thead><tr><th>时间</th><th>品牌</th><th>场次</th><th>结果</th><th>说明</th></tr></thead>
<tbody>
{{range .Results}}<tr>
<td class="muted">{{.At}}</td>
<td>{{.BrandName}}</td>
<td>{{.Title}}</td>
<td>{{if .Bad}}<span class="badge failed">{{.Result}}</span>{{else}}<span class="badge done">{{.Result}}</span>{{end}}</td>
<td class="err break">{{.Note}}</td>
</tr>{{end}}
{{if not .Results}}<tr><td colspan="5" class="muted">还没有入库记录。</td></tr>{{end}}
</tbody>
</table>
</div>
<script>
// 抓取进度轮询:只刷新状态条与「正在抓」表格。
//
// 用 DOM API 建行而不是拼 innerHTML:品牌名来自数据库,直接拼字符串会把
// 特殊字符当成标签解析(既会破版,也是一条注入面)。
(function () {
var body = document.getElementById('running-body');
if (!body) return;
function cell(text, cls) {
var td = document.createElement('td');
td.textContent = text;
if (cls) td.className = cls;
return td;
}
function paint(list) {
while (body.firstChild) body.removeChild(body.firstChild);
if (!list.length) {
var tr0 = document.createElement('tr');
var td0 = document.createElement('td');
td0.colSpan = 4;
td0.className = 'muted';
td0.textContent = '当前没有正在抓取的任务。';
tr0.appendChild(td0);
body.appendChild(tr0);
return;
}
list.forEach(function (it) {
var tr = document.createElement('tr');
var tdBrand = document.createElement('td');
var b = document.createElement('strong');
b.textContent = it.brand || '—';
tdBrand.appendChild(b);
tr.appendChild(tdBrand);
tr.appendChild(cell(it.progress || '刚领取,正在拉取秀列表…', it.stale ? 'progress stale' : 'progress'));
tr.appendChild(cell((it.beat || '0 秒') + '前', it.stale ? 'muted stale' : 'muted'));
tr.appendChild(cell(it.worker || '—', 'muted'));
body.appendChild(tr);
});
}
function tick() {
fetch('/admin/spider/live', { credentials: 'same-origin' })
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (d) {
if (!d) return;
var st = document.getElementById('st-state');
if (st) { st.textContent = d.state; st.className = 'badge ' + d.state_class; }
var rn = document.getElementById('st-running');
if (rn) rn.textContent = (d.running || []).length;
var qn = document.getElementById('st-queued');
if (qn) qn.textContent = d.queued;
var pn = document.getElementById('st-pending');
if (pn) pn.textContent = d.pending_ingest;
paint(d.running || []);
})
.catch(function () { /* 网络抖动忽略,下一轮再拉 */ });
}
setInterval(tick, 4000);
tick();
})();
</script>
{{end}}