From bc018afad079a90babcba7107260fed6f7f0e62b Mon Sep 17 00:00:00 2001 From: vamsiak Date: Sun, 20 Sep 2026 02:18:07 +0530 Subject: [PATCH] feat(scheduler): start Step Functions targets (#3936) Co-authored-by: Vamsi Akavarapu <16164447+vamsiak@users.noreply.github.com> --- docs/services/scheduler.md | 6 +- .../services/scheduler/ScheduleInvoker.java | 21 +- .../scheduler/ScheduleInvokerTest.java | 54 ++++- ...SchedulerStepFunctionsIntegrationTest.java | 220 ++++++++++++++++++ 4 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerStepFunctionsIntegrationTest.java diff --git a/docs/services/scheduler.md b/docs/services/scheduler.md index da21742be2..2dbf4c7cef 100644 --- a/docs/services/scheduler.md +++ b/docs/services/scheduler.md @@ -36,7 +36,11 @@ background dispatcher fires schedule targets on time. Supported expressions: window are skipped. The dispatcher ticks every `floci.services.scheduler.tick-interval-seconds` (default `10`). -Supported target types: SQS, Lambda, SNS, EventBridge `PutEvents`. +Supported target types: SQS, Lambda, SNS, ECS `RunTask`, EventBridge +`PutEvents`, and unqualified Step Functions state machine ARNs. A Step Functions +target starts an asynchronous execution with the schedule target's `Input`, or +`{}` when `Input` is absent. Scheduler delivery succeeds when `StartExecution` +is accepted; a later workflow failure does not trigger Scheduler retries. ### Retries and dead-letter queues diff --git a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java index 10a8619ddf..f2b96296d9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java +++ b/src/main/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvoker.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RequestScopes; import io.github.hectorvent.floci.services.ecs.EcsService; import io.github.hectorvent.floci.services.ecs.model.LaunchType; import io.github.hectorvent.floci.services.eventbridge.EventBridgeService; @@ -17,6 +18,7 @@ import io.github.hectorvent.floci.services.sns.SnsMessageAttributes; import io.github.hectorvent.floci.services.sns.SnsService; import io.github.hectorvent.floci.services.sqs.SqsService; import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue; +import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -29,7 +31,7 @@ import java.util.Map; /** * Delivers an EventBridge Scheduler target invocation to the underlying service. - * Supports templated SQS, Lambda, SNS, and EventBridge PutEvents targets, plus + * Supports templated SQS, Lambda, SNS, Step Functions, and EventBridge PutEvents targets, plus * universal targets ({@code arn:aws:scheduler:::aws-sdk::}) for * {@code sns:publish} and {@code sqs:sendMessage}. Mirrors the subset handled by * {@code EventBridgeInvoker} but using Scheduler's {@link Target} model (raw @@ -45,6 +47,7 @@ public class ScheduleInvoker { private final SnsService snsService; private final EventBridgeService eventBridgeService; private final EcsService ecsService; + private final StepFunctionsService stepFunctionsService; private final ObjectMapper objectMapper; private final String baseUrl; @@ -54,6 +57,7 @@ public class ScheduleInvoker { SnsService snsService, EventBridgeService eventBridgeService, EcsService ecsService, + StepFunctionsService stepFunctionsService, ObjectMapper objectMapper, EmulatorConfig config) { this.sqsService = sqsService; @@ -61,6 +65,7 @@ public class ScheduleInvoker { this.snsService = snsService; this.eventBridgeService = eventBridgeService; this.ecsService = ecsService; + this.stepFunctionsService = stepFunctionsService; this.objectMapper = objectMapper; this.baseUrl = config.baseUrl(); } @@ -99,6 +104,11 @@ public class ScheduleInvoker { } else if (arn.contains(":ecs:") && target.getEcsParameters() != null) { deliverToEcsRunTask(target, targetRegion); LOG.debugv("Scheduler delivered to ECS RunTask: {0}", arn); + } else if (isStateMachineArn(arn)) { + String targetAccount = AwsArnUtils.parse(arn).accountId(); + RequestScopes.runAs(targetAccount, + () -> stepFunctionsService.startExecution(arn, null, payload, targetRegion)); + LOG.debugv("Scheduler started Step Functions execution: {0}", arn); } else if (isEventBridgePutEventsArn(arn)) { deliverToEventBridge(target, payload, targetRegion); LOG.debugv("Scheduler delivered to EventBridge: {0}", arn); @@ -277,6 +287,15 @@ public class ScheduleInvoker { return arn.contains(":events:") && arn.contains(":event-bus/"); } + private static boolean isStateMachineArn(String arn) { + if (!AwsArnUtils.isArnFor(arn, "states")) { + return false; + } + String resource = AwsArnUtils.parse(arn).resource(); + String prefix = "stateMachine:"; + return resource.startsWith(prefix) && resource.indexOf(':', prefix.length()) < 0; + } + private void deliverToEventBridge(Target target, String payload, String region) { String busArn = target.getArn(); String busName = busArn.substring(busArn.indexOf(":event-bus/") + ":event-bus/".length()); diff --git a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java index 67b1617541..00e1ef6869 100644 --- a/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/scheduler/ScheduleInvokerTest.java @@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.scheduler.model.Target; import io.github.hectorvent.floci.services.sns.SnsService; import io.github.hectorvent.floci.services.sqs.SqsService; import io.github.hectorvent.floci.services.sqs.model.MessageAttributeValue; +import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -50,6 +51,7 @@ class ScheduleInvokerTest { private SnsService snsService; private EventBridgeService eventBridgeService; private EcsService ecsService; + private StepFunctionsService stepFunctionsService; private ScheduleInvoker invoker; @BeforeEach @@ -59,10 +61,11 @@ class ScheduleInvokerTest { snsService = mock(SnsService.class); eventBridgeService = mock(EventBridgeService.class); ecsService = mock(EcsService.class); + stepFunctionsService = mock(StepFunctionsService.class); EmulatorConfig config = mock(EmulatorConfig.class); when(config.baseUrl()).thenReturn("http://localhost:4566"); invoker = new ScheduleInvoker(sqsService, lambdaService, snsService, - eventBridgeService, ecsService, new ObjectMapper(), config); + eventBridgeService, ecsService, stepFunctionsService, new ObjectMapper(), config); } @Test @@ -279,6 +282,55 @@ class ScheduleInvokerTest { eq("Scheduler"), eq("us-east-1")); } + @Test + void stateMachineTargetStartsExecutionWithInputAndArnRegion() { + String stateMachineArn = "arn:aws:states:eu-west-1:000000000000:stateMachine:scheduled-workflow"; + String input = "{\"order\":{\"id\":42}}"; + Target target = new Target(); + target.setArn(stateMachineArn); + target.setRoleArn("arn:aws:iam::000000000000:role/scheduler-role"); + target.setInput(input); + + invoker.invoke(target, "us-east-1"); + + verify(stepFunctionsService).startExecution(stateMachineArn, null, input, "eu-west-1"); + } + + @Test + void stateMachineTargetWithoutInputUsesEmptyObject() { + String stateMachineArn = "arn:aws:states:us-east-1:000000000000:stateMachine:scheduled-workflow"; + Target target = new Target(); + target.setArn(stateMachineArn); + + invoker.invoke(target, "us-east-1"); + + verify(stepFunctionsService).startExecution(stateMachineArn, null, "{}", "us-east-1"); + } + + @Test + void stateMachineStartFailurePropagatesToSchedulerDeliveryHandling() { + String stateMachineArn = "arn:aws:states:us-east-1:000000000000:stateMachine:missing"; + Target target = new Target(); + target.setArn(stateMachineArn); + when(stepFunctionsService.startExecution(stateMachineArn, null, "{}", "us-east-1")) + .thenThrow(new AwsException("StateMachineDoesNotExist", "State machine does not exist", 400)); + + AwsException error = assertThrows(AwsException.class, + () -> invoker.invoke(target, "us-east-1")); + + assertEquals("StateMachineDoesNotExist", error.getErrorCode()); + } + + @Test + void qualifiedStateMachineTargetRemainsUnsupported() { + Target target = new Target(); + target.setArn("arn:aws:states:us-east-1:000000000000:stateMachine:scheduled-workflow:PROD"); + + assertThrows(UnsupportedOperationException.class, + () -> invoker.invoke(target, "us-east-1")); + verifyNoInteractions(stepFunctionsService); + } + @Test @SuppressWarnings("unchecked") void eventBusTargetUsesDeclaredDetailTypeAndSource() { diff --git a/src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerStepFunctionsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerStepFunctionsIntegrationTest.java new file mode 100644 index 0000000000..da19a6263c --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/scheduler/SchedulerStepFunctionsIntegrationTest.java @@ -0,0 +1,220 @@ +package io.github.hectorvent.floci.services.scheduler; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.services.scheduler.model.Schedule; +import io.github.hectorvent.floci.services.sqs.SqsService; +import io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService; +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@QuarkusTest +class SchedulerStepFunctionsIntegrationTest { + + private static final String REGION = "us-east-1"; + private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/scheduler-role"; + private static final ObjectMapper MAPPER = new ObjectMapper(); + private static final DateTimeFormatter SCHEDULE_TIME = + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC); + + @Inject + ScheduleInvoker scheduleInvoker; + + @Inject + SchedulerService schedulerService; + + @Inject + StepFunctionsService stepFunctionsService; + + @Inject + SqsService sqsService; + + @Inject + EmulatorConfig config; + + private final List scheduleNames = new ArrayList<>(); + private final List stateMachineArns = new ArrayList<>(); + private final List testDispatchers = new ArrayList<>(); + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @AfterEach + void cleanUp() { + for (String scheduleName : scheduleNames) { + try { + schedulerService.deleteSchedule(scheduleName, null, REGION); + } catch (AwsException expected) { + // A schedule with ActionAfterCompletion=DELETE may already be gone. + } + } + for (String stateMachineArn : stateMachineArns) { + stepFunctionsService.deleteStateMachine(stateMachineArn); + } + for (ScheduleDispatcher dispatcher : testDispatchers) { + dispatcher.onStop(null); + } + } + + @Test + void oneTimeScheduleStartsStateMachineWithExactInput() { + String stateMachineArn = createPassStateMachine(); + String scheduleName = uniqueName("sfn-at"); + String input = "{\"order\":{\"id\":42},\"items\":[\"a\",\"b\"]}"; + Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS); + createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", "ENABLED", + stateMachineArn, input); + + dispatcherFor(scheduleName).tick(fireAt); + + List> executions = listExecutions(stateMachineArn); + assertEquals(1, executions.size()); + Response execution = waitForTerminalExecution((String) executions.get(0).get("executionArn")); + assertEquals("SUCCEEDED", execution.jsonPath().getString("status")); + assertEquals(input, execution.jsonPath().getString("input")); + assertEquals(input, execution.jsonPath().getString("output")); + } + + @Test + void recurringScheduleStartsOneExecutionPerOccurrence() { + String stateMachineArn = createPassStateMachine(); + String scheduleName = uniqueName("sfn-rate"); + createSchedule(scheduleName, "rate(1 minute)", "ENABLED", stateMachineArn, "{\"kind\":\"recurring\"}"); + Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION); + Instant firstFire = schedule.getCreationDate().plus(61, ChronoUnit.SECONDS); + ScheduleDispatcher dispatcher = dispatcherFor(scheduleName); + + dispatcher.tick(firstFire); + dispatcher.tick(firstFire.plus(61, ChronoUnit.SECONDS)); + + assertEquals(2, listExecutions(stateMachineArn).size()); + } + + @Test + void disabledScheduleDoesNotStartStateMachine() { + String stateMachineArn = createPassStateMachine(); + String scheduleName = uniqueName("sfn-disabled"); + Instant fireAt = Instant.now().plus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.SECONDS); + createSchedule(scheduleName, "at(" + SCHEDULE_TIME.format(fireAt) + ")", "DISABLED", + stateMachineArn, "{\"disabled\":true}"); + + dispatcherFor(scheduleName).tick(fireAt); + + assertEquals(0, listExecutions(stateMachineArn).size()); + } + + private String createPassStateMachine() { + String name = uniqueName("scheduled-workflow"); + String definition = "{\"StartAt\":\"Pass\",\"States\":{\"Pass\":{\"Type\":\"Pass\",\"End\":true}}}"; + ObjectNode request = MAPPER.createObjectNode(); + request.put("name", name); + request.put("definition", definition); + request.put("roleArn", ROLE_ARN); + + Response response = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine") + .contentType(SFN_CONTENT_TYPE) + .body(request.toString()) + .when() + .post("/"); + response.then().statusCode(200); + String stateMachineArn = response.jsonPath().getString("stateMachineArn"); + stateMachineArns.add(stateMachineArn); + return stateMachineArn; + } + + private void createSchedule(String name, String expression, String state, + String targetArn, String input) { + ObjectNode request = MAPPER.createObjectNode(); + request.put("ScheduleExpression", expression); + request.putObject("FlexibleTimeWindow").put("Mode", "OFF"); + request.put("State", state); + ObjectNode target = request.putObject("Target"); + target.put("Arn", targetArn); + target.put("RoleArn", ROLE_ARN); + target.put("Input", input); + + given() + .contentType("application/json") + .body(request.toString()) + .when() + .post("/schedules/" + name) + .then() + .statusCode(200); + scheduleNames.add(name); + } + + private ScheduleDispatcher dispatcherFor(String scheduleName) { + Schedule schedule = schedulerService.getSchedule(scheduleName, null, REGION); + SchedulerService scopedSchedulerService = mock(SchedulerService.class); + when(scopedSchedulerService.listAllSchedules()).thenReturn(List.of(schedule)); + ScheduleDispatcher dispatcher = new ScheduleDispatcher( + scopedSchedulerService, scheduleInvoker, sqsService, config); + testDispatchers.add(dispatcher); + return dispatcher; + } + + private List> listExecutions(String stateMachineArn) { + ObjectNode request = MAPPER.createObjectNode(); + request.put("stateMachineArn", stateMachineArn); + Response response = given() + .header("X-Amz-Target", "AWSStepFunctions.ListExecutions") + .contentType(SFN_CONTENT_TYPE) + .body(request.toString()) + .when() + .post("/"); + response.then().statusCode(200); + return response.jsonPath().getList("executions"); + } + + private Response waitForTerminalExecution(String executionArn) { + await() + .atMost(Duration.ofSeconds(5)) + .pollInterval(Duration.ofMillis(100)) + .until(() -> !"RUNNING".equals(describeExecution(executionArn).jsonPath().getString("status"))); + return describeExecution(executionArn); + } + + private Response describeExecution(String executionArn) { + ObjectNode request = MAPPER.createObjectNode(); + request.put("executionArn", executionArn); + Response response = given() + .header("X-Amz-Target", "AWSStepFunctions.DescribeExecution") + .contentType(SFN_CONTENT_TYPE) + .body(request.toString()) + .when() + .post("/"); + response.then().statusCode(200); + return response; + } + + private static String uniqueName(String prefix) { + return prefix + "-" + UUID.randomUUID().toString().substring(0, 8); + } +}