Merge pull request #360 from KiranNiranjan/ELECTRON-445

Electron-445 (Added a new API to keep electron in sync with client app user meeting status)
This commit is contained in:
Vikas Shashidhar 2018-06-06 13:48:03 +05:30 committed by GitHub
commit 0a5c5ab193
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 8 deletions

View File

@ -20,7 +20,8 @@ const cmds = keyMirror({
bringToFront: null,
openScreenPickerWindow: null,
popupMenu: null,
optimizeMemoryConsumption: null
optimizeMemoryConsumption: null,
setIsInMeeting: null
});
module.exports = {

View File

@ -17,6 +17,7 @@ const { bringToFront } = require('./bringToFront.js');
const eventEmitter = require('./eventEmitter');
const { isMac } = require('./utils/misc');
const { openScreenPickerWindow } = require('./desktopCapturer');
const { optimizeMemory, setIsInMeeting } = require('./memoryMonitor');
const apiEnums = require('./enums/api.js');
const apiCmds = apiEnums.cmds;
@ -147,6 +148,16 @@ electron.ipcMain.on(apiName, (event, arg) => {
windowMgr.getMenu().popup(browserWin, { x: 20, y: 15, async: true });
}
break;
case apiCmds.optimizeMemoryConsumption:
if (typeof arg.memory === 'object' && typeof arg.cpuUsage === 'object' && typeof arg.memory.workingSetSize === 'number') {
optimizeMemory(arg.memory, arg.cpuUsage);
}
break;
case apiCmds.setIsInMeeting:
if (typeof arg.isInMeeting === 'boolean') {
setIsInMeeting(arg.isInMeeting);
}
break;
default:
}

View File

@ -2,14 +2,16 @@
const log = require('./log.js');
const logLevels = require('./enums/logLevels.js');
const { getMainWindow } = require('./windowMgr');
const { getMainWindow, setIsAutoReload } = require('./windowMgr');
const systemIdleTime = require('@paulcbetts/system-idle-time');
const { getConfigField } = require('./config');
const maxMemory = 800;
let maxIdleTime = 4 * 60 * 1000;
let reloadThreshold = 30 * 60 * 1000;
let maxIdleTime = 15 * 60 * 1000;
let reloadThreshold = 60 * 60 * 1000;
let reloadedTimeStamp;
let isInMeeting = false;
// once a minute
setInterval(gatherMemory, 1000 * 60);
@ -33,26 +35,47 @@ function gatherMemory() {
* application to free up some memory consumption
*
* @param memoryInfo
* @param cpuUsage
*/
function optimizeMemory(memoryInfo) {
function optimizeMemory(memoryInfo, cpuUsage) {
const memoryConsumed = (memoryInfo && memoryInfo.workingSetSize / 1024) || 0;
const canReload = (!reloadedTimeStamp || (new Date().getTime() - reloadedTimeStamp) > reloadThreshold);
if (memoryConsumed > maxMemory && systemIdleTime.getIdleTime() > maxIdleTime && canReload) {
if (memoryConsumed > maxMemory
&& systemIdleTime.getIdleTime() > maxIdleTime
&& canReload
&& !isInMeeting
&& cpuUsage.percentCPUUsage < 1
) {
getConfigField('memoryRefresh')
.then((enabled) => {
if (enabled) {
const mainWindow = getMainWindow();
if (mainWindow && !mainWindow.isDestroyed()) {
setIsAutoReload(true);
reloadedTimeStamp = new Date().getTime();
log.send(logLevels.INFO, 'Reloading the app to optimize memory usage');
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
optimizeMemory,
setIsInMeeting
};

View File

@ -64,6 +64,24 @@ const throttledSetBadgeCount = throttle(1000, function(count) {
});
});
const throttledSetIsInMeetingStatus = throttle(1000, function(isInMeeting) {
local.ipcRenderer.send(apiName, {
cmd: apiCmds.setIsInMeeting,
isInMeeting
});
});
// Gathers renderer process memory
setInterval(() => {
const memory = process.getProcessMemoryInfo();
const cpuUsage = process.getCPUUsage();
local.ipcRenderer.send(apiName, {
cmd: apiCmds.optimizeMemoryConsumption,
memory: memory,
cpuUsage: cpuUsage
});
}, 1000 * 60 * 15);
createAPI();
// creates API exposed from electron.
@ -277,6 +295,14 @@ function createAPI() {
cmd: apiCmds.showNotificationSettings,
windowName: windowName
});
},
/**
* Sets if the user is in an active meeting
* will be used to handle memory refresh functionality
*/
setIsInMeeting: function (isInMeeting) {
throttledSetIsInMeetingStatus(isInMeeting);
}
};