Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable/enable gc via settings menu #1740

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"launchOnStartup": "Launch at Login",
"openWebUIAtLaunch": "Open Web UI at Launch",
"pubsub": "Enable PubSub",
"automaticGC": "Automatic Garbage Collection",
"ipfsCommandLineTools": "Command Line Tools",
"takeScreenshotShortcut": "Global Screenshot Shortcut",
"downloadHashShortcut": "Global Download Shortcut",
Expand Down
54 changes: 54 additions & 0 deletions src/automatic-gc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const createToggler = require('./utils/create-toggler')
const logger = require('./common/logger')
const store = require('./common/store')
const { ipcMain } = require('electron')

const CONFIG_KEY = 'automaticGC'
const gcFlag = '--enable-gc'
const isEnabled = flags => flags.some(f => f === gcFlag)

function enable () {
const flags = store.get('ipfsConfig.flags', [])
if (!isEnabled(flags)) {
flags.push(gcFlag)
applyConfig(flags)
}
}

function disable () {
let flags = store.get('ipfsConfig.flags', [])
if (isEnabled(flags)) {
flags = flags.filter(item => item !== gcFlag) // remove flag
applyConfig(flags)
}
}

function applyConfig (newFlags) {
store.set('ipfsConfig.flags', newFlags)
ipcMain.emit('ipfsConfigChanged') // trigger node restart
}

module.exports = async function () {
const activate = ({ newValue, oldValue }) => {
if (newValue === oldValue) return

try {
if (newValue === true) {
enable()
} else {
disable()
}

return true
} catch (err) {
logger.error(`[automatic gc] ${err.toString()}`)

return false
}
}
activate({ newValue: store.get(CONFIG_KEY, true) })
createToggler(CONFIG_KEY, activate)
logger.info(`[automatic gc] ${store.get(CONFIG_KEY, true) ? 'enabled' : 'disabled'}`)
}

module.exports.CONFIG_KEY = CONFIG_KEY
11 changes: 9 additions & 2 deletions src/common/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const defaults = {
'--migrate',
'--enable-gc',
'--routing', 'dhtclient'
],
keysize: 2048
]
},
language: (electron.app || electron.remote.app).getLocale(),
experiments: {}
Expand All @@ -25,6 +24,14 @@ const migrations = {
if (flags.includes('--migrate=true') || flags.includes('--enable-gc=true')) {
store.set('ipfsConfig.flags', defaults.ipfsConfig.flags)
}
},
'>0.13.2': store => {
const flags = store.get('ipfsConfig.flags', [])
const automaticGC = store.get('automaticGC', false)
// ensure checkbox follows cli flag config
if (flags.includes('--enable-gc') && !automaticGC) {
store.set('automaticGC', true)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function writeIpfsBinaryPath (path) {
)
}

async function spawn ({ flags, path, keysize }) {
async function spawn ({ flags, path }) {
const ipfsBin = getIpfsBinPath()
writeIpfsBinaryPath(ipfsBin)

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const setupNpmOnIpfs = require('./npm-on-ipfs')
const setupDaemon = require('./daemon')
const setupWebUI = require('./webui')
const setupAutoLaunch = require('./auto-launch')
const setupAutoGc = require('./automatic-gc')
const setupPubsub = require('./enable-pubsub')
const setupDownloadCid = require('./download-cid')
const setupTakeScreenshot = require('./take-screenshot')
Expand Down Expand Up @@ -75,6 +76,7 @@ async function run () {
await Promise.all([
setupArgvFilesHandler(ctx),
setupAutoLaunch(ctx),
setupAutoGc(ctx),
setupPubsub(ctx),
setupSecondInstance(ctx),
// Setup global shortcuts
Expand Down
3 changes: 3 additions & 0 deletions src/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ const { CONFIG_KEY: SCREENSHOT_KEY, SHORTCUT: SCREENSHOT_SHORTCUT, takeScreensho
const { CONFIG_KEY: DOWNLOAD_KEY, SHORTCUT: DOWNLOAD_SHORTCUT, downloadCid } = require('./download-cid')
const { CONFIG_KEY: AUTO_LAUNCH_KEY, isSupported: supportsLaunchAtLogin } = require('./auto-launch')
const { CONFIG_KEY: PUBSUB_KEY } = require('./enable-pubsub')
const { CONFIG_KEY: AUTO_GC_KEY } = require('./automatic-gc')
const { CONFIG_KEY: IPFS_PATH_KEY } = require('./ipfs-on-path')
const { CONFIG_KEY: NPM_IPFS_KEY } = require('./npm-on-ipfs')
const { CONFIG_KEY: AUTO_LAUNCH_WEBUI_KEY } = require('./webui')

const CONFIG_KEYS = [
AUTO_LAUNCH_KEY,
AUTO_LAUNCH_WEBUI_KEY,
AUTO_GC_KEY,
IPFS_PATH_KEY,
NPM_IPFS_KEY,
SCREENSHOT_KEY,
Expand Down Expand Up @@ -124,6 +126,7 @@ function buildMenu (ctx) {
},
buildCheckbox(AUTO_LAUNCH_KEY, 'settings.launchOnStartup'),
buildCheckbox(AUTO_LAUNCH_WEBUI_KEY, 'settings.openWebUIAtLaunch'),
buildCheckbox(AUTO_GC_KEY, 'settings.automaticGC'),
buildCheckbox(IPFS_PATH_KEY, 'settings.ipfsCommandLineTools'),
buildCheckbox(SCREENSHOT_KEY, 'settings.takeScreenshotShortcut'),
buildCheckbox(DOWNLOAD_KEY, 'settings.downloadHashShortcut'),
Expand Down