Skip to content

Commit 0b2cf59

Browse files
committed
Only disable invalid feeds after 1 day of failed attempts
1 parent 1c81984 commit 0b2cf59

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Migration } from '@mikro-orm/migrations';
2+
3+
export class Migration20250209001416 extends Migration {
4+
5+
async up(): Promise<void> {
6+
this.addSql('alter table "feed_retry_record" add column "created_at" timestamptz(0) null;');
7+
}
8+
9+
async down(): Promise<void> {
10+
this.addSql('alter table "feed_retry_record" drop column "created_at";');
11+
}
12+
13+
}

services/user-feeds/src/feed-event-handler/entities/feed-retry-record.entity.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export class FeedRetryRecord {
2020
@Property()
2121
attempts_so_far: number;
2222

23+
@Property({
24+
nullable: true,
25+
type: "timestamp with time zone",
26+
})
27+
created_at: Date = new Date();
28+
2329
constructor(data: Omit<FeedRetryRecord, "id">) {
2430
this.feed_id = data.feed_id;
2531
this.attempts_so_far = data.attempts_so_far;

services/user-feeds/src/feed-event-handler/feed-event-handler.service.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import { EntityRepository } from "@mikro-orm/postgresql";
4141
import { z } from "zod";
4242
import { CacheStorageService } from "../cache-storage/cache-storage.service";
4343
import { PartitionedFeedArticleFieldStoreService } from "../articles/partitioned-feed-article-field-store.service";
44+
import dayjs from "dayjs";
4445

4546
@Injectable()
4647
export class FeedEventHandlerService {
@@ -366,8 +367,6 @@ export class FeedEventHandlerService {
366367

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

369-
// START TEMPORARY - Should revisit this for a more robust retry strategy
370-
371370
const foundRetryRecord = await this.feedRetryRecordRepo.findOne(
372371
{
373372
feed_id: event.data.feed.id,
@@ -389,8 +388,6 @@ export class FeedEventHandlerService {
389388
});
390389
}
391390

392-
// END TEMPORARY
393-
394391
if (!articles.length) {
395392
this.debugLog(
396393
`Debug ${event.data.feed.id}: Ignoring feed event due to no` +
@@ -484,17 +481,31 @@ export class FeedEventHandlerService {
484481
stack: (err as Error).stack,
485482
});
486483

487-
const retryRecord = await this.feedRetryRecordRepo.findOne(
488-
{
489-
feed_id: event.data.feed.id,
490-
},
491-
{
492-
fields: ["id", "attempts_so_far"],
493-
}
494-
);
484+
const retryRecord = await this.feedRetryRecordRepo.findOne({
485+
feed_id: event.data.feed.id,
486+
});
495487

496-
// Rudimentary retry to alleviate some pressure
497-
if (retryRecord?.attempts_so_far && retryRecord.attempts_so_far >= 8) {
488+
const createdAt = retryRecord?.created_at;
489+
let disableFeed = false;
490+
491+
if (createdAt) {
492+
const cutoffTime = dayjs(createdAt).add(1, "day");
493+
494+
if (dayjs().isAfter(cutoffTime)) {
495+
logger.info(
496+
`Feed ${event.data.feed.id} exceeded retry cutoff date for invalid feed` +
497+
`, sending disable event`,
498+
{
499+
xml: err.feedText,
500+
}
501+
);
502+
503+
disableFeed = true;
504+
}
505+
} else if (
506+
retryRecord?.attempts_so_far &&
507+
retryRecord.attempts_so_far >= 8
508+
) {
498509
logger.info(
499510
`Feed ${event.data.feed.id} exceeded retry limit for invalid feed` +
500511
`, sending disable event`,
@@ -503,6 +514,11 @@ export class FeedEventHandlerService {
503514
}
504515
);
505516

517+
disableFeed = true;
518+
}
519+
520+
// Rudimentary retry to alleviate some pressure
521+
if (disableFeed && retryRecord) {
506522
this.amqpConnection.publish(
507523
"",
508524
MessageBrokerQueue.FeedRejectedDisableFeed,
@@ -532,6 +548,7 @@ export class FeedEventHandlerService {
532548
await this.feedRetryRecordRepo.upsert({
533549
feed_id: event.data.feed.id,
534550
attempts_so_far: (retryRecord?.attempts_so_far || 0) + 1,
551+
created_at: retryRecord?.created_at || new Date(),
535552
});
536553
}
537554

0 commit comments

Comments
 (0)