Fixed soft removed documents never getting permanently removed

This commit is contained in:
Stefan Zermatten
2020-06-05 23:08:31 +02:00
parent 986fe8fd93
commit e296ff1548
2 changed files with 22 additions and 15 deletions

View File

@@ -1,11 +1,18 @@
import CreatureProperties from '/imports/api/creature/CreatureProperties.js';
import LibraryNodes from '/imports/api/library/LibraryNodes.js'; import LibraryNodes from '/imports/api/library/LibraryNodes.js';
import { assertAdmin } from '/imports/api/sharing/sharingPermissions.js';
import { SyncedCron } from 'meteor/percolate:synced-cron';
let collections = [LibraryNodes]; Meteor.startup(() => {
const collections = [
CreatureProperties,
LibraryNodes,
];
if (Meteor.isServer) Meteor.startup(() => {
/** /**
* Deletes all soft removed documents that were removed more than 30 minutes ago * Deletes all soft removed documents that were removed more than 30 minutes ago
* and were not restored * and were not restored
* @return {Number} Number of documents removed
*/ */
const deleteOldSoftRemovedDocs = function(){ const deleteOldSoftRemovedDocs = function(){
const now = new Date(); const now = new Date();
@@ -14,30 +21,30 @@ if (Meteor.isServer) Meteor.startup(() => {
collection.remove({ collection.remove({
removed: true, removed: true,
removedAt: {$lt: thirtyMinutesAgo} // dates *before* 30 minutes ago removedAt: {$lt: thirtyMinutesAgo} // dates *before* 30 minutes ago
}, error => { }, function(error){
if (error) console.error(error); if (error){
console.error(error);
}
}); });
}); });
return;
}; };
SyncedCron.add({ SyncedCron.add({
name: "Delete all soft removed items that haven't been restored", name: 'deleteSoftRemovedDocs',
schedule: function(parser) { schedule: function(parser) {
return parser.text('every 6 hours'); return parser.text('every 2 hours');
}, },
job: function() { job: deleteOldSoftRemovedDocs,
deleteOldSoftRemovedDocs();
}
}); });
SyncedCron.start();
// Add a method to manually trigger removal // Add a method to manually trigger removal
Meteor.methods({ Meteor.methods({
deleteOldSoftRemovedDocs() { deleteOldSoftRemovedDocs() {
const user = Meteor.users.findOne(this.userId); assertAdmin(this.userId);
if (user && _.contains(user.roles, "admin")){ this.unblock();
return deleteOldSoftRemovedDocs(); deleteOldSoftRemovedDocs();
}
}, },
}); });
}); });

View File

@@ -1,4 +1,4 @@
import '/imports/server/publications/index.js'; import '/imports/server/publications/index.js';
import '/imports/api/parenting/deleteRemovedDocuments.js';
import '/imports/server/config/simpleSchemaDebug.js'; import '/imports/server/config/simpleSchemaDebug.js';
import '/imports/server/cron/deleteSoftRemovedDocuments.js';
import '/imports/api/parenting/organizeMethods.js'; import '/imports/api/parenting/organizeMethods.js';