create a job for db trigger

This commit is contained in:
Hongbo Wu 2024-01-30 23:53:05 +08:00
parent 814a2bf519
commit ddddf8234e
4 changed files with 62 additions and 2 deletions

View file

@ -0,0 +1,31 @@
import { authTrx } from '../repository'
export const UPDATE_LABELS_IN_LIBRARY_ITEM_JOB = 'update-labels-in-library-item'
export interface UpdateLabelsInLibraryItemData {
libraryItemId: string
userId: string
}
export const updateLabelsInLibraryItem = async (
data: UpdateLabelsInLibraryItemData
) => {
return authTrx(
async (tx) =>
tx.query(
`WITH labels_agg AS (
SELECT array_agg(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;`,
[data.libraryItemId]
),
undefined,
data.userId
)
}

View file

@ -2,7 +2,7 @@
/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/no-misused-promises */
import { Job, Queue, QueueEvents, Worker, JobType } from 'bullmq'
import { Job, JobType, Queue, QueueEvents, Worker } from 'bullmq'
import express, { Express } from 'express'
import { SnakeNamingStrategy } from 'typeorm-naming-strategies'
import { appDataSource } from './data_source'
@ -11,10 +11,14 @@ import { findThumbnail, THUMBNAIL_JOB } from './jobs/find_thumbnail'
import { refreshAllFeeds } from './jobs/rss/refreshAllFeeds'
import { refreshFeed } from './jobs/rss/refreshFeed'
import { savePageJob } from './jobs/save_page'
import { triggerRule, TRIGGER_RULE_JOB_NAME } from './jobs/trigger_rule'
import {
updateLabelsInLibraryItem,
UPDATE_LABELS_IN_LIBRARY_ITEM_JOB,
} from './jobs/update_db'
import { updatePDFContentJob } from './jobs/update_pdf_content'
import { redisDataSource } from './redis_data_source'
import { CustomTypeOrmLogger } from './utils/logger'
import { triggerRule, TRIGGER_RULE_JOB_NAME } from './jobs/trigger_rule'
export const QUEUE_NAME = 'omnivore-backend-queue'
@ -125,6 +129,8 @@ const main = async () => {
return findThumbnail(job.data)
case TRIGGER_RULE_JOB_NAME:
return triggerRule(job.data)
case UPDATE_LABELS_IN_LIBRARY_ITEM_JOB:
return updateLabelsInLibraryItem(job.data)
}
},
{

View file

@ -5,6 +5,8 @@ import { Label } from '../entity/label'
import { createPubSubClient, EntityType, PubsubClient } from '../pubsub'
import { authTrx } from '../repository'
import { CreateLabelInput, labelRepository } from '../repository/label'
import { enqueueUpdateLabelsInLibraryItem } from '../utils/createTask'
import { logger } from '../utils/logger'
type AddLabelsToLibraryItemEvent = {
pageId: string
@ -120,6 +122,10 @@ export const saveLabelsInLibraryItem = async (
userId
)
}
// update labels in library item
const job = await enqueueUpdateLabelsInLibraryItem({ libraryItemId, userId })
logger.info('update labels in library item job created', job)
}
export const addLabelsToLibraryItem = async (

View file

@ -17,6 +17,10 @@ import {
import { THUMBNAIL_JOB } from '../jobs/find_thumbnail'
import { queueRSSRefreshFeedJob } from '../jobs/rss/refreshAllFeeds'
import { TriggerRuleJobData, TRIGGER_RULE_JOB_NAME } from '../jobs/trigger_rule'
import {
UpdateLabelsInLibraryItemData,
UPDATE_LABELS_IN_LIBRARY_ITEM_JOB,
} from '../jobs/update_db'
import { getBackendQueue } from '../queue-processor'
import { redisDataSource } from '../redis_data_source'
import { signFeatureToken } from '../services/features'
@ -660,4 +664,17 @@ export const enqueueTriggerRuleJob = async (data: TriggerRuleJobData) => {
})
}
export const enqueueUpdateLabelsInLibraryItem = async (
data: UpdateLabelsInLibraryItemData
) => {
const queue = await getBackendQueue()
if (!queue) {
return undefined
}
return queue.add(UPDATE_LABELS_IN_LIBRARY_ITEM_JOB, data, {
priority: 1,
})
}
export default createHttpTaskWithToken