From e13c418389676726ecee80f744802a02f400542d Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 1 Feb 2024 13:09:50 +0800 Subject: [PATCH 1/3] remove the inner join with highlights table in the sql of update-label job --- packages/api/src/jobs/update_db.ts | 34 ++++++++++++++---------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/api/src/jobs/update_db.ts b/packages/api/src/jobs/update_db.ts index dcde84383..d178ad384 100644 --- a/packages/api/src/jobs/update_db.ts +++ b/packages/api/src/jobs/update_db.ts @@ -17,16 +17,15 @@ export const updateLabels = async (data: UpdateLabelsData) => { return authTrx( async (tx) => tx.query( - `WITH labels_agg AS ( - SELECT array_agg(DISTINCT l.name) AS names_agg - FROM omnivore.labels l - INNER JOIN omnivore.entity_labels el ON el.label_id = l.id - LEFT JOIN omnivore.highlight h ON h.id = el.highlight_id - WHERE el.library_item_id = $1 OR h.library_item_id = $1 - ) - UPDATE omnivore.library_item li - SET label_names = COALESCE((SELECT names_agg FROM labels_agg), ARRAY[]::TEXT[]) - WHERE li.id = $1`, + `UPDATE omnivore.library_item + SET label_names = COALESCE(( + SELECT array_agg(DISTINCT l.name) + FROM omnivore.labels l + INNER JOIN omnivore.entity_labels el + ON el.label_id = l.id + AND el.library_item_id = $1 + ), ARRAY[]::TEXT[]) + WHERE id = $1`, [data.libraryItemId] ), undefined, @@ -38,14 +37,13 @@ export const updateHighlight = async (data: UpdateHighlightData) => { return authTrx( async (tx) => tx.query( - `WITH highlight_agg AS ( - SELECT array_agg(COALESCE(annotation, '')) AS annotation_agg - FROM omnivore.highlight - WHERE library_item_id = $1 - ) - UPDATE omnivore.library_item - SET highlight_annotations = COALESCE((SELECT annotation_agg FROM highlight_agg), ARRAY[]::TEXT[]) - WHERE id = $1`, + `UPDATE omnivore.library_item + SET highlight_annotations = COALESCE(( + SELECT array_agg(COALESCE(annotation, '')) + FROM omnivore.highlight + WHERE library_item_id = $1 + ), ARRAY[]::TEXT[]) + WHERE id = $1`, [data.libraryItemId] ), undefined, From 19fe60d27a191ebe7164fbe1e3880550202238a2 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 1 Feb 2024 13:14:01 +0800 Subject: [PATCH 2/3] change the update-db job priority to 5 and use exponential backoff strategy for retrying --- packages/api/src/utils/createTask.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/api/src/utils/createTask.ts b/packages/api/src/utils/createTask.ts index 6a30a9ccd..b4edb4e0d 100644 --- a/packages/api/src/utils/createTask.ts +++ b/packages/api/src/utils/createTask.ts @@ -679,7 +679,12 @@ export const bulkEnqueueUpdateLabels = async (data: UpdateLabelsData[]) => { name: UPDATE_LABELS_JOB, data: d, opts: { - priority: 1, + attempts: 3, + priority: 5, + backoff: { + type: 'exponential', + delay: 1000, + }, }, })) @@ -699,7 +704,12 @@ export const enqueueUpdateHighlight = async (data: UpdateHighlightData) => { try { return queue.add(UPDATE_HIGHLIGHT_JOB, data, { - priority: 1, + attempts: 3, + priority: 5, + backoff: { + type: 'exponential', + delay: 1000, + }, }) } catch (error) { logger.error('error enqueuing update highlight job', error) From abaf726044c156cd0467446896005f3fd5069ac2 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Thu, 1 Feb 2024 13:22:06 +0800 Subject: [PATCH 3/3] revert the change of priority --- packages/api/src/utils/createTask.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api/src/utils/createTask.ts b/packages/api/src/utils/createTask.ts index b4edb4e0d..8b4d57d1a 100644 --- a/packages/api/src/utils/createTask.ts +++ b/packages/api/src/utils/createTask.ts @@ -680,7 +680,7 @@ export const bulkEnqueueUpdateLabels = async (data: UpdateLabelsData[]) => { data: d, opts: { attempts: 3, - priority: 5, + priority: 1, backoff: { type: 'exponential', delay: 1000, @@ -705,7 +705,7 @@ export const enqueueUpdateHighlight = async (data: UpdateHighlightData) => { try { return queue.add(UPDATE_HIGHLIGHT_JOB, data, { attempts: 3, - priority: 5, + priority: 1, backoff: { type: 'exponential', delay: 1000,