diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java index 71872cf079..b0964d15b5 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardown.java @@ -3,17 +3,16 @@ package io.github.hectorvent.floci.core.common; /** * Implemented by services that launch Docker containers whose lifetime is bound to the * emulator process (Lambda warm pool, ECS tasks, EC2 instances, in-flight build/job - * containers). {@code EmulatorLifecycle.onStop} invokes every implementation during the - * ShutdownEvent phase, before {@code StorageFactory.shutdownAll()}, so teardown runs at - * a deterministic point and state changes made while stopping are still captured by the - * final flush. A {@code @PreDestroy} alone is not sufficient for that: bean destruction - * runs after the ShutdownEvent observers, when the storage flush schedulers are already - * stopped. - * - *

{@code EmulatorInfoController} also invokes every implementation on {@code /state/reset} - * and {@code /state/nuke}, since those wipe the same tracking state these containers were - * started against; without this, a reset leaves them running with nothing left to reconcile - * them against. + * containers). {@link ContainerTeardowns#stopAll} runs every implementation, tolerating + * individual failures, and is invoked from two call sites: {@code EmulatorLifecycle.onStop} + * during the ShutdownEvent phase, before {@code StorageFactory.shutdownAll()}, so teardown + * runs at a deterministic point and state changes made while stopping are still captured + * by the final flush; and {@code EmulatorInfoController} on {@code /state/reset} and + * {@code /state/nuke}, since those wipe the same tracking state these containers were + * started against, and without this a reset would leave them running with nothing left + * to reconcile them against. A {@code @PreDestroy} alone is not sufficient for the + * shutdown case: bean destruction runs after the ShutdownEvent observers, when the + * storage flush schedulers are already stopped. * *

Implementations must be idempotent; they may also be invoked from {@code @PreDestroy} * as a fallback. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java new file mode 100644 index 0000000000..22a4ed9626 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/ContainerTeardowns.java @@ -0,0 +1,27 @@ +package io.github.hectorvent.floci.core.common; + +import jakarta.enterprise.inject.Instance; +import org.jboss.logging.Logger; + +/** + * Runs every {@link ContainerTeardown} implementation, tolerating a failure in one so it + * doesn't block the others or whatever cleanup the caller runs next. {@code EmulatorLifecycle} + * (on {@code ShutdownEvent}) and {@code EmulatorInfoController} (on {@code /state/reset} and + * {@code /state/nuke}) each independently ran this identical loop before this class existed. + */ +public final class ContainerTeardowns { + + private ContainerTeardowns() { + } + + public static void stopAll(Instance teardowns, Logger log) { + for (ContainerTeardown teardown : teardowns) { + try { + teardown.stopManagedContainers(); + } catch (Exception e) { + log.warnv("Container teardown failed for {0}: {1}", + teardown.getClass().getSimpleName(), e.getMessage()); + } + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java index 740474c25e..d43d85637e 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.ContainerCaBundle; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.FlociCertificateAuthority; import io.github.hectorvent.floci.core.common.ContainerTeardown; +import io.github.hectorvent.floci.core.common.ContainerTeardowns; import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook; import jakarta.inject.Inject; @@ -157,16 +158,9 @@ public class EmulatorInfoController { // in any order relative to the storage wipe below, but stopping them here means a // client's reset actually reflects a clean slate instead of leaving Batch, CodeBuild, // or SageMaker containers running with no record of them left in the store. - for (ContainerTeardown teardown : containerTeardowns) { - try { - teardown.stopManagedContainers(); - } catch (Exception e) { - LOG.warnv("Container teardown failed for {0}: {1}", - teardown.getClass().getSimpleName(), e.getMessage()); - } - } - // Storage first. Services re-create their bootstrap state in clear(), and a wipe - // afterwards would remove it again until the next restart. + ContainerTeardowns.stopAll(containerTeardowns, LOG); + // Storage before resettables: services re-create their bootstrap state in clear(), and + // a wipe afterwards would remove it again until the next restart. storageFactory.clearAll(); for (Resettable r : resettables) { r.clear(); diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java index b658c07e20..d4671a84fa 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorLifecycle.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.lifecycle; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.ContainerTeardown; +import io.github.hectorvent.floci.core.common.ContainerTeardowns; import io.github.hectorvent.floci.core.common.ServiceRegistry; import io.github.hectorvent.floci.core.storage.PersistentPathValidator; import io.github.hectorvent.floci.core.storage.StorageFactory; @@ -329,14 +330,7 @@ public class EmulatorLifecycle { // Centralized teardown for process-bound containers (Lambda warm pool, ECS tasks, // EC2 instances, in-flight build/job containers). Runs before shutdownAll() so any // state written while stopping is captured by the final flush. - for (ContainerTeardown teardown : containerTeardowns) { - try { - teardown.stopManagedContainers(); - } catch (Exception e) { - LOG.warnv("Container teardown failed for {0}: {1}", - teardown.getClass().getSimpleName(), e.getMessage()); - } - } + ContainerTeardowns.stopAll(containerTeardowns, LOG); runCleanup("storage shutdown", storageFactory::shutdownAll); LOG.info("=== AWS Local Emulator Stopped ===");