Commit 2f724c37cca3bd9991f130a58bf73773f807b470

Authored by adi surya
1 parent 046fb821ea
Exists in master

remove old logs

Showing 6 changed files with 102 additions and 1 deletions Side-by-side Diff

1 1 node_modules
2 2 config.json
3 3 pid.txt
4   -logs
5 4 \ No newline at end of file
  5 +/logs
6 6 \ No newline at end of file
... ... @@ -0,0 +1,19 @@
  1 +const MODULE_NAME = 'CRON.INDEX';
  2 +const logger = require('tektrans-logger');
  3 +const uniqid = require('uniqid');
  4 +const { CronJob } = require('cron');
  5 +
  6 +const removeOldLogs = require('../logs/remove');
  7 +
  8 +// eslint-disable-next-line no-unused-vars
  9 +const everyDay = new CronJob(
  10 + '0 0 0 * * *',
  11 + () => {
  12 + const xid = uniqid();
  13 + logger.verbose(`${MODULE_NAME} E5B4260C: run every day at 00:00`, { xid });
  14 + removeOldLogs(xid);
  15 + },
  16 + null,
  17 + true,
  18 + 'Asia/Jakarta',
  19 +);
... ... @@ -0,0 +1 @@
  1 +exports.remove = require('./remove');
... ... @@ -0,0 +1,48 @@
  1 +const fs = require('fs').promises;
  2 +const path = require('path');
  3 +const moment = require('moment');
  4 +const logger = require('tektrans-logger');
  5 +
  6 +const MODULE_NAME = 'LOGS.REMOVE';
  7 +const MAX_OLD_LOG_FILE_D = 14;
  8 +const APP_DIRECTORY = path.dirname(require.main.filename);
  9 +const LOG_DIRECTORY = `${APP_DIRECTORY}/logs`;
  10 +
  11 +/**
  12 + * remove old log file
  13 + *
  14 + * @param xid
  15 + */
  16 +module.exports = async (xid) => {
  17 + try {
  18 + const files = await fs.readdir(LOG_DIRECTORY);
  19 + files.forEach(async (file) => {
  20 + try {
  21 + const filepath = `${LOG_DIRECTORY}/${file}`;
  22 + const stat = await fs.stat(filepath);
  23 + const now = moment();
  24 + const endtime = moment(stat.ctime).add(MAX_OLD_LOG_FILE_D, 'days');
  25 + if (now > endtime) {
  26 + logger.verbose(`${MODULE_NAME} 7F8B9696: Remove old log file`, {
  27 + xid,
  28 + file,
  29 + ctime: stat.ctime || null,
  30 + filepath,
  31 + });
  32 +
  33 + await fs.unlink(filepath);
  34 + }
  35 + } catch (e) {
  36 + logger.error(`${MODULE_NAME} 3CD3E8E1: Exception`, {
  37 + xid,
  38 + file,
  39 + eMessage: e.message,
  40 + });
  41 + }
  42 + });
  43 + } catch (e) {
  44 + logger.error(`${MODULE_NAME} A34F605F: Exception`, {
  45 + xid, eMessage: e.message,
  46 + });
  47 + }
  48 +};
... ... @@ -10,7 +10,9 @@
10 10 "license": "SEE LICENSE IN license.txt",
11 11 "dependencies": {
12 12 "axios": "^0.27.2",
  13 + "cron": "^2.1.0",
13 14 "express": "^4.18.1",
  15 + "moment": "^2.29.4",
14 16 "tektrans-logger": "^1.2.4",
15 17 "uniqid": "^5.4.0"
16 18 },
... ... @@ -453,6 +455,14 @@
453 455 "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
454 456 "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
455 457 },
  458 + "node_modules/cron": {
  459 + "version": "2.1.0",
  460 + "resolved": "https://registry.npmjs.org/cron/-/cron-2.1.0.tgz",
  461 + "integrity": "sha512-Hq7u3P8y7UWYvsZbSKHHJDVG0VO9O7tp2qljxzTScelcTODBfCme8AIhnZsFwmQ9NchZ3hr2uNr+s3DSms7q6w==",
  462 + "dependencies": {
  463 + "luxon": "^1.23.x"
  464 + }
  465 + },
456 466 "node_modules/cross-spawn": {
457 467 "version": "7.0.3",
458 468 "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
... ... @@ -1720,6 +1730,14 @@
1720 1730 "triple-beam": "^1.3.0"
1721 1731 }
1722 1732 },
  1733 + "node_modules/luxon": {
  1734 + "version": "1.28.0",
  1735 + "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.0.tgz",
  1736 + "integrity": "sha512-TfTiyvZhwBYM/7QdAVDh+7dBTBA29v4ik0Ce9zda3Mnf8on1S5KJI8P2jKFZ8+5C0jhmr0KwJEO/Wdpm0VeWJQ==",
  1737 + "engines": {
  1738 + "node": "*"
  1739 + }
  1740 + },
1723 1741 "node_modules/media-typer": {
1724 1742 "version": "0.3.0",
1725 1743 "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
... ... @@ -3082,6 +3100,14 @@
3082 3100 "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
3083 3101 "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
3084 3102 },
  3103 + "cron": {
  3104 + "version": "2.1.0",
  3105 + "resolved": "https://registry.npmjs.org/cron/-/cron-2.1.0.tgz",
  3106 + "integrity": "sha512-Hq7u3P8y7UWYvsZbSKHHJDVG0VO9O7tp2qljxzTScelcTODBfCme8AIhnZsFwmQ9NchZ3hr2uNr+s3DSms7q6w==",
  3107 + "requires": {
  3108 + "luxon": "^1.23.x"
  3109 + }
  3110 + },
3085 3111 "cross-spawn": {
3086 3112 "version": "7.0.3",
3087 3113 "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
... ... @@ -4040,6 +4066,11 @@
4040 4066 "triple-beam": "^1.3.0"
4041 4067 }
4042 4068 },
  4069 + "luxon": {
  4070 + "version": "1.28.0",
  4071 + "resolved": "https://registry.npmjs.org/luxon/-/luxon-1.28.0.tgz",
  4072 + "integrity": "sha512-TfTiyvZhwBYM/7QdAVDh+7dBTBA29v4ik0Ce9zda3Mnf8on1S5KJI8P2jKFZ8+5C0jhmr0KwJEO/Wdpm0VeWJQ=="
  4073 + },
4043 4074 "media-typer": {
4044 4075 "version": "0.3.0",
4045 4076 "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
... ... @@ -25,7 +25,9 @@
25 25 },
26 26 "dependencies": {
27 27 "axios": "^0.27.2",
  28 + "cron": "^2.1.0",
28 29 "express": "^4.18.1",
  30 + "moment": "^2.29.4",
29 31 "tektrans-logger": "^1.2.4",
30 32 "uniqid": "^5.4.0"
31 33 }