import * as electron from 'electron' ; import { isMac } from '../common/env' ; import { logger } from '../common/logger' ; import { CloudConfigDataTypes , config } from './config-handler' ; import { windowHandler } from './window-handler' ; import { windowExists } from './window-utils' ; class MemoryMonitor { private memoryInfo : Electron . ProcessMemoryInfo | undefined = undefined ; private isInMeeting : boolean ; private canReload : boolean ; private lastReloadTime ? : number ; private memoryThreshold : number ; private readonly maxIdleTime : number ; private readonly memoryRefreshThreshold : number ; constructor ( ) { this . isInMeeting = false ; this . canReload = true ; this . maxIdleTime = 4 * 60 * 60 * 1000 ; this . memoryThreshold = 800 * 1024 ; this . memoryRefreshThreshold = 24 * 60 * 60 * 1000 ; } param memoryInfo { Electron . ProcessMemoryInfo } public setMemoryInfo ( memoryInfo ) { this . memoryInfo = memoryInfo ; logger . info ( `memory-monitor: setting memory info to ${ JSON . stringify ( memoryInfo ) }` ) ; this . validateMemory ( ) ; } param isInMeeting { boolean } public setMeetingStatus ( isInMeeting ) { this . isInMeeting = isInMeeting ; logger . info ( `memory-monitor: setting meeting status to ${ isInMeeting }` ) ; } param memoryThreshold public setMemoryThreshold ( memoryThreshold ) { this . memoryThreshold = memoryThreshold * 1024 ; } /**
     * Validates the predefined conditions and refreshes the client
     */ private validateMemory ( ) { logger . info ( 'memory-monitor: validating memory refresh conditions' ) ; const { memoryRefresh } = config . getConfigFields ( [ 'memoryRefresh' ] ) ; if ( memoryRefresh !== CloudConfigDataTypes . ENABLED ) { logger . info ( 'memory-monitor: memory reload is disabled in the config, not going to refresh!' ) ; return ; } const time = electron . powerMonitor . getSystemIdleTime ( ) ; const idleTime = time * 1000 ; const memoryConsumption = isMac ? this . memoryInfo && this . memoryInfo . private : this . memoryInfo && this . memoryInfo . residentSet ; logger . info ( 'memory-monitor: Checking different conditions to see if we should auto reload the app' ) ; logger . info ( 'memory-monitor: Is in meeting: ' , this . isInMeeting ) ; logger . info ( 'memory-monitor: Is Network online: ' , windowHandler . isOnline ) ; logger . info ( 'memory-monitor: Memory consumption: ' , memoryConsumption ) ; logger . info ( 'memory-monitor: Idle Time: ' , idleTime ) ; logger . info ( 'memory-monitor: Last Reload time: ' , this . lastReloadTime ) ; if ( this . isInMeeting ) { logger . info ( 'memory-monitor: NOT RELOADING -> User is currently in a meeting. Meeting status from client: ' , this . isInMeeting ) ; return ; } if ( ! windowHandler . isOnline ) { logger . info ( 'memory-monitor: NOT RELOADING -> Not connected to network. Network status: ' , windowHandler . isOnline ) ; return ; } if ( ! ( memoryConsumption && memoryConsumption > this . memoryThreshold ) ) { logger . info ( `memory-monitor: NOT RELOADING -> Memory consumption ${ memoryConsumption } is lesser than the threshold ${ this . memoryThreshold }` ) ; return ; } if ( ! ( idleTime > this . maxIdleTime ) ) { logger . info ( 'memory-monitor: NOT RELOADING -> User is not idle for: ' , idleTime ) ; return ; } if ( ! this . canReload ) { logger . info ( 'memory-monitor: NOT RELOADING -> Already refreshed at: ' , this . lastReloadTime ) ; return ; } const mainWindow = windowHandler . getMainWindow ( ) ; if ( ! ( mainWindow && windowExists ( mainWindow ) ) ) { logger . info ( "memory-monitor: NOT RELOADING -> Main window doesn't exist!" ) ; return ; } logger . info ( 'memory-monitor: RELOADING -> auto reloading the app as all the conditions are satisfied' ) ; this . reloadMainWindow ( ) ; } /***
     * Reloads main window
     */ private reloadMainWindow ( ) { const mainWebContents = windowHandler . getMainWebContents ( ) ; if ( mainWebContents && ! mainWebContents . isDestroyed ( ) ) { windowHandler . setIsAutoReload ( true ) ; this . canReload = false ; this . lastReloadTime = new Date ( ) . getTime ( ) ; setTimeout ( ( ) => { this . canReload = true ; } , this . memoryRefreshThreshold ) ; logger . info ( 'memory-monitor: auto-reload start' ) ; mainWebContents . reloadIgnoringCache ( ) ; } else { logger . error ( 'memory-monitor: Unable to reload, no main window webContents found.' ) ; } } } const memoryMonitor = new MemoryMonitor ( ) ; export { memoryMonitor } ;