Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
3.8 kB
2
Indexable
Never
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.trimLogs = exports.LogTask = void 0;
const common_1 = require("@nestjs/common");
const schedule_1 = require("@nestjs/schedule");
const App_1 = require("../service/configuration/App");
const fs = require("fs");
const readline = require("readline");
const logspath = App_1.App.LOGS_PATH;
const trim_limit = 10000;
const directory_level_limit = 3;
let LogTask = class LogTask {
    constructor() { }
    _trimLogs() {
        trimLogs();
    }
};
exports.LogTask = LogTask;
__decorate([
    (0, schedule_1.Cron)(schedule_1.CronExpression.EVERY_5_MINUTES),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], LogTask.prototype, "_trimLogs", null);
exports.LogTask = LogTask = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [])
], LogTask);
function trimLogs(directory = logspath, level = 0) {
    if (level > directory_level_limit) {
        console.warn("There are some directories in logs/ that will not be trimmed due to directory_level_limit being set too low. This should't happen naturally.");
        console.warn(directory);
        return;
    }
    const lines = fs.readdirSync(directory);
    for (const line of lines) {
        let absolute_path = directory + '/' + line;
        let file_or_directory = fs.statSync(absolute_path);
        if (file_or_directory.isFile() && absolute_path.endsWith('.log')) {
            trimFile(absolute_path, trim_limit);
        }
        else if (file_or_directory.isDirectory()) {
            trimLogs(absolute_path, level + 1);
        }
        else if (file_or_directory.isFile() && !absolute_path.endsWith('.log')) {
        }
        else if (file_or_directory.isSymbolicLink()) {
        }
        else {
            console.error("Unable to determine path: " + absolute_path);
        }
    }
}
exports.trimLogs = trimLogs;
function trimFile(filepath, limit) {
    const fileDescriptor = fs.openSync(filepath, 'r+');
    const linesBuffer = [];
    const buffer = [];
    const readlineMachine = readline.createInterface({
        input: fs.createReadStream(filepath),
        crlfDelay: Infinity,
    });
    readlineMachine.on('line', (line) => {
        linesBuffer.push(line);
        if (linesBuffer.length > limit) {
            linesBuffer.shift();
        }
    });
    readlineMachine.on('close', () => {
        const trimmedContent = linesBuffer.join('\n') + '\n';
        const trimmedBuffer = Buffer.from(trimmedContent);
        fs.ftruncateSync(fileDescriptor, trimmedBuffer.length);
        fs.writeSync(fileDescriptor, trimmedBuffer, 0, trimmedBuffer.length, 0);
        fs.closeSync(fileDescriptor);
        console.log(`Trimmed file: ${filepath}`);
    });
    readlineMachine.on('error', (err) => {
        console.error('Error reading file:', err);
        fs.closeSync(fileDescriptor);
    });
}
//# sourceMappingURL=log.task.js.map