Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented dynamic deletion of logs with different names #99

Open
wants to merge 1 commit into
base: experimental
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,52 @@ function get_limit_size() {

function delete_old(file) {
if (file === "/dev/null") return;
var fileBaseName = file.substr(0, file.length - 4).split('/').pop() + "__";
var dirName = path.dirname(file);
var fileBaseNames = [];

fs.readdir(dirName, function(err, files) {
var i, len;
if (err) return pmx.notify(err);

var rotated_files = [];
for (i = 0, len = files.length; i < len; i++) {
if (files[i].indexOf(fileBaseName) >= 0)
rotated_files.push(files[i]);
var x, length, currentFileName, fileNameRegex;
for (x = 0, length = files.length; x < length; x++ ) {
currentFileName = files[x];

if (currentFileName.indexOf('__') >= 0) {
fileNameRegex = currentFileName.split('__')[0];
} else {
fileNameRegex = currentFileName.substr(0, currentFileName.length - 4);
}

if (!fileBaseNames.includes(fileNameRegex)) {
fileBaseNames.push(fileNameRegex);
}
}
rotated_files.sort().reverse();

for (i = rotated_files.length - 1; i >= RETAIN; i--) {
(function(i) {
fs.unlink(path.resolve(dirName, rotated_files[i]), function (err) {
if (err) return console.error(err);
console.log('"' + rotated_files[i] + '" has been deleted');
});
})(i);
var y, fileBaseNamesLength;
var rotated_files, fileBaseName;

for (y = 0, fileBaseNamesLength = fileBaseNames.length; y < fileBaseNamesLength; y++) {
rotated_files = [];
fileBaseName = fileBaseNames[y];

for (i = 0, len = files.length; i < len; i++) {
if (files[i].indexOf(fileBaseName) >= 0 && files[i].indexOf('__') >= 0) {
rotated_files.push(files[i]);
}
}

rotated_files.sort().reverse();

for (i = rotated_files.length - 1; i >= RETAIN; i--) {
console.log("Deleting : ", rotated_files[i]);
(function(i) {
fs.unlink(path.resolve(dirName, rotated_files[i]), function (err) {
if (err) return console.error(err);
console.log('"' + rotated_files[i] + '" has been deleted');
});
})(i);
}
}
});
}
Expand Down