From c8ab6583dc320f5f90244965370cc6021737770e Mon Sep 17 00:00:00 2001 From: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com> Date: Mon, 22 Jan 2024 19:25:53 -0300 Subject: [PATCH] fix: Cron job for clearing OEmbed cache isn't working (#31336) --- .changeset/selfish-rice-wave.md | 6 ++++++ .../admin/settings/inputs/ActionSettingInput.tsx | 5 +++-- apps/meteor/server/cron/oembed.ts | 5 +++-- apps/meteor/server/methods/OEmbedCacheCleanup.ts | 12 ++++++++---- apps/meteor/server/models/raw/OEmbedCache.ts | 2 +- .../model-typings/src/models/IOEmbedCacheModel.ts | 2 +- 6 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 .changeset/selfish-rice-wave.md diff --git a/.changeset/selfish-rice-wave.md b/.changeset/selfish-rice-wave.md new file mode 100644 index 000000000000..3d6f1935dce2 --- /dev/null +++ b/.changeset/selfish-rice-wave.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": patch +"@rocket.chat/model-typings": patch +--- + +Fixed issue with OEmbed cache not being cleared daily diff --git a/apps/meteor/client/views/admin/settings/inputs/ActionSettingInput.tsx b/apps/meteor/client/views/admin/settings/inputs/ActionSettingInput.tsx index b674e589d446..86c76c86f502 100644 --- a/apps/meteor/client/views/admin/settings/inputs/ActionSettingInput.tsx +++ b/apps/meteor/client/views/admin/settings/inputs/ActionSettingInput.tsx @@ -19,9 +19,10 @@ function ActionSettingInput({ _id, actionText, value, disabled, sectionChanged } const handleClick = async (): Promise => { 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 }); } diff --git a/apps/meteor/server/cron/oembed.ts b/apps/meteor/server/cron/oembed.ts index 2d5cd2c41d5c..4f8655f6eff9 100644 --- a/apps/meteor/server/cron/oembed.ts +++ b/apps/meteor/server/cron/oembed.ts @@ -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 { - await cronJobs.add('Cleanup OEmbed cache', '24 2 * * *', async () => Meteor.callAsync('OEmbedCacheCleanup')); + await cronJobs.add('Cleanup OEmbed cache', '24 2 * * *', async () => executeClearOEmbedCache()); } diff --git a/apps/meteor/server/methods/OEmbedCacheCleanup.ts b/apps/meteor/server/methods/OEmbedCacheCleanup.ts index 00d937bb913e..158964125502 100644 --- a/apps/meteor/server/methods/OEmbedCacheCleanup.ts +++ b/apps/meteor/server/methods/OEmbedCacheCleanup.ts @@ -12,6 +12,13 @@ declare module '@rocket.chat/ui-contexts' { } } +export const executeClearOEmbedCache = async () => { + const date = new Date(); + const expirationDays = settings.get('API_EmbedCacheExpirationDays'); + date.setDate(date.getDate() - expirationDays); + return OEmbedCache.removeBeforeDate(date); +}; + Meteor.methods({ async OEmbedCacheCleanup() { const uid = Meteor.userId(); @@ -21,10 +28,7 @@ Meteor.methods({ }); } - const date = new Date(); - const expirationDays = settings.get('API_EmbedCacheExpirationDays'); - date.setDate(date.getDate() - expirationDays); - await OEmbedCache.removeAfterDate(date); + await executeClearOEmbedCache(); return { message: 'cache_cleared', }; diff --git a/apps/meteor/server/models/raw/OEmbedCache.ts b/apps/meteor/server/models/raw/OEmbedCache.ts index 396e2972571a..6bfab4024b4e 100644 --- a/apps/meteor/server/models/raw/OEmbedCache.ts +++ b/apps/meteor/server/models/raw/OEmbedCache.ts @@ -23,7 +23,7 @@ export class OEmbedCacheRaw extends BaseRaw implements IOEmbedCach return record; } - removeAfterDate(date: Date): Promise { + removeBeforeDate(date: Date): Promise { const query = { updatedAt: { $lte: date, diff --git a/packages/model-typings/src/models/IOEmbedCacheModel.ts b/packages/model-typings/src/models/IOEmbedCacheModel.ts index 43efc296f967..7f9fddc9b885 100644 --- a/packages/model-typings/src/models/IOEmbedCacheModel.ts +++ b/packages/model-typings/src/models/IOEmbedCacheModel.ts @@ -6,5 +6,5 @@ import type { IBaseModel } from './IBaseModel'; export interface IOEmbedCacheModel extends IBaseModel { createWithIdAndData(_id: string, data: any): Promise; - removeAfterDate(date: Date): Promise; + removeBeforeDate(date: Date): Promise; }