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 UpdateEventStatus = 'success' | 'failure' | 'notfound' | 'pending';
export type UpdateEvent = { export type UpdateEvent = {
id: string; // id presented in start event
percentage: number; percentage: number;
status: UpdateEventStatus; status: UpdateEventStatus;
error?: unknown; // set when status is failure 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 = { export type SingleScraperEvents = {
update?: (evt: UpdateEvent) => void; update?: (evt: UpdateEvent) => void;
}; };

View File

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

View File

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