Make engine more resilient against init crashes

This commit is contained in:
JeremyStar™ 2024-12-15 13:50:36 +01:00
parent 6f998f4e33
commit 1fc337d9b8
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 61 additions and 15 deletions

View file

@ -20,6 +20,7 @@
package de.staropensource.engine.base package de.staropensource.engine.base
import de.staropensource.engine.base.exception.EngineInitializationFailureException
import de.staropensource.engine.base.utility.Environment import de.staropensource.engine.base.utility.Environment
import de.staropensource.engine.base.utility.FileAccess import de.staropensource.engine.base.utility.FileAccess
import de.staropensource.engine.logging.Logger import de.staropensource.engine.logging.Logger
@ -78,31 +79,38 @@ class Engine private constructor() {
* and generally performs actions which * and generally performs actions which
* should only be executed once. * should only be executed once.
* *
* @return `true` if bootstrapping was performed successfully, `false` if failed
* @throws Throwable on initialization error * @throws Throwable on initialization error
* @since v1-alpha10 * @since v1-alpha10
*/ */
@JvmStatic @JvmStatic
@Throws(Throwable::class) @Throws(Throwable::class)
private fun bootstrap() { private fun bootstrap(): Boolean {
if (state != State.UNINITIALIZED || bootstrapping != null) if (state != State.UNINITIALIZED || bootstrapping != null)
return return true
try {
bootstrapping = true bootstrapping = true
logger.info("Bootstrapping") logger.info("Bootstrapping")
// Run bootstrapping code // Run bootstrapping code
bootstrapping = false bootstrapping = false
return true
} catch (exception: Exception) {
logger.crash("Engine failed to bootstrap", throwable = exception, fatal = true)
return false
}
} }
/** /**
* Initializes the engine. * Initializes the engine.
* *
* @throws Throwable on initialization error * @throws EngineInitializationFailureException on initialization error
* @since v1-alpha10 * @since v1-alpha10
*/ */
@JvmStatic @JvmStatic
@Throws(Throwable::class) @Throws(EngineInitializationFailureException::class)
fun initialize() { fun initialize() {
// Abort if initializing, shutting down // Abort if initializing, shutting down
// or the engine has crashed // or the engine has crashed
@ -114,8 +122,10 @@ class Engine private constructor() {
) return ) return
// Bootstrap if not already done so // Bootstrap if not already done so
bootstrap() if (!bootstrap())
return
try {
state = State.INITIALIZING state = State.INITIALIZING
logger.info("Initializing") logger.info("Initializing")
@ -124,6 +134,10 @@ class Engine private constructor() {
FileAccess.updateDefaultPaths() FileAccess.updateDefaultPaths()
state = State.INITIALIZED state = State.INITIALIZED
} catch (exception: Exception) {
logger.crash("Engine failed to initialize", throwable = exception, fatal = true)
throw EngineInitializationFailureException(exception)
}
} }
/** /**

View file

@ -0,0 +1,32 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* Licensed under the GNU Affero General Public License v3
* with an exception allowing classpath linking.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.engine.base.exception
/**
* Thrown when the engine fails to
* initialize or bootstrap itself.
*
* After this occurs, it will no longer
* be possible to initialize the engine.
*
* @since v1-alpha10
*/
class EngineInitializationFailureException(val throwable: Throwable) : RuntimeException("The StarOpenSource Engine failed to initialize", throwable)