SymphonyElectron/js/memoryMonitor.js
Kiran Niranjan e6d16241f8 ELECTRON-445 (Change the memory refresh logic to auto-refresh after 4Hrs of user inactivity) (#415)
- Change the memory refresh logic to auto refresh after 4Hrs
2018-07-02 17:17:01 +05:30

75 lines
2.3 KiB
JavaScript

'use strict';
const log = require('./log.js');
const logLevels = require('./enums/logLevels.js');
const { getMainWindow, setIsAutoReload } = require('./windowMgr');
const systemIdleTime = require('@paulcbetts/system-idle-time');
const { getConfigField } = require('./config');
const maxMemory = 800;
let maxIdleTime = 4 * 60 * 60 * 1000;
let isInMeeting = false;
// once a minute
setInterval(gatherMemory, 1000 * 60);
/**
* Gathers system memory and logs it to the remote system
*/
function gatherMemory() {
let memory = process.getProcessMemoryInfo();
let details =
'workingSetSize: ' + memory.workingSetSize +
' peakWorkingSetSize: ' + memory.peakWorkingSetSize +
' privatesBytes: ' + memory.privatesBytes +
' sharedBytes: ' + memory.sharedBytes;
log.send(logLevels.INFO, details);
}
/**
* Method that checks memory usage every minute
* and verify if the user in inactive if so it reloads the
* application to free up some memory consumption
*
* @param memoryInfo
* @param cpuUsage
*/
function optimizeMemory(memoryInfo, cpuUsage) {
const memoryConsumed = (memoryInfo && memoryInfo.workingSetSize / 1024) || 0;
if (memoryConsumed > maxMemory
&& systemIdleTime.getIdleTime() > maxIdleTime
&& !isInMeeting
) {
getConfigField('memoryRefresh')
.then((enabled) => {
if (enabled) {
const mainWindow = getMainWindow();
if (mainWindow && !mainWindow.isDestroyed()) {
setIsAutoReload(true);
log.send(logLevels.INFO, 'Reloading the app to optimize memory usage as' +
' memory consumption was ' + memoryConsumed +
' CPU usage percentage was ' + cpuUsage.percentCPUUsage +
' user activity tick was ' + systemIdleTime.getIdleTime() +
' user was in a meeting? ' + isInMeeting );
mainWindow.reload();
}
}
});
}
}
/**
* Sets the current user meeting status
* @param meetingStatus - Whether user is in an active meeting
*/
function setIsInMeeting(meetingStatus) {
isInMeeting = meetingStatus;
}
module.exports = {
optimizeMemory,
setIsInMeeting
};