Saturday 1 April 2017

ECONRESET socket hungup

I have a function that triggers on firebase database onWrite. The function body use two google cloud apis (DNS and Storage).

While the function is running and working as expected (mostly), the issue is that the Socket hang up more often than I'd like. (50%~ of times)

My questions are: Is it similar to what the rest of the testers have experienced? Is it a well known issue that is outstanding or expected behavior?

the example code is as follows:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {credentials} = functions.config().auth;
credentials.private_key = credentials.private_key.replace(/\\n/g, '\n');
const config = Object.assign({}, functions.config().firebase, {credentials});
admin.initializeApp(config);
const gcs = require('@google-cloud/storage')({credentials});
const dns = require('@google-cloud/dns')({credentials});
const zoneName = 'applambda';
const zone = dns.zone(zoneName);

exports.createDeleteDNSAndStorage = functions.database.ref('/apps/{uid}/{appid}/name')
.onWrite(event => {
    // Only edit data when it is first created.
    const {uid, appid} = event.params;
    const name = event.data.val();
    const dbRef = admin.database().ref(`/apps/${uid}/${appid}`);

    if (event.data.previous.exists()) {
        console.log(`already exists ${uid}/${appid}`);
        return;
    }
    // Exit when the data is deleted.
    if (!event.data.exists()) {
        console.log(`data is being deleted ${uid}/${appid}`);
        return;
    }

    const url = `${name}.${zoneName}.com`;
    console.log(`data: ${uid}/${appid}/${name}\nsetting up: ${url}`);

    setupDNS({url, dbRef});
    setupStorage({url, dbRef});
    return;
});

function setupDNS({url, dbRef}) {

    // Create an NS record.
    let cnameRecord = zone.record('cname', {
        name: `${url}.`,
        data: 'c.storage.googleapis.com.',
        ttl: 3000
    });

    zone.addRecords(cnameRecord).then(function() {
        console.log(`done setting up zonerecord for ${url}`);
        dbRef.update({dns: url}).then(res => console.log(res)).catch(err => console.log(err));
    }).catch(function(err) {
        console.error(`error setting up zonerecord for ${url}`);
        console.error(err);
    });
}

function setupStorage({url, dbRef}) {
    console.log(`setting up storage bucket for ${url}`);

    gcs.createBucket(url, {
        website: {
            mainPageSuffix: `https://${url}`,
            notFoundPage: `https://${url}/404.html`
        }
    }).then(function(res) {
        let bucket = res[0];
        console.log(`created bucket ${url}, setting it as public`);
        dbRef.update({storage: url}).then(function() {
            console.log(`done setting up bucket for ${url}`);
        }).catch(function(err) {
            console.error(`db update for storage failed ${url}`);
            console.error(err);
        });
        bucket.makePublic().then(function() {
            console.log(`bucket set as public for ${url}`);
        }).catch(function(err) {
            console.error(`setting public for storage failed ${url}`);
            console.error(err);
        });
    }).catch(function(err) {
        console.error(`creating bucket failed ${url}`);
        console.error(err);
    });
}



via Adnan Y

No comments:

Post a Comment