Friday, 7 April 2017

BigQuery read ECONNRESET

When using Firebase Cloud Functions in combination with Google BigQuery. Sometimes an error is thrown randomly when this function is triggered.

This is our error log:

Error: read ECONNRESET
    at exports._errnoException (util.js:1026:11)
    at TLSWrap.onread (net.js:569:26)

And here the code my colleague made.

const bigQuery = require('@google-cloud/bigquery');
const admin = require('firebase-admin');
const database = admin.database();

exports.updateAllPlaceStatistics = (request, response) => {
    const secret = request.query['secret'];

    if (secret !== 'secret') {
        return response.json({message: 'Request failed!'});
    }

    const big = bigQuery();

    return big.query({
        query: [
            'SELECT place.id, COUNT(DISTINCT beacon.id) as beacons, COUNT(DISTINCT promotion.id) as promotions, placeUsers.users as users',
            'FROM `omega.sw_places` as place',
            'LEFT JOIN `omega.sw_promotions` as promotion ON promotion.place_id = place.id',
            'LEFT JOIN `omega.sw_beacons` as beacon ON beacon.place_id = place.id',
            'LEFT JOIN `omega.view_users_per_place` as placeUsers ON placeUsers.id = place.id',
            'GROUP BY place.id, placeUsers.users'
        ].join(' '),
        params: []
    }).then((data) => {
        const rows = data[0];

        let result = {};
        for (let index = 0; index < rows.length; index++) {
            const item = rows[index];

            result[item.id] = {
                beacons: item.beacons,
                promotions: item.promotions
            };
        }

        return database.ref('statistics/general').set(result);
    }).then(() => {
        return response.json({message: 'Request succeeded!'});
    }).catch((error) => {
        console.log('An error has happened to the big_query.js');
        console.log(JSON.stringify(error));

        return response.json({message: 'Request failed!'});
    });
};

What is going wrong?



via Patrix Williams

No comments:

Post a Comment