changelog delete
This commit is contained in:
		
							
								
								
									
										12
									
								
								src/db.ts
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/db.ts
									
									
									
									
									
								
							@@ -2,12 +2,14 @@ import mongoose from 'mongoose';
 | 
			
		||||
import cfenv from 'cfenv';
 | 
			
		||||
import _ from 'lodash';
 | 
			
		||||
import ChangelogModel from './models/changelog';
 | 
			
		||||
import cron from 'node-cron';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// database urls, prod db url is retrieved automatically
 | 
			
		||||
const TESTING_URL = 'mongodb://localhost/dfopdb_test';
 | 
			
		||||
const DEV_URL = 'mongodb://localhost/dfopdb';
 | 
			
		||||
const debugging = true;
 | 
			
		||||
const changelogKeepDays = 30;  // days to keep the changelog
 | 
			
		||||
 | 
			
		||||
if (process.env.NODE_ENV !== 'production' && debugging) {
 | 
			
		||||
  mongoose.set('debug', true);  // enable mongoose debug
 | 
			
		||||
@@ -78,6 +80,16 @@ export default class db {
 | 
			
		||||
      this.state.db = mongoose.connection;
 | 
			
		||||
      done();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if (mode !== 'test') {  // clear old changelog regularly
 | 
			
		||||
      cron.schedule('0 0 * * *', () => {
 | 
			
		||||
        ChangelogModel.deleteMany({_id: {$lt:  // id from time
 | 
			
		||||
          Math.floor(new Date().getTime() / 1000 - changelogKeepDays * 24 * 60 * 60).toString(16) + '0000000000000000'
 | 
			
		||||
        }}).log({method: 'scheduled changelog delete', url: '', authDetails: {}}).lean().exec(err => {
 | 
			
		||||
          if (err) console.error(err);
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static disconnect (done) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user