feat: start a cloud task to set thumbnail and pre-cache all the images in the content

This commit is contained in:
Hongbo Wu
2023-06-06 11:56:31 +08:00
parent e2315b5662
commit 0c3fa2cdd4
4 changed files with 35 additions and 11 deletions

View File

@ -4,7 +4,7 @@ import { PubsubClient } from '../datalayer/pubsub'
import { addHighlightToPage } from '../elastic/highlights'
import { createPage, getPageByParam, updatePage } from '../elastic/pages'
import { ArticleSavingRequestStatus, Page, PageType } from '../elastic/types'
import { homePageURL } from '../env'
import { env, homePageURL } from '../env'
import {
HighlightType,
Maybe,
@ -14,6 +14,8 @@ import {
SaveResult,
} from '../generated/graphql'
import { DataModels } from '../resolvers/types'
import { generateVerificationToken } from '../utils/auth'
import createHttpTaskWithToken from '../utils/createTask'
import {
generateSlug,
stringToHash,
@ -164,7 +166,21 @@ export const savePage = async (
pageId = newPageId
}
// TODO: update thumbnail and pre-cache images
// create a task to update thumbnail and pre-cache all images
try {
await createHttpTaskWithToken({
payload: {
userId: saver.userId,
slug,
},
taskHandlerUrl: env.queue.thumbnailTaskHandlerUrl,
requestHeaders: {
Authorization: generateVerificationToken(saver.userId),
},
})
} catch (e) {
console.log('Failed to create thumbnail task', e)
}
if (parseResult.highlightData) {
const highlight = {

View File

@ -66,6 +66,7 @@ interface BackendEnv {
integrationTaskHandlerUrl: string
textToSpeechTaskHandlerUrl: string
recommendationTaskHandlerUrl: string
thumbnailTaskHandlerUrl: string
}
fileUpload: {
gcsUploadBucket: string
@ -159,6 +160,7 @@ const nullableEnvVars = [
'GCP_LOCATION',
'RECOMMENDATION_TASK_HANDLER_URL',
'POCKET_CONSUMER_KEY',
'THUMBNAIL_TASK_HANDLER_URL',
] // Allow some vars to be null/empty
/* If not in GAE and Prod/QA/Demo env (f.e. on localhost/dev env), allow following env vars to be null */
@ -245,6 +247,7 @@ export function getEnv(): BackendEnv {
integrationTaskHandlerUrl: parse('INTEGRATION_TASK_HANDLER_URL'),
textToSpeechTaskHandlerUrl: parse('TEXT_TO_SPEECH_TASK_HANDLER_URL'),
recommendationTaskHandlerUrl: parse('RECOMMENDATION_TASK_HANDLER_URL'),
thumbnailTaskHandlerUrl: parse('THUMBNAIL_TASK_HANDLER_URL'),
}
const imageProxy = {
url: parse('IMAGE_PROXY_URL'),

View File

@ -22,7 +22,7 @@ const logger = buildLogger('app.dispatch')
const client = new CloudTasksClient()
const createHttpTaskWithToken = async ({
project,
project = process.env.GOOGLE_CLOUD_PROJECT,
queue = env.queue.name,
location = env.queue.location,
taskHandlerUrl = env.queue.contentFetchUrl,
@ -32,7 +32,7 @@ const createHttpTaskWithToken = async ({
scheduleTime,
requestHeaders,
}: {
project: string
project?: string
queue?: string
location?: string
taskHandlerUrl?: string
@ -42,12 +42,18 @@ const createHttpTaskWithToken = async ({
scheduleTime?: number
requestHeaders?: Record<string, string>
}): Promise<
[
protos.google.cloud.tasks.v2.ITask,
protos.google.cloud.tasks.v2.ICreateTaskRequest | undefined,
unknown | undefined
]
| [
protos.google.cloud.tasks.v2.ITask,
protos.google.cloud.tasks.v2.ICreateTaskRequest | undefined,
unknown | undefined
]
| null
> => {
// If there is no Google Cloud Project Id exposed, it means that we are in local environment
if (env.dev.isLocal || !project) {
return null
}
// Construct the fully qualified queue name.
priority === 'low' && (queue = `${queue}-low`)
@ -458,7 +464,6 @@ export const enqueueRecommendation = async (
}
export const enqueueImportFromIntegration = async (
userId: string,
integrationId: string,
authToken: string
): Promise<string> => {

View File

@ -83,7 +83,7 @@ export function authorized<
return (parent, args, ctx, info) => {
const { claims } = ctx
if (claims?.uid) {
return resolver(parent, args, { ...ctx, claims }, info)
return resolver(parent, args, { ...ctx, claims, uid: claims.uid }, info)
}
return { errorCodes: ['UNAUTHORIZED'] } as TError
}