Skip to content

Commit

Permalink
Merge pull request Inist-CNRS#51 from Inist-CNRS/cron
Browse files Browse the repository at this point in the history
Update cron system
  • Loading branch information
parmentf authored Jul 26, 2024
2 parents 4be2569 + 8733602 commit 7c2a1c8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 38 deletions.
43 changes: 5 additions & 38 deletions tdm-be/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import dataWrapperRoute from '~/controller/data-wrapper';
import traitmentRoute from '~/controller/traitment';
import webhookRoute from '~/controller/webhook';
import environment from '~/lib/config';
import { filesLocation, initFilesSystem } from '~/lib/files';
import logger, { httpLogger, cronLogger } from '~/lib/logger';
import initCron from '~/lib/cron';
import { initFilesSystem } from '~/lib/files';
import logger, { httpLogger } from '~/lib/logger';
import swaggerFile from '~/swagger/swagger-config.json';

import cors from 'cors';
import express from 'express';
import basicAuth from 'express-basic-auth';
import rateLimit from 'express-rate-limit';
import cron from 'node-cron';
import swaggerUi from 'swagger-ui-express';

import fs from 'node:fs';
import path from 'path';

const app = express();
Expand Down Expand Up @@ -86,43 +85,11 @@ app.use((req, res) => {
});

initFilesSystem().then(() => {
initCron();

const server = app.listen(environment.port, () => {
logger.debug(`Running on ${environment.port}`);
});

server.setTimeout(600000); // 10 minutes timeout for all routes
});

cron.schedule(environment.cron.schedule, () => {
const oneWeekAgo = new Date(); // Date actuelle
oneWeekAgo.setDate(oneWeekAgo.getDate() - environment.cron.deleteFileOlderThan); // Soustrait une semaine

fs.readdir(filesLocation.upload, (err, files) => {
if (err) {
cronLogger.error('Erreur de lecture du dossier', err);
return;
}

files.forEach((file) => {
const filePath = path.join(filesLocation.upload, file);

fs.stat(filePath, (error, stats) => {
if (error) {
cronLogger.error('Erreur de récupération des informations sur le fichier', error);
return;
}

// Vérifie si le fichier est plus ancien d'une semaine
if (stats.isFile() && stats.mtime < oneWeekAgo) {
fs.unlink(filePath, (unlinkError) => {
if (unlinkError) {
cronLogger.error('Erreur de suppression du fichier', unlinkError);
return;
}
cronLogger.info(`Le fichier ${file} a été supprimé.`);
});
}
});
});
});
});
66 changes: 66 additions & 0 deletions tdm-be/src/lib/cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import environment from '~/lib/config';
import { customFile, filesLocation } from '~/lib/files';
import { cronLogger } from '~/lib/logger';

import cron from 'node-cron';

import fs from 'node:fs/promises';

const deleteOldFiles = async (directory: string) => {
const oneWeekAgo = new Date(); // Date actuelle
oneWeekAgo.setDate(oneWeekAgo.getDate() - environment.cron.deleteFileOlderThan);

const files = await fs.readdir(customFile(directory));

for (const file of files) {
const filePath = customFile(directory, file);
const stats = await fs.stat(filePath);

if (stats.isFile() && stats.mtime < oneWeekAgo) {
await fs.unlink(filePath);
cronLogger.info(`${filePath} has been delete`);
}
}
};

const initCron = () => {
cronLogger.debug('Creating upload files cron');
cron.schedule(environment.cron.schedule, () => {
deleteOldFiles(filesLocation.upload)
.then(() => {
cronLogger.info('Upload files cron ended successfully');
})
.catch((reason) => {
cronLogger.error('Upload files cron ended with a crash');
cronLogger.error(reason);
});
});

cronLogger.debug('Creating temporary files cron');
cron.schedule(environment.cron.schedule, () => {
deleteOldFiles(filesLocation.tmp)
.then(() => {
cronLogger.info('Temporary files cron ended successfully');
})
.catch((reason) => {
cronLogger.error('Temporary files cron ended with a crash');
cronLogger.error(reason);
});
});

cronLogger.debug('Creating download files cron');
cron.schedule(environment.cron.schedule, () => {
deleteOldFiles(filesLocation.download)
.then(() => {
cronLogger.info('Download files cron ended successfully');
})
.catch((reason) => {
cronLogger.error('Download files cron ended with a crash');
cronLogger.error(reason);
});
});

cronLogger.info('Cron initialized');
};

export default initCron;
4 changes: 4 additions & 0 deletions tdm-be/src/lib/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const readDir = async (directory: string) => {
);
};

export const customFile = (...paths: string[]): string => {
return path.join(filesLocation.app, ...paths);
};

export const randomFileName = (): string => {
return md5(`${Date.now()}-${Math.round(Math.random() * 1e9)}`);
};
Expand Down

0 comments on commit 7c2a1c8

Please sign in to comment.