Skip to content

Commit

Permalink
[FIX] Prune messages by cron if room not updated (#15252)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo authored and sampaiodiego committed Sep 19, 2019
1 parent 94b67b0 commit fa78ecd
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions app/retention-policy/server/cronPruneMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ let types = [];

const oldest = new Date('0001-01-01T00:00:00Z');

let lastPrune = oldest;

const maxTimes = {
c: 0,
p: 0,
d: 0,
};
const toDays = 1000 * 60 * 60 * 24;
const gracePeriod = 5000;

const toDays = (d) => d * 1000 * 60 * 60 * 24;

function job() {
const now = new Date();
const filesOnly = settings.get('RetentionPolicy_FilesOnly');
Expand All @@ -27,11 +27,10 @@ function job() {
// get all rooms with default values
types.forEach((type) => {
const maxAge = maxTimes[type] || 0;
const latest = new Date(now.getTime() - maxAge * toDays);
const latest = new Date(now.getTime() - toDays(maxAge));

Rooms.find({
t: type,
_updatedAt: { $gte: latest },
$or: [
{ 'retention.enabled': { $eq: true } },
{ 'retention.enabled': { $exists: false } },
Expand All @@ -46,25 +45,23 @@ function job() {
'retention.enabled': { $eq: true },
'retention.overrideGlobal': { $eq: true },
'retention.maxAge': { $gte: 0 },
_updatedAt: { $gte: lastPrune },
}).forEach((room) => {
const { maxAge = 30, filesOnly, excludePinned } = room.retention;
const latest = new Date(now.getTime() - maxAge * toDays);
const latest = new Date(now.getTime() - toDays(maxAge));
cleanRoomHistory({ rid: room._id, latest, oldest, filesOnly, excludePinned, ignoreDiscussion });
});
lastPrune = new Date(now.getTime() - gracePeriod);
}

function getSchedule(precision) {
switch (precision) {
case '0':
return '0 */30 * * * *';
return '0 */30 * * * *'; // 30 minutes
case '1':
return '0 0 * * * *';
return '0 0 * * * *'; // hour
case '2':
return '0 0 */6 * * *';
return '0 0 */6 * * *'; // 6 hours
case '3':
return '0 0 0 * * *';
return '0 0 0 * * *'; // day
}
}

Expand All @@ -84,26 +81,26 @@ function deployCron(precision) {
function reloadPolicy() {
types = [];

if (settings.get('RetentionPolicy_Enabled')) {
if (settings.get('RetentionPolicy_AppliesToChannels')) {
types.push('c');
}
if (!settings.get('RetentionPolicy_Enabled')) {
return SyncedCron.remove(pruneCronName);
}
if (settings.get('RetentionPolicy_AppliesToChannels')) {
types.push('c');
}

if (settings.get('RetentionPolicy_AppliesToGroups')) {
types.push('p');
}
if (settings.get('RetentionPolicy_AppliesToGroups')) {
types.push('p');
}

if (settings.get('RetentionPolicy_AppliesToDMs')) {
types.push('d');
}
if (settings.get('RetentionPolicy_AppliesToDMs')) {
types.push('d');
}

maxTimes.c = settings.get('RetentionPolicy_MaxAge_Channels');
maxTimes.p = settings.get('RetentionPolicy_MaxAge_Groups');
maxTimes.d = settings.get('RetentionPolicy_MaxAge_DMs');
maxTimes.c = settings.get('RetentionPolicy_MaxAge_Channels');
maxTimes.p = settings.get('RetentionPolicy_MaxAge_Groups');
maxTimes.d = settings.get('RetentionPolicy_MaxAge_DMs');

return deployCron(settings.get('RetentionPolicy_Precision'));
}
return SyncedCron.remove(pruneCronName);
return deployCron(settings.get('RetentionPolicy_Precision'));
}

Meteor.startup(function() {
Expand Down

0 comments on commit fa78ecd

Please sign in to comment.