如何在Google Cloud Engine中运行具有事件触发功能的Firebase Admin App?

时间:2019-06-14 04:10:13

标签: node.js firebase firebase-realtime-database google-compute-engine

我之前还没有意识到Cloud Function对区域有很大的限制。我的服务部署在asia-southeast1c(基于Go的服务,Redis缓存在相同的区域和区域)区域内,但是不幸的是,云功能仅在us-central1中可用。是否可以在存在Redis和Service实例的Google Compute Engine上运行该功能?

我已经尝试使用VPC,但是不知道该区域一定在us-central1中。我想实现Cloud Function,因为它具有事件触发更新(我想对我的服务使用该更新)来提供向我的服务提供数据的读写Redis缓存。如果我直接从Firebase那里拉,那很费时间。

这是我的代码,IP和有关db和服务帐户的其他信息,由于敏感信息而被审查。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const bbkey             = "outlet:1:available_vehicles:bluebird"
const   sbkey             = "outlet:1:available_vehicles:silverbird"
Redis = require('ioredis')
var redis = new Redis({
    //sentinels: [{host: "XXX.XX.XX.2", port: "26379"},{host: "XXX.XX.XX.3", port: "26379"}],
    sentinels: [{host: "XXXXX", port: "26379"},{host: "XXXXXXZ", port: "26379"}],
name: "mymaster"
});
redis.ping(function (err, result) {
    console.log(result);
    console.log(err)
});

redis.on("error",function(err){
    console.log("ERROR! "+err);
});


function Counter (fleetname,serviceType,location,status){

        if(serviceType===bbkey&&location==="1:PERIMETER BANDARA"){
            date = new Date().getTime();
            if(status<=0){
                console.log(fleetname+" from Bxxxxxx are inbound pool at "+date);
                redis.rpush(bbkey,fleetname);
            }
            if(status>0){
                console.log(fleetname+" from Bxxxxxx are outbound pool at "+date);
                redis.lrem(bbkey,0,fleetname);
            }
            if(serviceType===sbkey&&location==="1:PERIMETER BANDARA"){
                date = new Date().getTime();
            if(status<=0){
                console.log(fleetname+" from Sxxxxxx are inbound pool at "+date);
                redis.rpush(sbkey,fleetname)
            }
            if(status>0){
                console.log(fleetname+" from Sxxxxxx are outbound pool at "+date);
                redis.lrem(sbkey,0,fleetname)
            }
        }
        }
}
exports.counterTaxi = 
functions.database.ref('/airport/{type}/vehicles/{no}/position')
.onUpdate((change, context) => {
    console.log('Counting', context.params.no);
    const position = change.after.val();
        return Counter(context.params.no,context.params.type,position.content.location,position.content.time_out)
})
;

0 个答案:

没有答案