throw error if export-item job fails

This commit is contained in:
Hongbo Wu 2024-03-01 15:00:28 +08:00
parent a337e0e39d
commit e33cf465fe
2 changed files with 45 additions and 69 deletions

View file

@ -35,47 +35,41 @@ export const exportItem = async (jobData: ExportItemJobData) => {
return
}
await Promise.all(
integrations.map(async (integration) => {
const logObject = {
userId,
integrationId: integration.id,
}
logger.info('exporting item...', logObject)
// currently only readwise integration is supported
const integration = integrations[0]
try {
const client = getIntegrationClient(integration.name)
const logObject = {
userId,
integrationId: integration.id,
}
logger.info('exporting item...', logObject)
const synced = await client.export(integration.token, libraryItems)
if (!synced) {
logger.error('failed to export item', logObject)
return Promise.resolve(false)
}
const client = getIntegrationClient(integration.name)
const syncedAt = new Date()
logger.info('updating integration...', {
...logObject,
syncedAt,
})
const synced = await client.export(integration.token, libraryItems)
if (!synced) {
logger.error('failed to export item', logObject)
return false
}
// update integration syncedAt if successful
const updated = await updateIntegration(
integration.id,
{
syncedAt,
},
userId
)
logger.info('integration updated', {
...logObject,
updated,
})
const syncedAt = new Date()
logger.info('updating integration...', {
...logObject,
syncedAt,
})
return Promise.resolve(true)
} catch (err) {
logger.error('export with integration failed', err)
return Promise.resolve(false)
}
})
// update integration syncedAt if successful
const updated = await updateIntegration(
integration.id,
{
syncedAt,
},
userId
)
logger.info('integration updated', {
...logObject,
updated,
})
return true
}

View file

@ -1,6 +1,6 @@
import axios from 'axios'
import { LibraryItem } from '../../entity/library_item'
import { highlightUrl, wait } from '../../utils/helpers'
import { highlightUrl } from '../../utils/helpers'
import { logger } from '../../utils/logger'
import { IntegrationClient } from './integration'
@ -95,40 +95,22 @@ export class ReadwiseClient implements IntegrationClient {
syncWithReadwise = async (
token: string,
highlights: ReadwiseHighlight[],
retryCount = 0
highlights: ReadwiseHighlight[]
): Promise<boolean> => {
const url = `${this.apiUrl}/highlights`
try {
const response = await axios.post(
url,
{
highlights,
const response = await axios.post(
url,
{
highlights,
},
{
headers: {
Authorization: `Token ${token}`,
'Content-Type': 'application/json',
},
{
headers: {
Authorization: `Token ${token}`,
'Content-Type': 'application/json',
},
timeout: 5000, // 5 seconds
}
)
return response.status === 200
} catch (error) {
console.error(error)
if (axios.isAxiosError(error)) {
if (error.response?.status === 429 && retryCount < 3) {
console.log('Readwise API rate limit exceeded, retrying...')
// wait for Retry-After seconds in the header if rate limited
// max retry count is 3
const retryAfter = error.response?.headers['retry-after'] || '10' // default to 10 seconds
await wait(parseInt(retryAfter, 10) * 1000)
return this.syncWithReadwise(token, highlights, retryCount + 1)
}
timeout: 5000, // 5 seconds
}
return false
}
)
return response.status === 200
}
}