适用于Firebase BigQuery同步错误的云功能

时间:2017-04-13 15:35:20

标签: node.js firebase firebase-realtime-database google-bigquery google-cloud-functions

我们正在开发一个云功能,允许我们保持我们的bigquery和firebase数据库同步。创建/更新/删除场所时会触发该功能。

根据触发操作(创建/更新/删除),我们添加一个名为big_query_active的属性来表示对象是否存在。同样适用于约会。

我们当前的问题是对大查询的调用有时会返回错误。这意味着数据不再同步。如何防止这种情况?

'use strict';

// Default imports.

const functions = require('firebase-functions');
const bigQuery = require('@google-cloud/bigquery');

// If you want to change the nodes to listen to REMEMBER TO change the constants below.
// The 'id' field is AUTOMATICALLY added to the values, so you CANNOT add it.

const ROOT_NODE = 'places';
const VALUES = [
    'country_id',
    'category_id',
    'name',
    'active',
    'archived'
];

// This function listens to the supplied root node, but on child added/removed/changed.
// When an object is inserted/deleted/updated the appropriate action will be taken.

exports.children = functions.database.ref(ROOT_NODE + '/{id}').onWrite(event => {
    const query = bigQuery();
    const dataset = query.dataset('stampwallet');
    const table = dataset.table(ROOT_NODE);

    if (!event.data.exists() && !event.data.previous.exists()) {
        return;
    }

    const item = event.data.exists() ? event.data.val() : event.data.previous.val();

    const data = {};

    data['id'] = event.params.id;

    for (let index = 0; index < VALUES.length; index++) {
        const key = VALUES[index];
        data[key] = item[key] !== undefined ? item[key] : null;
    }

    data['big_query_date'] = new Date().getTime() / 1000;
    data['big_query_active'] = event.data.exists();

    return table.insert(data).then(() => {
        return true;
    }).catch((error) => {
        if (error.name === 'PartialFailureError') {
            console.log('A PartialFailureError happened while uploading to BigQuery...');
        } else {
            console.log(JSON.stringify(error));
            console.log('Random error happened while uploading to BigQuery...');
        }
    });
});

这是我们(有时)收到的错误

{"code":"ECONNRESET","errno":"ECONNRESET","syscall":"read"}

如何防止数据不同步?或者有没有办法重试,以便它总是成功?

0 个答案:

没有答案