Skip to content

Commit

Permalink
Check request statuses explicitly to allow empty feed processing
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Jan 1, 2025
1 parent 906ca40 commit fd2543f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FeedRejectedDisabledCode,
Article,
UserFeedFormatOptions,
FeedResponseRequestStatus,
} from "../shared";
import { RabbitSubscribe, AmqpConnection } from "@golevelup/nestjs-rabbitmq";
import { MikroORM, UseRequestContext } from "@mikro-orm/core";
Expand Down Expand Up @@ -316,7 +317,11 @@ export class FeedEventHandlerService {
}
}

if (!response || !response.body) {
if (
!response ||
response.requestStatus === FeedResponseRequestStatus.Pending ||
response.requestStatus === FeedResponseRequestStatus.MatchedHash
) {
this.debugLog(
`Debug ${event.data.feed.id}: no response body. is pending request or` +
` matched hash`,
Expand All @@ -329,29 +334,35 @@ export class FeedEventHandlerService {
return;
}

const useResponseBody = response.body || "";
const useResponseBodyHash = response.bodyHash || "";

this.debugLog(
`Debug ${event.data.feed.id}: Parsing feed XML from ${url}`,
{},
event.debug
);

const { allArticles, articlesToDeliver: articles } =
await this.articlesService.getArticlesToDeliverFromXml(response.body, {
id: event.data.feed.id,
blockingComparisons,
passingComparisons,
formatOptions: {
dateFormat: event.data.feed.formatOptions?.dateFormat,
dateTimezone: event.data.feed.formatOptions?.dateTimezone,
disableImageLinkPreviews:
event.data.feed.formatOptions?.disableImageLinkPreviews,
dateLocale: event.data.feed.formatOptions?.dateLocale,
},
dateChecks: event.data.feed.dateChecks,
debug: event.debug,
useParserRules: getParserRules({ url: event.data.feed.url }),
externalFeedProperties: event.data.feed.externalProperties,
});
await this.articlesService.getArticlesToDeliverFromXml(
useResponseBody,
{
id: event.data.feed.id,
blockingComparisons,
passingComparisons,
formatOptions: {
dateFormat: event.data.feed.formatOptions?.dateFormat,
dateTimezone: event.data.feed.formatOptions?.dateTimezone,
disableImageLinkPreviews:
event.data.feed.formatOptions?.disableImageLinkPreviews,
dateLocale: event.data.feed.formatOptions?.dateLocale,
},
dateChecks: event.data.feed.dateChecks,
debug: event.debug,
useParserRules: getParserRules({ url: event.data.feed.url }),
externalFeedProperties: event.data.feed.externalProperties,
}
);

await this.updateFeedArticlesInCache({ event, articles: allArticles });

Expand Down Expand Up @@ -390,7 +401,7 @@ export class FeedEventHandlerService {

await this.responseHashService.set({
feedId: event.data.feed.id,
hash: response.bodyHash,
hash: useResponseBodyHash,
});

return [];
Expand Down Expand Up @@ -457,7 +468,7 @@ export class FeedEventHandlerService {

await this.responseHashService.set({
feedId: event.data.feed.id,
hash: response.bodyHash,
hash: useResponseBodyHash,
});

this.logEventFinish(event, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class ResponseHashService {

async set({ feedId, hash }: { hash: string; feedId: string }) {
try {
if (!hash) {
throw new Error(`Hash is required`);
}

await this.orm.em.upsert(ResponseHash, {
feed_id: feedId,
hash,
Expand All @@ -17,6 +21,7 @@ export class ResponseHashService {
} catch (err) {
logger.error(`Failed to set in cache storage`, {
err: (err as Error).stack,
feedId,
});
}
}
Expand Down

0 comments on commit fd2543f

Please sign in to comment.