69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { Controller, Post, Get, Param, Body } from '@nestjs/common';
|
||
import { BidCrawlerService } from './services/bid-crawler.service';
|
||
|
||
@Controller('api/crawler')
|
||
export class CrawlerController {
|
||
private isCrawling = false;
|
||
private crawlingSources = new Set<string>();
|
||
|
||
constructor(private readonly crawlerService: BidCrawlerService) {}
|
||
|
||
@Get('status')
|
||
getStatus() {
|
||
return {
|
||
isCrawling: this.isCrawling,
|
||
crawlingSources: Array.from(this.crawlingSources),
|
||
};
|
||
}
|
||
|
||
@Post('run')
|
||
async runCrawl() {
|
||
if (this.isCrawling) {
|
||
return { message: 'Crawl is already running' };
|
||
}
|
||
|
||
this.isCrawling = true;
|
||
|
||
// We don't await this because we want it to run in the background
|
||
// and return immediately, or we can await if we want to user to wait.
|
||
// Given the requirement "Immediate Crawl", usually implies triggering it.
|
||
// However, for a better UI experience, we might want to wait or just trigger.
|
||
// Let's await it so that user knows when it's done (or failed),
|
||
// assuming it doesn't take too long for the mock.
|
||
// Real crawling might take long, so background is better.
|
||
// For this prototype, I'll await it to show completion.
|
||
try {
|
||
await this.crawlerService.crawlAll();
|
||
return { message: 'Crawl completed successfully' };
|
||
} finally {
|
||
this.isCrawling = false;
|
||
}
|
||
}
|
||
|
||
@Post('crawl/:sourceName')
|
||
async crawlSingleSource(@Param('sourceName') sourceName: string) {
|
||
if (this.crawlingSources.has(sourceName)) {
|
||
return { message: `Source ${sourceName} is already being crawled` };
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
}
|