我在 Firebase(第一代)中实现了一个每 5 分钟运行一次的计划函数。下面的代码片段是该函数的简化版本,仅显示有问题的部分:
initializeApp()
export const example = runWith({
timeoutSeconds: 20,
memory: '128MB',
maxInstances: 1,
minInstances: 0,
})
.region('europe-west1')
.pubsub.schedule('*/5 * * * *')
.onRun(async () => {
const date = new Date();
const weekday = date.getDay();
const hour = date.getHours();
const minute = Math.floor(date.getMinutes() / 15) * 15;
const topicName = `${weekday}-${hour}-${minute}`;
const messaging = getMessaging();
try {
await messaging.sendToTopic(
topicName,
{
data: {
mode: '15MinSchedule',
type: topicName,
},
},
{
priority: 'high',
},
);
debug(`Successfully sent to topic: ${topicName}`);
} catch (err) {
warn(`Error sending to topic ${topicName}:`, err);
}
});
当
sendToTopic
方法抛出以下错误时,该问题会间歇性地发生,大约每 2 到 3 小时发生一次(完整日志在本文末尾提供):
"error":"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1"
根据错误消息,该函数似乎正在尝试访问已弃用的 FCM 端点 (v1)。我不确定,但我认为 firebase-admin 包应该已经使用正确的、未弃用的端点,而不需要任何手动迁移,对吧? 然而,大多数时候一切都工作正常,正如我所说,它只是每 2 小时抛出一次错误,之后它会再次正确发送消息。
我已经尝试过的
套装版本:
"firebase-admin": "^12.3.1",
"firebase-functions": "^5.0.1",
错误日志:
{
"textPayload": "FirebaseMessagingError: An unknown server error was returned. Raw server response: \"{\"error\":\"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1\"}\"\n at FirebaseMessagingError.fromServerError (/workspace/node_modules/firebase-admin/lib/utils/error.js:269:16)\n at createFirebaseError (/workspace/node_modules/firebase-admin/lib/messaging/messaging-errors-internal.js:35:47)\n at /workspace/node_modules/firebase-admin/lib/messaging/messaging-api-request-internal.js:80:75\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async sendToTopic (/workspace/lib/scheduled/***.js:73:9)\n at async Promise.all (index 1)\n at async exports.default (/workspace/lib/scheduled/***.js:43:5)\n at async /workspace/lib/group-functions/pubsub-group.js:56:5 {\n errorInfo: {\n code: 'messaging/unknown-error',\n message: 'An unknown server error was returned. Raw server response: \"{\"error\":\"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1\"}\"'\n },\n codePrefix: 'messaging'\n}",
"insertId": "66cdb534000ecdfcc85c9c9e",
"resource": {
"type": "cloud_function",
"labels": {
"function_name": "pubsub-min5Schedule",
"region": "europe-west1",
"project_id": "***"
}
},
"timestamp": "2024-08-27T11:15:00.970236Z",
"severity": "WARNING",
"labels": {
"runtime_version": "nodejs20_20240818_20_16_0_RC00",
"execution_id": "***",
"instance_id": "***"
},
"logName": "projects/***/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/***/traces/cee5cfbbaf474cae746709dafadd9785",
"receiveTimestamp": "2024-08-27T11:15:00.995052585Z"
}
我自己找到了解决方案:
截至 2024 年 8 月 22 日,sendToTopic
和
sendToCondition
方法已被弃用,我们现在需要对这两种方法使用
send
。我已经运行了一天,一切都很顺利,没有任何问题。