云任务函数未触发-任何需要App Engine的资源都只能在App Engine区域创建/更新 [英] Cloud Task function not firing -Any resource that needs App Engine can only be created/updated in the App Engine region

本文介绍了云任务函数未触发-任何需要App Engine的资源都只能在App Engine区域创建/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的处理程序名为taskToFireOff已删除allUsersCloud Tasks Enqueuer主体的名称设置为ServiceAccountEmail,其角色设置为Invoker(未显示)

创建帖子时,我使用.onCreate创建任务。任务已创建,但http请求所连接的函数从不触发。我看了看这里https://console.cloud.google.com/cloudtasks/queue/,发现有两个任务(我尝试了两次)就坐在那里。

当我转到控制台中的云任务页面时&>单击任务>;任务&>Method / URL,它显示

POSThttps://us-east1-myProjectId.cloudfunctions.net/taskToFireOff

但当我转到任务&>日志&>查看&>建议&>查询详细信息时,它会显示

在onCreateData中看到新错误组:错误:3无效参数:ANY 需要App Engine的资源只能在App中创建/更新 引擎区。位置必须等于us-East1,因为应用程序

这是一个奇怪的错误,因为上面的url以us-east1开头,而在我的index.js文件中,我使用us-east1

设置了位置
exports.onCreateData = functions.database.ref('/posts/{postId}').onCreate(async (snapshot, context) => {

    const payload = {"postId": context.params.postId};

    const project = JSON.parse(process.env.FIREBASE_CONFIG!).projectId;
    const location = 'us-east1';
    const queue = 'ttl-task';
    const serviceAccountEmail = 'yaddayadda@myProjectId.iam.gserviceaccount.com';

    const queuePath = tasksClient.queuePath(project, location, queue);

    const url = `https://${location}-${project}.cloudfunctions.net/taskToFireOff`

    const task = {
        httpRequest: {
            httpMethod: 'POST',
            url,
            oidcToken: {
                serviceAccountEmail
            },
            body: Buffer.from(JSON.stringify(payload)).toString('base64'), // JSON.parse(JSON.stringify(payloadData));
            headers: {
                'Content-Type': 'application/json',
            },
        },
        scheduleTime: {
            seconds: // ...
        }
    }

    const [response] = await tasksClient.createTask({ parent: queuePath, task });

    // ...
});

exports.taskToFireOff = functions.https.onRequest((request, response) => {
    // ...
});

在控制台中,当我单击任务本身并查看有效负载时,所有正确的信息都在那里。

当我创建我的实时数据库时,我收到了us-central1

但当我创建我的存储桶时,我选择了us-east1。在FiRestore数据库的底部显示

当我输入$ gcloud app describe时,它显示LocationID为us-east1

会不会是云任务在us-east1而云函数在us-central1

运行$ gcloud functions list会列出us-central1区域中的所有云函数

顺便说一句,我创建了另一个项目,只是为了查看在创建存储存储桶时给我的选择。对于存储,没有办法选择以我们为中心1。有一个默认的Multi-region nam5(us-central),但us-central1没有特定的区域。我选择了us-east1,因为我在纽约。

推荐答案

问题是应用程序引擎在us-east1中,但函数处理程序taskToFireOffus-central1中。错误是说App Engine/Cloud任务关联的函数需要一起位于同一地域。简而言之,我需要更改该特定云函数的区域。

要解决这个问题,我必须创建一个新的处理程序,这非常简单。我按照answer的说明进入了link,特别是更改函数的一个或多个区域部分。

我运行的第一个终端:

$ gcloud app describe 
// use whatever appears for the locationId which for me was us-east1

Second-我将此新函数添加到index.js文件:

// whatever appeared for the locationId I put inside the region param

exports.newTaskToFireOff = functions.region('us-east1').https.onRequest((request, response) => {

    // ... in here I c+p all of the code from my initial taskToFireOff function
});

3-我在终端中运行此命令,以使用新函数更新gCloud

$ firebase deploy --only functions:newTaskToFireOff

4-我在终端中运行此命令,以便从gCloud中删除旧函数:

$ firebase functions: delete taskToFireOff

5-我进入index.js文件,手动删除了整个taskToFireOff。您不需要旧函数,只需要它内部的代码

// I deleted this old function
exports.taskToFireOff = functions.https.onRequest((request, response) => {

    // ... I took this code and c+p inside the newTaskToFireOff before I deleted this function
});

7-

当我转到控制台的Cloud Functions pagetaskToFireOff不再存在,而新TaskToFireOff存在区域us-east1

时 完成这些步骤后,不要忘记步骤2中的新函数,您必须进入云控制台(使用上面的链接)并更改其Permissions。将角色设置为Invoker,并将其主要角色名称设置为使用您为云任务ServiceAccountEmail设置的任何内容,否则它仍然无法工作。您也应该删除allUsers

这篇关于云任务函数未触发-任何需要App Engine的资源都只能在App Engine区域创建/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆