Use cloud task function from backend code instead of from rss-handler
This commit is contained in:
@ -6,7 +6,8 @@ import { parseHTML } from 'linkedom'
|
||||
import Parser, { Item } from 'rss-parser'
|
||||
import { promisify } from 'util'
|
||||
import { RedisClientType } from 'redis'
|
||||
import { CONTENT_FETCH_URL, createCloudTask } from './task'
|
||||
import createHttpTaskWithToken from '../../utils/createTask'
|
||||
import { env } from '../../env'
|
||||
|
||||
type FolderType = 'following' | 'inbox'
|
||||
|
||||
@ -306,7 +307,7 @@ const fetchContentAndCreateItem = async (
|
||||
item: RssFeedItem,
|
||||
folder: string
|
||||
) => {
|
||||
const input = {
|
||||
const payload = {
|
||||
userId,
|
||||
source: 'rss-feeder',
|
||||
url: item.link,
|
||||
@ -319,9 +320,16 @@ const fetchContentAndCreateItem = async (
|
||||
}
|
||||
|
||||
try {
|
||||
console.log('Creating task', input.url)
|
||||
// save page
|
||||
const task = await createCloudTask(CONTENT_FETCH_URL, input)
|
||||
console.log('Creating task', {
|
||||
url: env.queue.contentFetchGCFUrl,
|
||||
fetch: payload.url,
|
||||
})
|
||||
|
||||
const task = await createHttpTaskWithToken({
|
||||
queue: 'omnivore-rss-feed-queue',
|
||||
taskHandlerUrl: env.queue.contentFetchGCFUrl,
|
||||
payload,
|
||||
})
|
||||
console.log('Created task', task)
|
||||
|
||||
return !!task
|
||||
|
||||
@ -1,48 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
||||
import { CloudTasksClient, protos } from '@google-cloud/tasks'
|
||||
|
||||
const cloudTask = new CloudTasksClient()
|
||||
|
||||
export const CONTENT_FETCH_URL = process.env.CONTENT_FETCH_GCF_URL
|
||||
|
||||
export const createCloudTask = async (
|
||||
taskHandlerUrl: string | undefined,
|
||||
payload: unknown,
|
||||
requestHeaders?: Record<string, string>,
|
||||
queue = 'omnivore-rss-feed-queue'
|
||||
) => {
|
||||
const location = process.env.GCP_LOCATION
|
||||
const project = process.env.GCP_PROJECT_ID
|
||||
|
||||
if (!project || !location || !queue || !taskHandlerUrl) {
|
||||
throw `Environment not configured: ${project}, ${location}, ${queue}, ${taskHandlerUrl}`
|
||||
}
|
||||
|
||||
const serviceAccountEmail = `${project}@appspot.gserviceaccount.com`
|
||||
|
||||
const parent = cloudTask.queuePath(project, location, queue)
|
||||
const convertedPayload = JSON.stringify(payload)
|
||||
const body = Buffer.from(convertedPayload).toString('base64')
|
||||
const task: protos.google.cloud.tasks.v2.ITask = {
|
||||
httpRequest: {
|
||||
httpMethod: 'POST',
|
||||
url: taskHandlerUrl,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...requestHeaders,
|
||||
},
|
||||
body,
|
||||
...(serviceAccountEmail
|
||||
? {
|
||||
oidcToken: {
|
||||
serviceAccountEmail,
|
||||
},
|
||||
}
|
||||
: null),
|
||||
},
|
||||
}
|
||||
|
||||
return cloudTask.createTask({ parent, task }).then((result) => {
|
||||
return result[0].name ?? undefined
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user