Cleanup old log files on Linux

This commit is contained in:
Björn Dahlgren 2021-02-28 14:47:48 +01:00 committed by Björn Dahlgren
parent 0d9b6134b7
commit 1ebd0afe86

View File

@ -12,6 +12,8 @@ var gamesLogFolder = {
arma3_x64: 'Arma 3'
}
var numberOfLogsToKeep = 20
var Logs = function (config) {
this.config = config
@ -29,18 +31,16 @@ Logs.generateLogFileName = function (suffix) {
}
Logs.prototype.delete = function (filename, callback) {
callback = callback || function () {}
this.getLogFile(filename, function (err, logFile) {
if (err) {
if (callback) {
return callback(err)
}
return callback(err)
} else {
if (logFile && logFile.path) {
fs.unlink(logFile.path, callback)
} else {
if (callback) {
return callback(new Error('File not found'))
}
return callback(new Error('File not found'))
}
}
})
@ -141,6 +141,10 @@ Logs.prototype.logServerProcesses = function (serverProcess, headlessClientProce
headlessClientProcesses.forEach(function (headlessClientProcess, idx) {
self.logServerProcess(headlessClientProcess, 'hc_' + (idx + 1))
})
if (this.config.type === 'linux') {
this.cleanupOldLogFiles()
}
}
Logs.prototype.logServerProcess = function (serverProcess, suffix) {
@ -178,4 +182,19 @@ Logs.prototype.logServerProcess = function (serverProcess, suffix) {
})
}
Logs.prototype.cleanupOldLogFiles = function () {
var self = this
self.logFiles(function (err, files) {
if (err) {
return
}
var oldLogFiles = files.slice(numberOfLogsToKeep)
oldLogFiles.forEach(function (logFile) {
self.delete(logFile.name)
})
})
}
module.exports = Logs