Add id to update event

This commit is contained in:
mrjvs
2023-09-08 00:01:02 +02:00
parent 823d941962
commit ae653c8d27
3 changed files with 11 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
export type UpdateEventStatus = 'success' | 'failure' | 'notfound' | 'pending';
export type UpdateEvent = {
id: string; // id presented in start event
percentage: number;
status: UpdateEventStatus;
error?: unknown; // set when status is failure
@@ -21,13 +22,6 @@ export type DiscoverEmbedsEvent = {
}>;
};
export type StartScrapingEvent = {
sourceId: string;
// embed Id (not embedScraperId)
embedId?: string;
};
export type SingleScraperEvents = {
update?: (evt: UpdateEvent) => void;
};

View File

@@ -27,6 +27,7 @@ export async function scrapeInvidualSource(
proxiedFetcher: ops.proxiedFetcher,
progress(val) {
ops.events?.update?.({
id: sourceScraper.id,
percentage: val,
status: 'pending',
});
@@ -70,6 +71,7 @@ export async function scrapeIndividualEmbed(
url: ops.url,
progress(val) {
ops.events?.update?.({
id: embedScraper.id,
percentage: val,
status: 'pending',
});

View File

@@ -42,12 +42,14 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
});
const embeds = reorderOnIdList(ops.embedOrder ?? [], list.embeds);
const embedIds = embeds.map((v) => v.id);
let lastId = '';
const contextBase: ScrapeContext = {
fetcher: ops.fetcher,
proxiedFetcher: ops.proxiedFetcher,
progress(val) {
ops.events?.update?.({
id: lastId,
percentage: val,
status: 'pending',
});
@@ -60,6 +62,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
for (const s of sources) {
ops.events?.start?.(s.id);
lastId = s.id;
// run source scrapers
let output: SourcererOutput | null = null;
@@ -77,6 +80,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
} catch (err) {
if (err instanceof NotFoundError) {
ops.events?.update?.({
id: s.id,
percentage: 100,
status: 'notfound',
reason: err.message,
@@ -84,6 +88,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
continue;
}
ops.events?.update?.({
id: s.id,
percentage: 100,
status: 'failure',
error: err,
@@ -123,6 +128,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
// run embed scraper
const id = [s.id, ind].join('-');
ops.events?.start?.(id);
lastId = id;
let embedOutput: EmbedOutput;
try {
embedOutput = await scraper.scrape({
@@ -132,6 +138,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
} catch (err) {
if (err instanceof NotFoundError) {
ops.events?.update?.({
id,
percentage: 100,
status: 'notfound',
reason: err.message,
@@ -139,6 +146,7 @@ export async function runAllProviders(list: ProviderList, ops: ProviderRunnerOpt
continue;
}
ops.events?.update?.({
id,
percentage: 100,
status: 'failure',
error: err,