Firebase任务将函数作为队列负载传递



我正在尝试在firebase中创建一个通用任务队列。

以下文档我应该创建一个新的队列为每个函数我需要,但我想创建一个通用队列,将接受任何函数。

我试过创建这样的东西

export async function enqueue<T>(
task: any, // this will be the function that will be executed in the queue
payload: T // this is all params for this function
): Promise<any> {
if ( isDev() ) {
// in dev mode this works fine
return await task( payload )
}
const FUNCTIONS_REGION = process.env.FUNCTIONS_REGION
const queue = functions.taskQueue(
`locations/${ FUNCTIONS_REGION }/functions/queue`
)
return await Promise.all( [ queue.enqueue( {
task,
payload
}, {
dispatchDeadlineSeconds: 60 * 5 // 5 minutes
} ) ] )
}
.......
exports.queue = functions
.region( FUNCTIONS_REGION! )
.tasks.taskQueue( queuesConfig )
.onDispatch( async ( data: QueueType ) => {
const {
task,
payload
} = data
// here's my problem, in GCP console, the task is showing as undefined
console.log( 'task', task )
// but the payload is ok, everything is there
console.log( 'payload', payload )
await task( payload )
} )

我如何将函数作为数据传递给我的队列?

函数队列的调用看起来是正确的:

import { getFunctions  } from 'firebase-admin/functions'
function EnqueueIt() {
const queuePath = `locations/{location}/functions/{functionName}`;
const queue = getFunctions().taskQueue(queuePath);
queue.enqueue({ task, payload });
}

从描述来看,task是一个JavaScript函数,在这种情况下,它是不可序列化的,不能传输到队列。这解释了为什么只有payload作为一个对象成功地出现在队列中。

好的,我已经找出了错误是什么…当firebase排队任何任务时,它会向云任务API发出http请求,这不支持函数作为参数,因为所有数据都是json,所以我的"任务"变量总是未定义的。这在局部有效,因为JS接受函数作为参数。

我结束了使用一个函数对象,'taks'是一个对象键

最新更新