Skip to content

Commit

Permalink
fix: Cron job for clearing OEmbed cache isn't working (#31336)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 authored Jan 22, 2024
1 parent ec285f7 commit c8ab658
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/selfish-rice-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---

Fixed issue with OEmbed cache not being cleared daily
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ function ActionSettingInput({ _id, actionText, value, disabled, sectionChanged }

const handleClick = async (): Promise<void> => {
try {
const data: { message: TranslationKey; params: string[] } = await actionMethod();
const data: { message: TranslationKey; params?: string[] } = await actionMethod();

dispatchToastMessage({ type: 'success', message: t(data.message, ...data.params) });
const params = data.params || [];
dispatchToastMessage({ type: 'success', message: t(data.message, ...params) });
} catch (error) {
dispatchToastMessage({ type: 'error', message: error });
}
Expand Down
5 changes: 3 additions & 2 deletions apps/meteor/server/cron/oembed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cronJobs } from '@rocket.chat/cron';
import { Meteor } from 'meteor/meteor';

import { executeClearOEmbedCache } from '../methods/OEmbedCacheCleanup';

export async function oembedCron(): Promise<void> {
await cronJobs.add('Cleanup OEmbed cache', '24 2 * * *', async () => Meteor.callAsync('OEmbedCacheCleanup'));
await cronJobs.add('Cleanup OEmbed cache', '24 2 * * *', async () => executeClearOEmbedCache());
}
12 changes: 8 additions & 4 deletions apps/meteor/server/methods/OEmbedCacheCleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ declare module '@rocket.chat/ui-contexts' {
}
}

export const executeClearOEmbedCache = async () => {
const date = new Date();
const expirationDays = settings.get<number>('API_EmbedCacheExpirationDays');
date.setDate(date.getDate() - expirationDays);
return OEmbedCache.removeBeforeDate(date);
};

Meteor.methods<ServerMethods>({
async OEmbedCacheCleanup() {
const uid = Meteor.userId();
Expand All @@ -21,10 +28,7 @@ Meteor.methods<ServerMethods>({
});
}

const date = new Date();
const expirationDays = settings.get<number>('API_EmbedCacheExpirationDays');
date.setDate(date.getDate() - expirationDays);
await OEmbedCache.removeAfterDate(date);
await executeClearOEmbedCache();
return {
message: 'cache_cleared',
};
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/models/raw/OEmbedCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class OEmbedCacheRaw extends BaseRaw<IOEmbedCache> implements IOEmbedCach
return record;
}

removeAfterDate(date: Date): Promise<DeleteResult> {
removeBeforeDate(date: Date): Promise<DeleteResult> {
const query = {
updatedAt: {
$lte: date,
Expand Down
2 changes: 1 addition & 1 deletion packages/model-typings/src/models/IOEmbedCacheModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import type { IBaseModel } from './IBaseModel';
export interface IOEmbedCacheModel extends IBaseModel<IOEmbedCache> {
createWithIdAndData(_id: string, data: any): Promise<IOEmbedCache>;

removeAfterDate(date: Date): Promise<DeleteResult>;
removeBeforeDate(date: Date): Promise<DeleteResult>;
}

0 comments on commit c8ab658

Please sign in to comment.