Skip to content

Commit

Permalink
#349 - Added a "danger" setting to disable the cache killswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed Jun 27, 2024
1 parent 87d2085 commit 3611884
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
17 changes: 12 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ export default class OmnisearchPlugin extends Plugin {
})
)

this.refreshIndexCallback = this.notesIndexer.refreshIndex.bind(this.notesIndexer)
this.refreshIndexCallback = this.notesIndexer.refreshIndex.bind(
this.notesIndexer
)
addEventListener('blur', this.refreshIndexCallback)
removeEventListener

Expand Down Expand Up @@ -263,15 +265,20 @@ export default class OmnisearchPlugin extends Plugin {
indexingStep.set(IndexingStepType.WritingCache)

// Disable settings.useCache while writing the cache, in case it freezes
this.settings.useCache = false
await saveSettings(this)
const cacheEnabled = this.settings.useCache
if (cacheEnabled && !this.settings.DANGER_forceSaveCache) {
this.settings.useCache = false
await saveSettings(this)
}

// Write the cache
await searchEngine.writeToCache()

// Re-enable settings.caching
this.settings.useCache = true
await saveSettings(this)
if (cacheEnabled) {
this.settings.useCache = true
await saveSettings(this)
}
}

console.timeEnd('Omnisearch - Indexing total time')
Expand Down
28 changes: 23 additions & 5 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { writable } from 'svelte/store'
import { K_DISABLE_OMNISEARCH } from './globals'
import type OmnisearchPlugin from './main'
import { enablePrintDebug } from "./tools/utils";
import { enablePrintDebug } from './tools/utils'

interface WeightingSettings {
weightBasename: number
Expand Down Expand Up @@ -71,6 +71,7 @@ export interface OmnisearchSettings extends WeightingSettings {
httpApiNotice: boolean

DANGER_httpHost: string | null
DANGER_forceSaveCache: boolean
}

/**
Expand Down Expand Up @@ -139,9 +140,7 @@ export class SettingsTab extends PluginSettingTab {
span.innerHTML = `Omnisearch will use Text Extractor to index the content of your PDFs.`
})
new Setting(containerEl)
.setName(
`PDFs content indexing ${textExtractor ? '' : '⚠️ Disabled'}`
)
.setName(`PDFs content indexing ${textExtractor ? '' : '⚠️ Disabled'}`)
.setDesc(indexPDFsDesc)
.addToggle(toggle =>
toggle.setValue(settings.PDFIndexing).onChange(async v => {
Expand Down Expand Up @@ -663,7 +662,7 @@ export class SettingsTab extends PluginSettingTab {
})
)

new Setting(containerEl)
new Setting(containerEl)
.setName('Ignore Arabic diacritics (beta)')
.setDesc(diacriticsDesc)
.addToggle(toggle =>
Expand Down Expand Up @@ -695,6 +694,23 @@ export class SettingsTab extends PluginSettingTab {
})
)

// Force save cache
const forceSaveCacheDesc = new DocumentFragment()
forceSaveCacheDesc.createSpan({}, span => {
span.innerHTML = `Omnisearch has a security feature that automatically disables cache writing if it cannot fully perform the operation.<br>
Use this option to force the cache to be saved, even if it causes a crash.<br>
⚠️ <span style="color: var(--text-accent)">Enabling this setting could lead to crash loops</span>`
})
new Setting(containerEl)
.setName('Force save the cache')
.setDesc(forceSaveCacheDesc)
.addToggle(toggle =>
toggle.setValue(settings.DANGER_forceSaveCache).onChange(async v => {
settings.DANGER_forceSaveCache = v
await saveSettings(this.plugin)
})
)

// Clear cache data
if (isCacheEnabled()) {
const resetCacheDesc = new DocumentFragment()
Expand All @@ -713,6 +729,7 @@ export class SettingsTab extends PluginSettingTab {
})
})
}

//#endregion Danger Zone
}

Expand Down Expand Up @@ -769,6 +786,7 @@ export function getDefaultSettings(app: App): OmnisearchSettings {
verboseLogging: false,

DANGER_httpHost: null,
DANGER_forceSaveCache: false,
}
}

Expand Down

0 comments on commit 3611884

Please sign in to comment.