简体   繁体   English

使用 pubsub 推送触发器运行云 function

[英]Running a cloud function with a pubsub push trigger

I've setup a Python script that will take certain bigquery tables from one dataset, clean them with a SQL query, and add the cleaned tables to a new dataset.我已经设置了一个 Python 脚本,该脚本将从一个数据集中获取某些 bigquery 表,使用 SQL 查询清理它们,并将清理后的表添加到新数据集。 This script works correctly.该脚本工作正常。 I want to set this up as a cloud function that triggers at midnight every day.我想将其设置为每天午夜触发的云 function。

I've also used cloud scheduler to send a message to a pubsub topic at midnight every day.我还使用云调度程序在每天午夜向 pubsub 主题发送消息。 I've verified that this works correctly.我已经验证这可以正常工作。 I am new to pubsub but I followed the tutorial in the documentation and managed to setup a test cloud function that prints out hello world when it gets a push notification from pubsub.我是 pubsub 新手,但我遵循文档中的教程并设法设置了一个测试云 function,当它从 pubsub 收到推送通知时打印出 hello world。

However, my issue is that when I try to combine the two and automate my script - I get a log message that the execution crashed:但是,我的问题是,当我尝试将两者结合起来并自动化我的脚本时 - 我收到一条执行崩溃的日志消息:

Function execution took 1119 ms, finished with status: 'crash'

To help you understand what I'm doing, here is the code in my main.py:为了帮助您了解我在做什么,这是我的 main.py 中的代码:

# Global libraries
import base64

# Local libraries
from scripts.one_minute_tables import helper

def one_minute_tables(event, context):

    # Log out the message that triggered the function
    print("""This Function was triggered by messageId {} published at {}
    """.format(context.event_id, context.timestamp))

    # Get the message from the event data
    name = base64.b64decode(event['data']).decode('utf-8')

    # If it's the message for the daily midnight schedule, execute function
    if name == 'midnight':
        helper.format_tables('raw_data','table1')
    else:
        pass

For the sake of convenience, this is a simplified version of my python script:为方便起见,这是我的 python 脚本的简化版本:

# Global libraries
from google.cloud import bigquery
import os

# Login to bigquery by providing credentials
credential_path = 'secret.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

def format_tables(dataset, list_of_tables):

    # Initialize the client
    client = bigquery.Client()

    # Loop through the list of tables
    for table in list_of_tables:

        # Create the query object
        script = f"""
            SELECT *
            FROM {dataset}.{table}
        """

        # Call the API
        query = client.query(script)

        # Wait for job to finish
        results = query.result()

        # Print
        print('Data cleaned and updated in table: {}.{}'.format(dataset, table))

This is my folder structure:这是我的文件夹结构:

在此处输入图像描述

And my requirements.txt file has only one entry in it: google-cloud-bigquery==1.24.0而我的requirements.txt文件中只有一个条目: google-cloud-bigquery==1.24.0

I'd appreciate your help in figuring out what I need to fix to run this script with the pubsub trigger without getting a log message that says the execution crashed.感谢您帮助我弄清楚我需要修复什么才能使用 pubsub 触发器运行此脚本,而不会收到显示执行崩溃的日志消息。

EDIT: Based on the comments, this is the log of the function crash编辑:根据评论,这是 function 崩溃的日志

{
  "textPayload": "Function execution took 1078 ms, finished with status: 'crash'",
  "insertId": "000000-689fdf20-aee2-4900-b5a1-91c34d7c1448",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "function_name": "one_minute_tables",
      "region": "us-central1",
      "project_id": "PROJECT_ID"
    }
  },
  "timestamp": "2020-05-15T16:53:53.672758031Z",
  "severity": "DEBUG",
  "labels": {
    "execution_id": "x883cqs07f2w"
  },
  "logName": "projects/PROJECT_ID/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "trace": "projects/PROJECT_ID/traces/f391b48a469cbbaeccad5d04b4a704a0",
  "receiveTimestamp": "2020-05-15T16:53:53.871051291Z"
}

The problem comes from the list_of_tables attributes.问题来自list_of_tables属性。 You call your function like this你这样称呼你的 function

    if name == 'midnight':
        helper.format_tables('raw_data','table1')

And you iterate on your 'table1' parameter然后你迭代你'table1'参数

Perform this, it should work执行此操作,它应该可以工作

    if name == 'midnight':
        helper.format_tables('raw_data',['table1'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 云 Function 由 Pubsub 触发 - Cloud Function Triggered by Pubsub Cloud Run 的存储触发功能 - Storage trigger function with Cloud Run 如何在google-cloud-platform vminstance中的pubsub回调函数中调用全局变量? - How to call a global variable from a pubsub callback function in google-cloud-platform vminstance? BASE64 在解码 pubsub 消息时在谷歌云 function 中应用时解码不起作用 - BASE64 decoding not working when applied in google Cloud function while decoding pubsub message 如何使用云 function 触发数据流? (Python SDK) - How to trigger a dataflow with a cloud function? (Python SDK) Google-Cloud-Scheduler 因 http 触发 Cloud Function 失败 - Google-Cloud-Scheduler fail with http trigger Cloud Function 在 Cloud Functions 中运行异步函数 - Running an asynchronous function within a Cloud Function 数据流无法将消息从PubSub推送到BigQuery - Dataflow failing to push messages to BigQuery from PubSub 将标签应用于 Cloud SQL 、BigQuery 和 PubSub - Applying labels to Cloud SQL , BigQuery and PubSub 无法执行 Cloud Function 触发 HTTP 触发 Cloud Function 不允许未经身份验证的调用? - Unable to perform Cloud Function trigger a HTTP triggered Cloud Function that doesn't allow unauthenticated invocations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM