create a job for finding thumbnail

This commit is contained in:
Hongbo Wu
2024-01-25 11:18:18 +08:00
parent f3c1b9d9a0
commit e54c691c09
5 changed files with 193 additions and 45 deletions

View File

@ -22,6 +22,8 @@ import { CreateTaskError } from './errors'
import { stringToHash } from './helpers'
import { logger } from './logger'
import View = google.cloud.tasks.v2.Task.View
import { getBackendQueue } from '../queue-processor'
import { THUMBNAIL_JOB } from '../jobs/find_thumbnail'
// Instantiates a client.
const client = new CloudTasksClient()
@ -579,50 +581,19 @@ export const enqueueExportToIntegration = async (
export const enqueueThumbnailTask = async (
userId: string,
slug: string
): Promise<string> => {
const { GOOGLE_CLOUD_PROJECT } = process.env
libraryItemId: string
) => {
const queue = await getBackendQueue()
if (!queue) {
return undefined
}
const payload = {
userId,
slug,
libraryItemId,
}
const headers = {
Cookie: `auth=${generateVerificationToken({ id: userId })}`,
}
// If there is no Google Cloud Project Id exposed, it means that we are in local environment
if (env.dev.isLocal || !GOOGLE_CLOUD_PROJECT) {
if (env.queue.thumbnailTaskHandlerUrl) {
// Calling the handler function directly.
setTimeout(() => {
axios
.post(env.queue.thumbnailTaskHandlerUrl, payload, {
headers,
})
.catch((error) => {
logError(error)
})
}, 0)
}
return ''
}
const createdTasks = await createHttpTaskWithToken({
payload,
taskHandlerUrl: env.queue.thumbnailTaskHandlerUrl,
requestHeaders: headers,
queue: 'omnivore-thumbnail-queue',
return queue.add(THUMBNAIL_JOB, payload, {
priority: 100,
})
if (!createdTasks || !createdTasks[0].name) {
logger.error(`Unable to get the name of the task`, {
payload,
createdTasks,
})
throw new CreateTaskError(`Unable to get the name of the task`)
}
return createdTasks[0].name
}
export interface RssSubscriptionGroup {