Firebase Function returning connection error - node.js

I am using firebase functions Database Triggers.
My Function :
exports.function_name = functions.database
.ref("/transact/{entry_type}/{id1}/{id2}/trigger_cross_entry_function")
.onCreate((data, context) => {
console.log("trigger_cross_entry_function value", data.val());
if (data.val() == true) {
console.log("Function Calling parent data");
return data.ref.parent.once('value').then(function(snapshot){
}, function(error) {
// The Promise was rejected.
console.error("THIS IS THE ERROR:"+error);
});
} else {
console.log("Function Returned");
return 0;
}
});
Whenever I want to trigger this function I put trigger_cross_entry_function into that partcular path from the Mobile App. Everything works fine and function is called as expected.
After sometime If I again try to do the same thing it gives me
Function execution took 16 ms, finished with status: 'connection error'
If I restart the function than it again works perfectly.
and If I trigger it continously than it has no issue.
But once I keep it idle and try to trigger it
again after sometime, it gives me this error.
And I am using Firebase Pay as you go Plan.
Also, as you can see I am returning all the promises correctly and function is being triggered everytime but It doesnt go into the function. it just exit with connection error.
What might be the cause for this ? I have a spent a day finding this.

Experiencing the same issue. Looks like Google changed the status of the service to down
https://status.firebase.google.com/incident/Functions/18046
https://status.firebase.google.com/
So most likely the Google side problem.

Related

Firebase cloud function execution not ending

I have firebase project that has few cloud functions. Some of them sends push notifications to the users. Before deploying any function, I develop it using the nodeJS on my local environment with standalone nodejs project.
However today I noticed that the function execution gets stuck after I try to send a push notification to even a single user. I mean to say the cursor does not return after I hit the node test.js and the execution never stops. I need to do ctrl + z in order to end the execution. However I get the push notification.
My code:
async function sendTest() {
await admin.messaging().sendToDevice(token, payload);
console.debug('Message Sent'); // this line gets printed instantly but execution continues
}
The following is also a similar code but it also doesn't end the execution even after printing the debug. statement.
async function sendTest() {
var notificationPersonalPromises = []
notificationPersonalPromises.push(admin.messaging().sendToDevice(token, payload));
await Promise.all(notificationPersonalPromises);
console.debug('Message Sent'); // this line gets printed instantly but execution continues
}
In both the above cases, I get the notification in couple of seconds and the debug gets printed instantly. If I comment out the push notification code then everything works fine. Please help...
The following image shows how the functions never ends the cursor keeps waiting...
Thanks
Make sure that your cloud function have a retunr. Even if you don't return anything. You can also return your last promise like:
async function sendTest() {
return admin.messaging().sendToDevice(token, payload);
}
or just an empty return like here:
async function sendTest() {
await admin.messaging().sendToDevice(token, payload);
console.debug('Message Sent'); // this line gets printed instantly but execution continues
return
}

Serverless lambda - Function not executing completely

I'm using serverless with nodejs. I have lambda function which have around 6 API in it.
All APIs are working except one. But this non-working api is working fine in local serverless offline. After deployment in server, then it is behaving delayed.
Here is skeleton of what I did in api function
let rec_list = await db.sequelize.query(query).spread(rec_list => { return rec_list; }).catch((e)=>{
console.log("error");
throw e;
})
let rec_list2= rec_list .map((rec_list_sub) => {
//some assignment here
//let new_var = {}; // just assignment - no db operation
return new_var;
});
let resultArr = await Promise.all(rec_list2).then((result) => {
return result;
}).catch((e) => {
throw e;
});
let tem_list = await db.mymodel.bulkCreate(resultArr).then(function (li) {
selectedIds = li.map(({ id }) => {
return {
reqId: id,
description: 'sent',
status: 0
}
});
return li;
}).catch(function (err) {
throw err;
});
//send fcm push
//triggering push notification to user
fcm_send_msg("success", "body-message-here",["fdsfdsfdsf-device-id"]); //*--> Push notification is not triggered. When i hit any of api from this same lambda function, then previously called apis' push notification triggered.*
If I place push notification function call which is aysnc (tried with/without await) before bulkcreate call, then it is working but bulk create delayed.
Execution time for lambda function is 6 sec but it took 108ms only. Memory allocated 1024mb but 120mb only used.
I'm going to make a lot of assumptions, so if I am totally off base let me know and I will delete this answer. I assume your lambda is running in a VPC since it is accessing a database. And from your question I am understanding you to be saying that when the call "bulkCreate" happens before the call to "fcm_send_msg" then you are experiencing the issue, but when the call the "fcm_send_msg" happens before the call to "bulkCreate" then you get the notification, but the bulk create is still delayed.
I suspect you may have a permission issue. Lambda uses an ephemeral port range for calls, so you will want to be sure your security groups and NACLs allow the full ephemeral port range from which Lambda may originate the calls. See https://docs.aws.amazon.com/vpc/latest/userguide/vpc-network-acls.html#nacl-ephemeral-ports:
AWS Lambda functions use ports 1024-65535.
You can check the CloudWatch logs for the lambda (from monitoring) to see what it is doing, and if you see things in the logs that seem like something failed and it is retrying it could very well indicate that the calls are being blocked due to security groups or NACLs that don't allow the communication.

Firebase Bucket Download File taking a lot of time

I'm developing an app using Firebase as BaaS.
I'm having time problems when I'm uploading an image (with 90KB or less) and triggering a Cloud Function.
My trigger starts when upload ends:
exports.uploadControl = functions.storage.object().onFinalize((req, res) => {
uploadControl.handler(req, res);
return 0;
});
And, inside uploadControl, i have:
return mkdirp(tempLocalDir).then(() => {
console.log('1. mkDirp - OK!');
console.log('2. Download starts...');
return bucket.file(filePath).download();
}).then((content) => {
console.log('3. Download ends');
return 0;
});
This code works fine, but the problem is the time spent between step 2 and 3...
It takes 24 sec or more.
How to solve this? Is there any code problem? Or is there a Firebase setting to solve it?
Tks.
Two things wrong here:
The onFinalize() callback doesn't receive res and req objects like HTTP triggers do. It receives object metadata as the first argument. Read the documentation for details.
background triggers like this one must return a promise when all the work is complete. Otherwise Cloud Functions will shut down the work prematurely, as it has no idea when it's finished. If you want to kick off all that work from another function, it should be returning that promise.
-
exports.uploadControl = functions.storage.object().onFinalize(object => {
return uploadControl.handler(object);
});

Getting TRIGGER_PAYLOAD_TOO_LARGE error on simple Firebase Cloud Function

I am getting this error when a cloud function is executed:
Error: TRIGGER_PAYLOAD_TOO_LARGE: This request would cause a function payload exceeding the maximum size allowed.
This is the cloud function causing the error:
exports.inyectSerie = functions.database.ref('forms/{pushId}').onCreate(event => {
if (!admin.apps.length) {
admin.initializeApp();
}
var form = event.val();
var formData = {
serie: form.serie
};
admin.database().ref('series/'+form.serie).set(formData);
});
How do I know this is the function causing the error? I removed all cloud functions from my firebase and it worked as expected. Then I put back this inyectSerie function and it gave me the error again.
This is my firebase structure, being "medidores" node the one with the most data, with 150k records (which doesn't sound like a lot to me):
+fallidas
+forms <-- This has only 20 records
+materiales
+medidores <-- This has 150,000+ records
+series
+users
If you notice, medidores node is never touched on the cloud function.
I searched for the error and only found this other question reporting it, but I think the cloud function causing the problem on that case did access all the records on the db.
The only thing that comes to my mind to be the problem on my case, is that functions.database loads the whole database no matter what.
UPDATE: Even after reducing my trigger to a bare minimum (thanks, James Poag), I am getting the same error.
exports.inyectSerie = functions.database.ref('forms/{pushId}').onCreate(event => {
return null;
});

Crashlytics doesn't trigger onRegressed when a previously logged crash occurs

I am trying to log all the errors from Firebase Crashlytics to my MongoDB database. When an error occurs for the first time the function below gets triggered successfully
exports.sendOnNew = functions.crashlytics.issue().onNew(issue => {
logmodel.create(issue, function(err) {
if (err)
returnValue = handleError(err);
else
returnValue = "Succeed";
});
return returnValue;
});
However, I expect that if the same error occurs again, it should trigger onRegressed, right? Well, it doesn't happen.
exports.sendOnRegressed = functions.crashlytics.issue().onRegressed(event => {
logmodel.create(event, function(err) {
if (err)
returnValue = handleError(err);
else
returnValue = "Succeed";
});
return returnValue;
});
Is there anything wrong with my code above? It follows the same pattern from Google Docs. My functions are all successfully deployed, and doesn't show me an error or any clue in the logs.
By the way, I can see the error in the Crashlytics tab on Firebase. However, my event doesn't get triggered.
I appreciate any advice you can give.
Thanks a lot.
According to the documentation:
Regressed issue events trigger when an issue reoccurs after it's closed in Crashlytics.
It's not simply triggered on a second instance of the same crash. The crash has to be previously closed in the console. That's what a regression is: a occurrence of a crash after you think that you've fixed it already.

Resources