mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(lifecycle): add support for startup and shutdown initialization hooks (#128)
* feat(lifecycle): add support for startup and shutdown initialization hooks * test(lifecycle): add unit tests for initialization hooks * test(lifecycle): add integration tests for initialization hooks * docs: add documentation and configuration for initialization hooks
This commit is contained in:
1 parent
823770e463
commit
7b2576fb42
14 files changed
+872
-34
No files matched your search
@@ -47,6 +47,8 @@ public interface EmulatorConfig {
|
||||
|
||||
ServicesConfig services();
|
||||
|
||||
InitHooksConfig initHooks();
|
||||
|
||||
interface StorageConfig {
|
||||
@WithDefault("hybrid")
|
||||
String mode();
|
||||
@@ -381,4 +383,15 @@ public interface EmulatorConfig {
|
||||
/** Docker network to attach Lambda containers to. Empty = default bridge. */
|
||||
Optional<String> dockerNetwork();
|
||||
}
|
||||
}
|
||||
|
||||
interface InitHooksConfig {
|
||||
@WithDefault("/bin/bash")
|
||||
String shellExecutable();
|
||||
|
||||
@WithDefault("2")
|
||||
long shutdownGracePeriodSeconds();
|
||||
|
||||
@WithDefault("30")
|
||||
long timeoutSeconds();
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,9 @@ package io.github.hectorvent.floci.lifecycle;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.ServiceRegistry;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
|
||||
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;
|
||||
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.RdsProxyManager;
|
||||
import io.quarkus.runtime.ShutdownEvent;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
@@ -13,6 +14,8 @@ import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EmulatorLifecycle {
|
||||
|
||||
@@ -23,35 +26,48 @@ public class EmulatorLifecycle {
|
||||
private final EmulatorConfig config;
|
||||
private final ElastiCacheProxyManager elastiCacheProxyManager;
|
||||
private final RdsProxyManager rdsProxyManager;
|
||||
private final RdsContainerManager rdsContainerManager;
|
||||
private final InitializationHooksRunner initializationHooksRunner;
|
||||
|
||||
@Inject
|
||||
public EmulatorLifecycle(StorageFactory storageFactory, ServiceRegistry serviceRegistry,
|
||||
EmulatorConfig config, ElastiCacheProxyManager elastiCacheProxyManager,
|
||||
RdsProxyManager rdsProxyManager, RdsContainerManager rdsContainerManager) {
|
||||
RdsProxyManager rdsProxyManager, InitializationHooksRunner initializationHooksRunner) {
|
||||
this.storageFactory = storageFactory;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.config = config;
|
||||
this.elastiCacheProxyManager = elastiCacheProxyManager;
|
||||
this.rdsProxyManager = rdsProxyManager;
|
||||
this.rdsContainerManager = rdsContainerManager;
|
||||
this.initializationHooksRunner = initializationHooksRunner;
|
||||
}
|
||||
|
||||
void onStart(@Observes StartupEvent event) {
|
||||
void onStart(@Observes StartupEvent ignored) throws IOException, InterruptedException {
|
||||
LOG.info("=== AWS Local Emulator Starting ===");
|
||||
LOG.infov("Storage mode: {0}", config.storage().mode());
|
||||
LOG.infov("Persistent path: {0}", config.storage().persistentPath());
|
||||
serviceRegistry.logEnabledServices();
|
||||
|
||||
serviceRegistry.logEnabledServices();
|
||||
storageFactory.loadAll();
|
||||
initializationHooksRunner.run(InitializationHook.START);
|
||||
|
||||
LOG.info("=== AWS Local Emulator Ready ===");
|
||||
}
|
||||
|
||||
void onStop(@Observes ShutdownEvent event) {
|
||||
void onStop(@Observes ShutdownEvent ignored) throws IOException, InterruptedException {
|
||||
LOG.info("=== AWS Local Emulator Shutting Down ===");
|
||||
elastiCacheProxyManager.stopAll();
|
||||
rdsProxyManager.stopAll();
|
||||
storageFactory.shutdownAll();
|
||||
|
||||
try {
|
||||
initializationHooksRunner.run(InitializationHook.STOP);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
LOG.error("Shutdown hook execution failed", e);
|
||||
throw e;
|
||||
} catch (RuntimeException e) {
|
||||
LOG.error("Shutdown hook script failed", e);
|
||||
} finally {
|
||||
elastiCacheProxyManager.stopAll();
|
||||
rdsProxyManager.stopAll();
|
||||
storageFactory.shutdownAll();
|
||||
}
|
||||
|
||||
LOG.info("=== AWS Local Emulator Stopped ===");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ApplicationScoped
|
||||
public class HookScriptExecutor {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(HookScriptExecutor.class);
|
||||
private final EmulatorConfig.InitHooksConfig initHooksConfig;
|
||||
|
||||
@Inject
|
||||
public HookScriptExecutor(final EmulatorConfig emulatorConfig) {
|
||||
this.initHooksConfig = emulatorConfig.initHooks();
|
||||
}
|
||||
|
||||
public void run(final File hookDirectory, final String scriptFileName) throws IOException, InterruptedException {
|
||||
final String shellExecutable = initHooksConfig.shellExecutable();
|
||||
LOG.debugv("Executing hook script {0} via {1}", scriptFileName, shellExecutable);
|
||||
|
||||
// Inherit parent I/O so script output is streamed directly and does not block on unconsumed buffers.
|
||||
final Process process = new ProcessBuilder(shellExecutable, scriptFileName).directory(hookDirectory).inheritIO().start();
|
||||
run(process, scriptFileName);
|
||||
}
|
||||
|
||||
void run(final Process process, final String scriptFileName) throws InterruptedException {
|
||||
final int exitCode = waitForProcessExitCode(process, scriptFileName);
|
||||
if (exitCode != 0) {
|
||||
final String message = String.format("Hook script failed: %s exited with code %d", scriptFileName, exitCode);
|
||||
throw new IllegalStateException(message);
|
||||
}
|
||||
}
|
||||
|
||||
private int waitForProcessExitCode(final Process process, final String scriptFileName) throws InterruptedException {
|
||||
try {
|
||||
final long timeoutSeconds = initHooksConfig.timeoutSeconds();
|
||||
final boolean finished = process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
|
||||
if (!finished) {
|
||||
LOG.debugv("Hook script exceeded timeout of {0} seconds, terminating process: {1}", timeoutSeconds, scriptFileName);
|
||||
terminateProcess(process, scriptFileName);
|
||||
|
||||
final String message = String.format("Hook script timed out after %d seconds: %s", timeoutSeconds, scriptFileName);
|
||||
throw new IllegalStateException(message);
|
||||
}
|
||||
|
||||
return process.exitValue();
|
||||
} finally {
|
||||
if (process.isAlive()) {
|
||||
LOG.debugv("Hook script process still alive during cleanup, forcing termination: {0}", scriptFileName);
|
||||
process.destroyForcibly();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void terminateProcess(final Process process, final String scriptFileName) throws InterruptedException {
|
||||
// Try a graceful shutdown first, then force termination if the process does not exit in time.
|
||||
process.destroy();
|
||||
if (process.isAlive()) {
|
||||
final long shutdownGracePeriodSeconds = initHooksConfig.shutdownGracePeriodSeconds();
|
||||
final boolean terminatedGracefully = process.waitFor(shutdownGracePeriodSeconds, TimeUnit.SECONDS);
|
||||
if (!terminatedGracefully) {
|
||||
LOG.debugv("Hook script process did not terminate gracefully, forcing termination: {0}", scriptFileName);
|
||||
process.destroyForcibly();
|
||||
process.waitFor(shutdownGracePeriodSeconds, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public enum InitializationHook {
|
||||
|
||||
STOP("shutdown", "/etc/floci/init/stop.d"),
|
||||
START("startup", "/etc/floci/init/start.d");
|
||||
|
||||
private final String name;
|
||||
private final File path;
|
||||
|
||||
InitializationHook(final String name, final String path) {
|
||||
this.name = name;
|
||||
this.path = new File(path);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public File getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package io.github.hectorvent.floci.lifecycle.inithook;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
@ApplicationScoped
|
||||
public class InitializationHooksRunner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(InitializationHooksRunner.class);
|
||||
|
||||
private static final String SHELL_SCRIPT_EXTENSION = ".sh";
|
||||
private static final FilenameFilter FILE_EXTENSION_FILTER = (ignored, name) -> name.endsWith(SHELL_SCRIPT_EXTENSION);
|
||||
|
||||
private final HookScriptExecutor hookScriptExecutor;
|
||||
|
||||
@Inject
|
||||
public InitializationHooksRunner(final HookScriptExecutor hookScriptExecutor) {
|
||||
this.hookScriptExecutor = hookScriptExecutor;
|
||||
}
|
||||
|
||||
private static String[] findScriptFileNames(final String hookName, final File hookDirectory) {
|
||||
final String hookDirectoryPath = hookDirectory.getAbsolutePath();
|
||||
if (!hookDirectory.exists()) {
|
||||
LOG.debugv("{0} hook directory does not exist: {1}", hookName, hookDirectoryPath);
|
||||
return new String[0];
|
||||
} else if (!hookDirectory.isDirectory()) {
|
||||
LOG.warnv("{0} hook path is not a directory: {1}", hookName, hookDirectoryPath);
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
final String[] scriptFileNames = hookDirectory.list(FILE_EXTENSION_FILTER);
|
||||
if ((scriptFileNames == null) || (scriptFileNames.length == 0)) {
|
||||
LOG.debugv("No {0} hook scripts found in {1}", hookName, hookDirectoryPath);
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
Arrays.sort(scriptFileNames);
|
||||
return scriptFileNames;
|
||||
}
|
||||
|
||||
public void run(final InitializationHook hook) throws IOException, InterruptedException {
|
||||
final String hookName = hook.getName();
|
||||
final File hookDirectory = hook.getPath();
|
||||
run(hookName, hookDirectory);
|
||||
}
|
||||
|
||||
public void run(final String hookName, final File hookDirectory) throws IOException, InterruptedException {
|
||||
final String[] scriptFileNames = findScriptFileNames(hookName, hookDirectory);
|
||||
if (scriptFileNames.length > 0) {
|
||||
final String hookDirectoryPath = hookDirectory.getAbsolutePath();
|
||||
final String scriptList = Arrays.toString(scriptFileNames);
|
||||
LOG.infov("Running {0} hook with {1} script(s) from {2}: {3}", hookName, scriptFileNames.length, hookDirectoryPath, scriptList);
|
||||
|
||||
// Hook scripts are executed sequentially and treated as dependent steps.
|
||||
// Stop at the first script failure to avoid continuing in a partially initialized state
|
||||
for (var scriptFileName : scriptFileNames) {
|
||||
LOG.infov("Executing {0} hook script: {1}", hookName, scriptFileName);
|
||||
hookScriptExecutor.run(hookDirectory, scriptFileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user