此代码是一个Google云函数node.js脚本,用于导出使用node-schedule和firebase-admin的函数fsdb_sch。该脚本使用node-schedule模块设置每天午夜运行的计划作业。该作业使用firebase-admin模块从位于All_machines/Generator位置的Firebase实时数据库读取值,然后将相同的值写入同一数据库中的新位置。
Firebase实时数据库可在存储在JSON文件./*********************************-7878b7ca66.json中的服务帐户的帮助下访问。
但当我运行这段代码时,我只得到日志中的这个错误,没有其他错误Function execution took 60015 ms, finished with status: 'timeout'
。我增加了540 s的时间,但540 s后功能停止,我也增加了内存,但没有任何工作。但相同的代码在我的本地系统中运行良好,从过去几天没有任何错误。这是我的代码:
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);
});
});
};
这是Package.json文件
{
"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
}
这是文件夹结构x1c 0d1x
日志:
1条答案
按热度按时间q9rjltbz1#
云函数具有timeout,不应将云函数用于长时间运行的任务。
您可以使用this guide来触发使用云调度器的函数。
1.调度程序将根据您的cron表达式发布到主题(仅为空数据
1.主题将触发您的云函数,并执行所需的脚本。