ELECTRON-788: add clear cache functionality (#506)

* ELECTRON-788: clean cache on bad exits

A few customers have reported that the cache gets corrupted when the app is not exited cleanly. And, upon the next start, we get cache_read_failures.

This fix adds an extra check during startup and exit to check if the app was exited cleanly and deletes the corrupted cache folder accordingly during startup.

* ELECTRON-788: add menu item, use electron session cache clear logic

Adds a new menu item to clear cache and reload the app.
Also remove the old logic of deleting the cache folder manually and use electron session api to clear the cache.

* ELECTRON-788: refactor code
This commit is contained in:
Vishwas Shashidhar 2018-09-26 13:08:59 +05:30 committed by GitHub
parent d1009a2326
commit 628b05d46b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 84 additions and 9 deletions

39
js/cacheHandler/index.js Normal file
View File

@ -0,0 +1,39 @@
const fs = require('fs');
const nodePath = require('path');
const electron = require('electron');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
const cacheCheckFilename = 'CacheCheck';
const cacheCheckFilePath = nodePath.join(electron.app.getPath('userData'), cacheCheckFilename);
function handleCacheFailureCheckOnStartup() {
return new Promise((resolve) => {
if (fs.existsSync(cacheCheckFilePath)) {
log.send(logLevels.INFO, `Cache check file exists, so not clearing cache!`);
fs.unlinkSync(cacheCheckFilePath);
resolve();
} else {
log.send(logLevels.INFO, `Cache check file does not exist, we are clearing the cache!`);
electron.session.defaultSession.clearCache(() => {
log.send(logLevels.INFO, `Cleared cache!`);
resolve();
});
}
});
}
function handleCacheFailureCheckOnExit() {
log.send(logLevels.INFO, `Clean exit! Creating cache check file!`);
fs.writeFileSync(cacheCheckFilePath, "");
}
module.exports = {
handleCacheFailureCheckOnStartup: handleCacheFailureCheckOnStartup,
handleCacheFailureCheckOnExit: handleCacheFailureCheckOnExit
};

View File

@ -25,6 +25,7 @@ const getCmdLineArg = require('./utils/getCmdLineArg.js');
const log = require('./log.js');
const logLevels = require('./enums/logLevels.js');
const autoLaunch = require('./autoLaunch');
const { handleCacheFailureCheckOnStartup, handleCacheFailureCheckOnExit} = require('./cacheHandler');
require('electron-dl')();
@ -180,15 +181,30 @@ setChromeFlags();
* Some APIs can only be used after this event occurs.
*/
app.on('ready', () => {
electron.powerMonitor.on('lock-screen', () => {
eventEmitter.emit('sys-locked');
});
electron.powerMonitor.on('unlock-screen', () => {
eventEmitter.emit('sys-unlocked');
});
checkFirstTimeLaunch()
.then(readConfigThenOpenMainWindow)
.catch(readConfigThenOpenMainWindow);
handleCacheFailureCheckOnStartup()
.then(() => {
initiateApp();
})
.catch((err) => {
log.send(logLevels.INFO, `Couldn't clear cache and refresh -> ${err}`);
initiateApp();
});
function initiateApp() {
electron.powerMonitor.on('lock-screen', () => {
eventEmitter.emit('sys-locked');
});
electron.powerMonitor.on('unlock-screen', () => {
eventEmitter.emit('sys-unlocked');
});
checkFirstTimeLaunch()
.then(readConfigThenOpenMainWindow)
.catch(readConfigThenOpenMainWindow);
}
});
/**
@ -199,6 +215,10 @@ app.on('window-all-closed', function () {
app.quit();
});
app.on('quit', function () {
handleCacheFailureCheckOnExit();
});
/**
* Is triggered when the app is up & running
*/

View File

@ -375,6 +375,18 @@ function getTemplate(app) {
}
});
// Window - View menu -> Clear Cache
template[index].submenu.push({
label: i18n.getMessageFor('Clear cache and Reload'),
click: function (item, focusedWindow) {
if (focusedWindow && !focusedWindow.isDestroyed()) {
electron.session.defaultSession.clearCache(() => {
focusedWindow.reload();
});
}
}
});
if (!isMac) {
/* eslint-disable no-param-reassign */
template[index].submenu.push({

View File

@ -89,6 +89,7 @@
"Quit Symphony": "Quit Symphony",
"Redo": "Redo",
"Refresh app when idle": "Refresh app when idle",
"Clear cache and Reload": "Clear cache and Reload",
"Relaunch Application": "Relaunch Application",
"Relaunch": "Relaunch",
"Reload": "Reload",

View File

@ -87,6 +87,7 @@
"Quit Symphony": "Quit Symphony",
"Redo": "Redo",
"Refresh app when idle": "Refresh app when idle",
"Clear cache and Reload": "Clear cache and Reload",
"Relaunch Application": "Relaunch Application",
"Relaunch": "Relaunch",
"Reload": "Reload",

View File

@ -89,6 +89,7 @@
"Quit Symphony": "Symphonyを終了",
"Redo": "やり直し",
"Refresh app when idle": "アイドル時にアプリを再表示",
"Clear cache and Reload": "キャッシュをクリアしてリロードする",
"Relaunch Application": "アプリケーションの再起動",
"Relaunch": "「リスタート」",
"Reload": "再読み込み",

View File

@ -87,6 +87,7 @@
"Quit Symphony": "Symphonyを終了",
"Redo": "やり直し",
"Refresh app when idle": "アイドル時にアプリを再表示",
"Clear cache and Reload": "キャッシュをクリアしてリロードする",
"Relaunch Application": "アプリケーションの再起動",
"Relaunch": "「リスタート」",
"Reload": "再読み込み",