fix: history cron job breaking on mysql (#21616)

* fix: history cron job breaking on mysql

* fix: backend unit test

* fix: use js date instead of string
This commit is contained in:
Rémi de Juvigny 2024-10-08 12:04:23 +02:00 committed by GitHub
parent 840550dc97
commit 80bde34977
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 8 deletions

View File

@ -97,6 +97,6 @@ describe('history lifecycles service', () => {
await lifecyclesService.bootstrap();
expect(mockScheduleJob).toHaveBeenCalledTimes(1);
expect(mockScheduleJob).toHaveBeenCalledWith('0 0 * * *', expect.any(Function));
expect(mockScheduleJob).toHaveBeenCalledWith('historyDaily', '0 0 * * *', expect.any(Function));
});
});

View File

@ -172,17 +172,24 @@ const createLifecyclesService = ({ strapi }: { strapi: Core.Strapi }) => {
});
// Schedule a job to delete expired history versions every day at midnight
state.deleteExpiredJob = scheduleJob('0 0 * * *', () => {
state.deleteExpiredJob = scheduleJob('historyDaily', '0 0 * * *', () => {
const retentionDaysInMilliseconds = serviceUtils.getRetentionDays() * 24 * 60 * 60 * 1000;
const expirationDate = new Date(Date.now() - retentionDaysInMilliseconds);
strapi.db.query(HISTORY_VERSION_UID).deleteMany({
where: {
created_at: {
$lt: expirationDate.toISOString(),
strapi.db
.query(HISTORY_VERSION_UID)
.deleteMany({
where: {
created_at: {
$lt: expirationDate,
},
},
},
});
})
.catch((error) => {
if (error instanceof Error) {
strapi.log.error('Error deleting expired history versions', error.message);
}
});
});
state.isInitialized = true;