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(JCEF): clean outdated library directories #5663

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,37 +109,39 @@ class JcefBrowser : IBrowser, EventListener {
}

/**
* Cleans up old cache directories.
* Cleans up old cache and library directories.
*
* TODO: Check if we have an active PID using the cache directory, if so, check if the LiquidBounce
* process attached to the JCEF PID is still running or not. If not, we could kill the JCEF process
* and clean up the cache directory.
*/
fun cleanup() {
if (cacheFolder.exists()) {
runCatching {
cacheFolder.listFiles()
?.filter { file ->
file.isDirectory && System.currentTimeMillis() - file.lastModified() > CACHE_CLEANUP_THRESHOLD
}
?.sumOf { file ->
try {
val fileSize = file.walkTopDown().sumOf { uFile -> uFile.length() }
file.deleteRecursively()
fileSize
} catch (e: Exception) {
logger.error("Failed to clean up old cache directory", e)
0
}
} ?: 0
}.onFailure {
// Not a big deal, not fatal.
logger.error("Failed to clean up old JCEF cache directories", it)
}.onSuccess { size ->
if (size > 0) {
logger.info("Cleaned up ${size.formatBytesAsSize()} JCEF cache directories")
runCatching {
val currentLibraryDirectory = MCEF.INSTANCE.resourceManager?.commitDirectory

buildList {
cacheFolder.listFiles()?.let(::addAll)
librariesFolder.listFiles()?.let(::addAll)
}.filter { file ->
file != currentLibraryDirectory && file.isDirectory
&& System.currentTimeMillis() - file.lastModified() > CACHE_CLEANUP_THRESHOLD
}.sumOf { file ->
try {
val fileSize = file.walkTopDown().sumOf { uFile -> uFile.length() }
file.deleteRecursively()
fileSize
} catch (e: Exception) {
logger.error("Failed to clean up old cache directory", e)
0
}
}
}.onFailure {
// Not a big deal, not fatal.
logger.error("Failed to clean up old JCEF cache directories", it)
}.onSuccess { size ->
if (size > 0) {
logger.info("Cleaned up ${size.formatBytesAsSize()} JCEF cache directories")
}
}
}

Expand Down
Loading