使用NodeJS自行触发永久运行的Firebase进程
我有一套logging,我想永久更新。 基本上:
- 获取最近更新的logging
- 更新logging
- 将loggingdate设置为现在(即将其发送到列表的最后)
- 回到第1步
以下是我使用Firebase的想法:
// update record function var updateRecord = function() { // get least recently updated record firebaseOOO.limit(1).once('value', function(snapshot) { key = _.keys(snapshot.val())[0]; /* * do 1-5 seconds of non-Firebase processing here */ snapshot.ref().child(key).transaction( // update record function(data) { return updatedData; }, // update priority after commit (would like to do it in transaction) function(error, committed, snap2) { snap2.ref().setPriority(snap2.dateUpdated); } ); }); }; // listen whenever priority changes (aka. new item needs processing) firebaseOOO.on('child_moved', function(snapshot) { updateRecord(); }); // kick off the whole thing updateRecord();
这是一个合理的事情吗?
一般来说,这种守护进程正是设想与Firebase NodeJS客户端一起使用的。 所以,这个方法看起来不错。
但是,在on()
调用中,看起来像是放弃了传递到地板上的snapshot
。 这可能是应用程序特定于你正在做的事情,但是消费与updateRecord()
中发生的once()
相关的snapshot
效率会更高。