fix: Cron job for clearing OEmbed cache isn't working (#31336)

pull/31337/head
Matheus Barbosa Silva 2 years ago committed by GitHub
parent ec285f7c06
commit c8ab6583dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .changeset/selfish-rice-wave.md
  2. 5
      apps/meteor/client/views/admin/settings/inputs/ActionSettingInput.tsx
  3. 5
      apps/meteor/server/cron/oembed.ts
  4. 12
      apps/meteor/server/methods/OEmbedCacheCleanup.ts
  5. 2
      apps/meteor/server/models/raw/OEmbedCache.ts
  6. 2
      packages/model-typings/src/models/IOEmbedCacheModel.ts

@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---
Fixed issue with OEmbed cache not being cleared daily

@ -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 });
}

@ -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,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();
@ -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',
};

@ -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,

@ -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>;
}

Loading…
Cancel
Save