Implement log file rotation
authorNick Downing <nick@ndcode.org>
Sat, 8 Jan 2022 03:30:33 +0000 (14:30 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 9 Jan 2022 00:30:05 +0000 (11:30 +1100)
_config/site.jst

index 0298224..b41a6a4 100644 (file)
@@ -1,5 +1,6 @@
 let assert = require('assert')
 let EmailJSCache = require('@ndcode/emailjs_cache')
+let XDate = require('xdate')
 let ZettairCache = require('@ndcode/zettair_cache')
 
 return async (resources, root, prev_site) => {
@@ -11,6 +12,7 @@ return async (resources, root, prev_site) => {
     _jst_server.Site.call(this, resources, root, options, prev_site)
 
     this.database = undefined
+    this.database_date = new XDate().toUTCString('yyyyMMdd')
     this.emailjs_cache = undefined
     this.zettair_cache = undefined
   }
@@ -67,7 +69,19 @@ return async (resources, root, prev_site) => {
     await _jst_server.Site.prototype.kick.call(this)
 
     assert(this.database !== undefined)
-    this.database.kick()
+    await this.database.kick()
+
+    let new_database_date = new XDate().toUTCString('yyyyMMdd')
+    if (new_database_date !== this.database_date) {
+      console.log(
+        'rotate database',
+        this.database_date,
+        '->',
+        new_database_date
+      )
+      await this.database.rotate('database.logjson.' + this.database_date)
+      this.database_date = new_database_date
+    }
 
     assert(this.emailjs_cache !== undefined)
     this.emailjs_cache.kick()