Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
4.2 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 tempFilePath = filepath + '.tmp';
    const writeStream = fs.createWriteStream(tempFilePath);
    const readlineMachine = readline.createInterface({
        input: fs.createReadStream(filepath),
        crlfDelay: Infinity,
    });
    let linesCount = 0;
    let linesBuffer = [];
    readlineMachine.on('line', (line) => {
        linesBuffer.push(line);
        linesCount++;
        if (linesCount >= limit) {
            linesBuffer.shift();
        }
    });
    readlineMachine.on('close', () => {
        for (const line of linesBuffer) {
            writeStream.write(line + '\n');
        }
        writeStream.end(() => {
            fs.rename(tempFilePath, filepath, (err) => {
                if (err) {
                    console.error('Error renaming file: ' + tempFilePath
                        + ' to ' + filepath, err);
                }
                else {
                    console.log('Renamed file: ' + tempFilePath
                        + ' to ' + filepath);
                }
            });
        });
        console.log('Trimmed file: ' + filepath);
    });
    readlineMachine.on('error', (err) => {
        console.error('Error reading file:' + filepath, err);
        readlineMachine.close();
        writeStream.end();
    });
    writeStream.on('error', (err) => {
        console.error('Error writing file:' + filepath, err);
        readlineMachine.close();
        writeStream.end();
    });
}
//# sourceMappingURL=log.task.js.map