【问题标题】:Triggering Cloud Dataflow pipeline from Cloud Function - function times out从 Cloud Function 触发 Cloud Dataflow 管道 - 函数超时
【发布时间】:2018-08-29 00:06:03
【问题描述】:

我正在尝试从 Cloud Function 触发 Dataflow 管道,该管道本身是在 GCS 存储桶中上传新文件时触发的。 当我上传文件时,云功能被正确触发,但几秒钟后超时,没有触发任何数据流。 下面是我的功能代码:

const google = require('googleapis');
const projectId = "iot-fitness-198120";
 
exports.moveDataFromGCStoPubSub = function(event, callback) {
 const file = event.data;
 if (file.resourceState === 'exists' && file.name) {
   google.auth.getApplicationDefault(function (err, authClient, projectId) {
     if (err) {
       throw err;
     }
 
     if (authClient.createScopedRequired && authClient.createScopedRequired()) {
       authClient = authClient.createScoped([
         'https://www.googleapis.com/auth/cloud-platform',
         'https://www.googleapis.com/auth/userinfo.email'
       ]);
     }
     console.log("File exists and client function is authenticated");
     console.log(file);
     
 
     const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
     console.log(`Incoming data: ${file.name}`);
 
     dataflow.projects.templates.create({
       projectId: projectId,
       resource: {
         parameters: {
           inputFile: `gs://${file.bucket}/${file.name}`,
           outputTopic: `projects/iot-fitness-198120/topics/MemberFitnessData`
         },
         jobName: 'CStoPubSub',
         gcsPath: 'gs://dataflow-templates/latest/GCS_Text_to_Cloud_PubSub',
         staginglocation: 'gs://fitnessanalytics-tmp/tmp'
       }
     }, function(err, response) {
       if (err) {
         console.error("problem running dataflow template, error was: ", err);
       }
       console.log("Dataflow template response: ", response);
       callback();
     });
 
   });
 }
};

执行甚至没有记录以下行,console.log("File exists and client function is authenticated");这告诉我它甚至没有走那么远。

这是执行期间的日志输出:

2018-03-20 04:56:43.283 GST 数据流触发函数 52957909906492 函数执行耗时 60097 毫秒,状态为:'timeout'

2018-03-20 04:55:43.188 GST 数据流触发函数 52957909906492 函数执行开始

知道为什么它没有触发数据流但没有抛出错误消息吗?

【问题讨论】:

    标签: javascript node.js timeout google-cloud-functions google-cloud-dataflow


    【解决方案1】:

    我终于修改了代码。从 GCP 支持获得了一些帮助。下面是正确的语法:

    var {google} = require('googleapis');
    
    exports.moveDataFromGCStoPubSub = (event, callback) => {
    
    
    const file = event.data;
    const context = event.context;
    
    console.log(`Event ${context.eventId}`);
    console.log(`  Event Type: ${context.eventType}`);
    console.log(`  Bucket: ${file.bucket}`);
    console.log(`  File: ${file.name}`);
    console.log(`  Metageneration: ${file.metageneration}`);
    console.log(`  Created: ${file.timeCreated}`);
    console.log(`  Updated: ${file.updated}`);
    
      google.auth.getApplicationDefault(function (err, authClient, projectId) {
         if (err) {
           throw err;
         }
    
     console.log(projectId);
    
     const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
     console.log(`gs://${file.bucket}/${file.name}`);
     dataflow.projects.templates.create({
      gcsPath: 'gs://dataflow-templates/latest/GCS_Text_to_Cloud_PubSub', 
      projectId: projectId,
       resource: {
        parameters: {
            inputFilePattern: `gs://${file.bucket}/${file.name}`,
            outputTopic: 'projects/iot-fitness-198120/topics/MemberFitnessData2'
          },
        environment: {
          tempLocation: 'gs://fitnessanalytics-tmp/tmp'
        },
          jobName: 'CStoPubSub',
          //gcsPath: 'gs://dataflow-templates/latest/GCS_Text_to_Cloud_PubSub',    
        }
     }, function(err, response) {
       if (err) {
         console.error("problem running dataflow template, error was: ", err);
       }
       console.log("Dataflow template response: ", response);
       callback();
     });
    
       });
    
     callback();
    };
    

    【讨论】:

    • 我已经尝试过您的代码,但由于某种原因,我遇到了权限问题:运行数据流模板时出现问题,错误为:{ 错误:尝试检索访问令牌时返回了一个禁止错误Compute Engine 内置服务帐号。这可能是因为 Compute Engine 实例没有指定正确的权限范围。 (cbfa072458b51d1e):模板文件加载失败:权限被拒绝。请检查我赋予云功能和计算机器人帐户项目所有者的文件权限,但这仍然失败。有什么想法吗?
    【解决方案2】:

    猜你的云函数执行失败不满足你的 if 语句,

    if (file.resourceState === 'exists' && file.name)
    

    当我开始使用 Cloud Function 时,我遇到了类似的问题。按照解决方案here中提供的方式修改您的index.js文件var {google} = require('googleapis');

    【讨论】:

    • 谢谢维特里。它确实有很大帮助。我现在正在复制您的模板并根据我的需要进行调整。但是,它仍然没有通过。显然,这是因为我写错了完成工作所需的参数,即 stagingLocation 参数。知道如何在代码中编写它: staging-location 吗?暂存地点?暂存地点?在 CLI 中,它是 --staging-location。
    • 我假设您询问的是云函数 js 文件。它是 stagingLocation,看起来像 '--jobName=FromACloudFunction', '--project=analytics-project', '--runner=DataflowRunner', '--stagingLocation=gs://mybucket/staging'
    猜你喜欢
    • 2020-12-10
    • 2020-07-18
    • 2020-10-26
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多