From 3528091684f610d80fcebb5b730d3a201e79a99a Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 28 Oct 2022 11:48:59 +0200 Subject: feat(sim/compute): Add completion parameter to startWorkload This change updates the interface of `SimMachine#startWorkload` to introduce a parameter `completion` that is invoked when the workload completes either succesfully or due to failure. This functionality has often been implemented by wrapping a `SimWorkload` and catching its exceptions. However, since this functionality is used in all usages of `SimMachine#startWorkload` we instead embed it into `SimMachine` itself. --- .../kotlin/org/opendc/compute/simulator/SimHost.kt | 31 +++++----------------- 1 file changed, 6 insertions(+), 25 deletions(-) (limited to 'opendc-compute') diff --git a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt index c07649bd..d07c50bc 100644 --- a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt +++ b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt @@ -43,7 +43,6 @@ import org.opendc.simulator.compute.model.MachineModel import org.opendc.simulator.compute.model.MemoryUnit import org.opendc.simulator.compute.model.ProcessingNode import org.opendc.simulator.compute.model.ProcessingUnit -import org.opendc.simulator.compute.workload.SimWorkload import org.opendc.simulator.flow2.FlowGraph import java.time.Duration import java.time.Instant @@ -284,30 +283,12 @@ public class SimHost( check(_ctx == null) { "Concurrent hypervisor running" } // Launch hypervisor onto machine - _ctx = machine.startWorkload( - object : SimWorkload { - override fun onStart(ctx: SimMachineContext) { - try { - _bootTime = clock.instant() - _state = HostState.UP - hypervisor.onStart(ctx) - } catch (cause: Throwable) { - _state = HostState.ERROR - _ctx = null - throw cause - } - } - - override fun onStop(ctx: SimMachineContext) { - try { - hypervisor.onStop(ctx) - } finally { - _ctx = null - } - } - }, - emptyMap() - ) + _bootTime = clock.instant() + _state = HostState.UP + _ctx = machine.startWorkload(hypervisor, emptyMap()) { cause -> + _state = if (cause != null) HostState.ERROR else HostState.DOWN + _ctx = null + } } /** -- cgit v1.2.3