feat: 添加爬虫状态更新中的显示支持
在爬虫状态列中增加"正在更新"状态显示,并在爬虫过程中更新状态
This commit is contained in:
@@ -24,9 +24,20 @@
|
|||||||
{{ formatDateTime(row.latestPublishDate) }}
|
{{ formatDateTime(row.latestPublishDate) }}
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="状态" width="70">
|
<el-table-column label="状态" width="80">
|
||||||
<template #default="{ row }">
|
<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 ? '正常' : '无数据') }}
|
{{ row.error && row.error.trim() ? '出错' : (row.count > 0 ? '正常' : '无数据') }}
|
||||||
</el-tag>
|
</el-tag>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -49,7 +49,17 @@ export class CrawlerController {
|
|||||||
this.crawlingSources.add(sourceName);
|
this.crawlingSources.add(sourceName);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 设置状态为正在更新(count = -1)
|
||||||
|
await this.crawlerService.updateCrawlStatus(sourceName, -1);
|
||||||
|
|
||||||
const result = await this.crawlerService.crawlSingleSource(sourceName);
|
const result = await this.crawlerService.crawlSingleSource(sourceName);
|
||||||
|
|
||||||
|
// 更新状态为实际数量
|
||||||
|
await this.crawlerService.updateCrawlStatus(
|
||||||
|
sourceName,
|
||||||
|
result.success ? result.count : 0,
|
||||||
|
);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
} finally {
|
} finally {
|
||||||
this.crawlingSources.delete(sourceName);
|
this.crawlingSources.delete(sourceName);
|
||||||
|
|||||||
@@ -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}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user