From ae653c8d278962dbfb2fe80ef28de79e689c7b3b Mon Sep 17 00:00:00 2001 From: mrjvs Date: Fri, 8 Sep 2023 00:01:02 +0200 Subject: [PATCH] Add id to update event --- src/main/events.ts | 8 +------- src/main/individualRunner.ts | 2 ++ src/main/runner.ts | 8 ++++++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/events.ts b/src/main/events.ts index bd4acce..50a8c2a 100644 --- a/src/main/events.ts +++ b/src/main/events.ts @@ -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; }; diff --git a/src/main/individualRunner.ts b/src/main/individualRunner.ts index 7fa1e0c..2dc63fa 100644 --- a/src/main/individualRunner.ts +++ b/src/main/individualRunner.ts @@ -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', }); diff --git a/src/main/runner.ts b/src/main/runner.ts index 09a6d2f..a438b01 100644 --- a/src/main/runner.ts +++ b/src/main/runner.ts @@ -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,