do not retry saving following items if item is saved

This commit is contained in:
Hongbo Wu
2024-01-16 12:15:21 +08:00
parent 816019e6a3
commit 5141c2a042
2 changed files with 42 additions and 11 deletions

View File

@ -1,5 +1,12 @@
import * as httpContext from 'express-http-context2'
import { EntityManager, EntityTarget, QueryBuilder, Repository } from 'typeorm'
import { DatabaseError } from 'pg'
import {
EntityManager,
EntityTarget,
QueryBuilder,
QueryFailedError,
Repository,
} from 'typeorm'
import { appDataSource } from '../data_source'
import { Claims } from '../resolvers/types'
import { SetClaimsRole } from '../utils/dictionary'
@ -106,3 +113,15 @@ export const valuesToRawSql = (
return sql
}
const isQueryFailedError = (
err: unknown
): err is QueryFailedError & DatabaseError => err instanceof QueryFailedError
export const isUniqueViolation = (err: unknown): boolean => {
if (isQueryFailedError(err)) {
return err.code === '23505'
}
return false
}

View File

@ -5,6 +5,7 @@ import {
PageType,
PreparedDocumentInput,
} from '../../generated/graphql'
import { isUniqueViolation } from '../../repository'
import { createAndSaveLabelsInLibraryItem } from '../../services/labels'
import { createLibraryItem } from '../../services/library_item'
import { parsedContentToLibraryItem } from '../../services/save_page'
@ -125,18 +126,29 @@ export function followingServiceRouter() {
state: ArticleSavingRequestStatus.ContentNotFetched,
})
const newItem = await createLibraryItem(itemToSave, userId)
logger.info('feed item saved in following')
try {
const newItem = await createLibraryItem(itemToSave, userId)
logger.info('feed item saved in following')
// save RSS label in the item
await createAndSaveLabelsInLibraryItem(
newItem.id,
userId,
[{ name: 'RSS' }],
feedUrl
)
// save RSS label in the item
await createAndSaveLabelsInLibraryItem(
newItem.id,
userId,
[{ name: 'RSS' }],
feedUrl
)
logger.info('RSS label added to the item')
logger.info('RSS label added to the item')
} catch (error) {
logger.error('error saving feed item', error)
if (isUniqueViolation(error)) {
logger.info('feed item already saved')
return res.sendStatus(200)
}
return res.sendStatus(500)
}
return res.sendStatus(200)
}