javascript - 从 Cloud Function 触发 Cloud Dataflow 管道 - 函数超时

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

我正在尝试从 Cloud Functions 触发数据流管道,它本身是在 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 消费税 数据流触发函数 52957909906492 函数执行耗时 60097 毫秒,完成状态:“超时”

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

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

最佳答案

我终于修改了代码。从 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();
};

关于javascript - 从 Cloud Function 触发 Cloud Dataflow 管道 - 函数超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49379756/

相关文章:

javascript - Google map 地理定位 API 非移动设备?

javascript - 输入类型=附加到 onclick 事件的文件

javascript - Nodejs 确保 API 登录仅发生一次

javascript - 从 Javascript 调用 Typescript 类时的语法

mysql - 如何用C代码减少MySQL连接超时

javascript - JavaScript 中带变量的倒计时

javascript - 使用 javascript for 循环执行 <script ...></script> 多次

javascript - 提交按钮无法正常工作

javascript - 如何将总纳秒转换为 HH :MM:SS:ms:ns in JavaScript and node? 格式的字符串

r - 如何在 R 中破解 lpSolveAPI?