Merge pull request #3563 from omnivore-app/fix/export-job

export at most 1000 most recent items
This commit is contained in:
Hongbo Wu
2024-02-23 11:14:16 +08:00
committed by GitHub
3 changed files with 47 additions and 22 deletions

View File

@ -1,6 +1,6 @@
import { IntegrationType } from '../../entity/integration'
import { findIntegration } from '../../services/integrations'
import { searchLibraryItems } from '../../services/library_item'
import { findRecentLibraryItems } from '../../services/library_item'
import { findActiveUser } from '../../services/user'
import { enqueueExportItem } from '../../utils/createTask'
import { logger } from '../../utils/logger'
@ -39,31 +39,37 @@ export const exportAllItems = async (jobData: ExportAllItemsJobData) => {
return
}
// get paginated items from the database
const first = 50
let after = 0
for (;;) {
console.log('searching for items...', {
userId,
first,
after,
})
const searchResult = await searchLibraryItems(
{ from: after, size: first },
userId
)
const libraryItems = searchResult.libraryItems
const size = libraryItems.length
if (size === 0) {
break
const maxItems = 1000
const limit = 100
let offset = 0
// get max 1000 most recent items from the database
while (offset < maxItems) {
const libraryItems = await findRecentLibraryItems(userId, limit, offset)
if (libraryItems.length === 0) {
logger.info('no library items found', {
userId,
})
return
}
logger.info('enqueuing export item...', {
userId,
offset,
integrationId,
})
await enqueueExportItem({
userId,
libraryItemIds: libraryItems.map((item) => item.id),
integrationId,
})
after += size
offset += libraryItems.length
logger.info('exported items', {
userId,
offset,
integrationId,
})
}
}

View File

@ -21,7 +21,6 @@ export const exportItem = async (jobData: ExportItemJobData) => {
if (libraryItems.length === 0) {
logger.error('library items not found', {
userId,
libraryItemIds,
})
return
}
@ -40,7 +39,6 @@ export const exportItem = async (jobData: ExportItemJobData) => {
integrations.map(async (integration) => {
const logObject = {
userId,
libraryItemIds,
integrationId: integration.id,
}
logger.info('exporting item...', logObject)

View File

@ -652,6 +652,28 @@ export const searchLibraryItems = async (
)
}
export const findRecentLibraryItems = async (
userId: string,
limit = 1000,
offset?: number
) => {
return authTrx(
async (tx) =>
tx
.createQueryBuilder(LibraryItem, 'library_item')
.where('library_item.user_id = :userId', { userId })
.andWhere('library_item.state = :state', {
state: LibraryItemState.Succeeded,
})
.orderBy('library_item.saved_at', 'DESC', 'NULLS LAST')
.take(limit)
.skip(offset)
.getMany(),
undefined,
userId
)
}
export const findLibraryItemsByIds = async (ids: string[], userId: string) => {
return authTrx(
async (tx) =>
@ -659,7 +681,6 @@ export const findLibraryItemsByIds = async (ids: string[], userId: string) => {
.createQueryBuilder(LibraryItem, 'library_item')
.leftJoinAndSelect('library_item.labels', 'labels')
.leftJoinAndSelect('library_item.highlights', 'highlights')
.leftJoinAndSelect('highlights.user', 'user')
.where('library_item.id IN (:...ids)', { ids })
.getMany(),
undefined,