feat: 添加爬虫状态更新中的显示支持

在爬虫状态列中增加"正在更新"状态显示,并在爬虫过程中更新状态
This commit is contained in:
dmy
2026-01-22 14:15:07 +08:00
parent eaed16a12e
commit 2475619228
3 changed files with 57 additions and 2 deletions

View File

@@ -24,9 +24,20 @@
{{ formatDateTime(row.latestPublishDate) }}
</template>
</el-table-column>
<el-table-column label="状态" width="70">
<el-table-column label="状态" width="80">
<template #default="{ row }">
<el-tag :type="row.error && row.error.trim() ? 'danger' : (row.count > 0 ? 'success' : 'info')" size="small">
<el-tag
v-if="row.count === -1"
type="warning"
size="small"
>
正在更新...
</el-tag>
<el-tag
v-else
:type="row.error && row.error.trim() ? 'danger' : (row.count > 0 ? 'success' : 'info')"
size="small"
>
{{ row.error && row.error.trim() ? '出错' : (row.count > 0 ? '正常' : '无数据') }}
</el-tag>
</template>

View File

@@ -49,7 +49,17 @@ export class CrawlerController {
this.crawlingSources.add(sourceName);
try {
// 设置状态为正在更新count = -1
await this.crawlerService.updateCrawlStatus(sourceName, -1);
const result = await this.crawlerService.crawlSingleSource(sourceName);
// 更新状态为实际数量
await this.crawlerService.updateCrawlStatus(
sourceName,
result.success ? result.count : 0,
);
return result;
} finally {
this.crawlingSources.delete(sourceName);

View File

@@ -430,4 +430,38 @@ export class BidCrawlerService {
);
}
}
// 更新爬虫状态count = -1 表示正在更新
async updateCrawlStatus(source: string, count: number) {
try {
// 使用原生查询实现 upsert 逻辑
await this.crawlInfoRepository.manager.transaction(
async (manager) => {
// 检查记录是否存在
const existing = await manager.findOne(CrawlInfoAdd, {
where: { source },
});
if (existing) {
// 更新现有记录
await manager.update(CrawlInfoAdd, { source }, { count });
} else {
// 插入新记录
await manager.save(CrawlInfoAdd, {
source,
count,
latestPublishDate: null,
error: null,
});
}
},
);
this.logger.log(`Updated crawl status for ${source}: ${count}`);
} catch (err) {
const errorMessage = err instanceof Error ? err.message : String(err);
this.logger.error(
`Failed to update crawl status for ${source}: ${errorMessage}`,
);
}
}
}