【HAVENT原创】Firebase 定时任务遍历删除子节点

时间:2021-10-29 07:53:36

每周定时执行,遍历 Firebase 数据库,删除过期的节点:

  1 var config = require('./config.json');
2
3 var admin = require('firebase-admin');
4 var schedule = require('node-schedule');
5
6 var log4js = require('log4js');
7 log4js.configure({ // configure to use all types in different files.
8 appenders: [
9 { type: 'file',
10 filename: config.firebase.log,
11 category: 'service',
12 maxLogSize: 20480000,
13 backups: 10
14 }
15 ]
16 });
17 var logger = log4js.getLogger('service');
18
19 var mysql = require('mysql');
20 var pool = mysql.createPool({
21 connectionLimit: 10,
22 host: config.mysql.host,
23 user: config.mysql.user,
24 password: config.mysql.password,
25 database: config.mysql.db
26 });
27
28
29 var defaultAppConfig = {
30 credential: admin.credential.cert(config.firebase.cert),
31 databaseURL: config.firebase.databaseURL
32 };
33
34 var defaultAppName = 'GoPeople-NodeJS-Admin';
35 var defaultApp = admin.initializeApp(defaultAppConfig, defaultAppName);
36
37 var isWorking = false;
38 var hasMoreData = true;
39 function init() {
40 logger.info('People Post remove signatures service start.');
41
42 // '0 0 0 * * 6' Saturday 00:00:00
43 // '*/5 * * * *' per 5 min
44 // '*/30 * * * * *' per 30 sec
45 var weekTask = schedule.scheduleJob('11 34 11 * * 2', function () {
46 logger.info('schedule week task start!');
47
48 var j = schedule.scheduleJob('*/30 * * * * *', function(){
49 startRemoveSignatures();
50
51 if(!hasMoreData){
52 logger.info('schedule task finished!');
53 logger.info();
54 logger.info();
55 schedule.cancelJob(j);
56 }
57 });
58 });
59 }
60
61 function startRemoveSignatures() {
62 var expiredDataCount = 0;
63
64 if(isWorking){
65 logger.info('startRemoveSignatures: hold on ...');
66 return;
67 }else{
68 isWorking = true;
69 logger.info();
70 logger.info('startRemoveSignatures: loading signatures data ...');
71 }
72
73 var signaturesRef = defaultApp.database().ref('signatures');
74
75 //signaturesRef.orderByChild("isChecked").equalTo(true).limitToLast(300).once("value")
76 signaturesRef.orderByChild("timestamp").limitToFirst(50).once("value")
77 .then(function(snapshot) {
78
79 snapshot.forEach(function(childSnapshot) {
80 var key = childSnapshot.key;
81 var childData = childSnapshot.val();
82
83 var now = new Date();
84 var date = new Date(childData.date);
85 var dayDiff = parseInt((now - date) / (1000 * 60 * 60 * 24)); // day diff
86
87 if(dayDiff > 7){
88 expiredDataCount++;
89
90 signaturesRef.child(key).remove(function(error) {
91 console.log(key);
92 console.log(dayDiff);
93 console.log(error ? ("Uh oh! " + error) : "Success!");
94
95 logger.info(key);
96 if(error){
97 logger.error(error);
98 }else{
99 logger.info("Success! [ " + dayDiff + ' day ]');
100 }
101 });
102 }else{
103 console.log(key);
104 console.log(dayDiff);
105
106 logger.info(key);
107 logger.info("Jump!");
108 }
109 });
110
111 isWorking = false;
112 logger.info('expiredDataCount: ' + expiredDataCount);
113
114 if(expiredDataCount < 30){
115 hasMoreData = false;
116 }
117 });
118 }
119
120 init();