Reputation: 283
This code is a Google cloud function node.js script that exports a function fsdb_sch which uses the node-schedule and firebase-admin. The script sets up a scheduled job that runs every day at midnight using the node-schedule module. The job reads values from a Firebase Realtime Database at the location All_machines/Generator using firebase-admin module, and then writes the same values to new locations within the same database.
The Firebase Realtime Database is accessed with the help of a service account which is stored in a JSON file, ./******************************-7878b7ca66.json.
But When I am running this code I am getting only this error in logs nothing else this error Function execution took 60015 ms, finished with status: 'timeout'
. I have increase the time till 540s but function stop after 540s i have also increase the memory but nothing work. But the same code is running fine in my local system from pas few days without any errors.
this is my code :
const schedule = require('node-schedule');
const Admin = require("firebase-admin");
const { getDatabase } = require("firebase-admin/database");
const serviceAccount = require("./*********************************-7878b7ca66.json");
Admin.initializeApp({
credential: Admin.credential.cert(serviceAccount),
databaseURL: "https://****************************.firebasedatabase.app"
});
exports.fsdb_sch = (req, res) => {
const db = Admin.database();
const Db = getDatabase();
const ref = Db.ref("All_machines/Generator");
const Ref = db.ref("All_machines/Generator")
schedule.scheduleJob('0 0 * * *',() => {
ref.once('value', (snapshot) => {
snapshot.forEach((data) => {
ref.child(data.key).child('waterY').once('value', (snapshot) => {
let childref = Ref.child(data.key).child('waterYY');
childref.set(snapshot.val());
console.log(snapshot.val());
});
ref.child(data.key).child('waterT').once('value', (snapshot) => {
let childref = Ref.child(data.key).child('waterY');
childref.set(snapshot.val());
console.log(snapshot.val());
});
ref.child(data.key).child('compY').once('value', (snapshot) => {
let childref = Ref.child(data.key).child('compYY');
childref.set(snapshot.val());
console.log(snapshot.val());
});
ref.child(data.key).child('compT').once('value', (snapshot) => {
let childref = Ref.child(data.key).child('compY');
childref.set(snapshot.val());
console.log(snapshot.val());
});
});
}, (errorObject) => {
console.log('The read failed: ' + errorObject.name);
});
});
};
This is Package.json file
{
"name": "sample-http",
"version": "0.0.1",
"description": "Cloud Function for Schedular",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "16"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^11.4.1",
"firebase-functions": "^3.14.1",
"mqtt": "^4.3.7",
"node-schedule": "^2.1.0"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}
And this is the folder structure
Upvotes: 0
Views: 254
Reputation: 203
Cloud functions have timeout, you should not use cloud functions for long running tasks.
You can use this guide to trigger your function using cloud scheduler instead.
Upvotes: 1