feat(scheduler): invoke targets when schedules fire (#551)

Adds a background dispatcher that reads persisted schedules and invokes their
targets on time, closing the CRUD-only gap called out in PR #260 and
documented under "Not Yet Supported".

- SchedulerExpressionParser parses at(), rate(), and 6-field cron() with
  optional ScheduleExpressionTimezone (default UTC).
- ScheduleInvoker delivers to SQS, Lambda, SNS, and EventBridge PutEvents,
  mirroring the EventBridgeInvoker pattern but adapted to Scheduler's Target
  model (raw Input string, no JSONPath/template).
- ScheduleDispatcher is a single-threaded ticker that scans every schedule,
  gates on State/StartDate/EndDate, fires due schedules, and honours
  ActionAfterCompletion=DELETE for one-time at() schedules.
- FlexibleTimeWindow, RetryPolicy and DeadLetterConfig are stored but not
  yet honoured; those can be layered on incrementally.

Config (floci.services.scheduler):
  - invocation-enabled (default true)
  - tick-interval-seconds (default 10)

Unit tests cover the parser (at/rate/cron, timezone, validation) and the
dispatcher (firing, once-semantics, delete-after-completion, state gating,
start/end windows, unsupported expression handling).

Closes #550
This commit is contained in:
Victor Sanchez Velasco authored and GitHub committed 2026-04-21 19:42:01 -05:00
1 parent 9ea28ee629
commit b9957dfdbe
8 files changed
+775 -1

No files matched your search

@@ -0,0 +1,179 @@
package io.github.hectorvent.floci.services.scheduler;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.scheduler.model.Schedule;
import io.github.hectorvent.floci.services.scheduler.model.Target;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class ScheduleDispatcherTest {
private static final String ARN_PREFIX = "arn:aws:scheduler:eu-central-1:000000000000:schedule/default/";
private static final String SQS_TARGET_ARN = "arn:aws:sqs:eu-central-1:000000000000:test-queue";
private SchedulerService schedulerService;
private ScheduleInvoker invoker;
private ScheduleDispatcher dispatcher;
@BeforeEach
void setUp() {
schedulerService = mock(SchedulerService.class);
invoker = mock(ScheduleInvoker.class);
EmulatorConfig.SchedulerServiceConfig schedulerCfg = mock(EmulatorConfig.SchedulerServiceConfig.class);
when(schedulerCfg.enabled()).thenReturn(true);
when(schedulerCfg.invocationEnabled()).thenReturn(true);
when(schedulerCfg.tickIntervalSeconds()).thenReturn(10L);
EmulatorConfig.ServicesConfig servicesCfg = mock(EmulatorConfig.ServicesConfig.class);
when(servicesCfg.scheduler()).thenReturn(schedulerCfg);
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.services()).thenReturn(servicesCfg);
dispatcher = new ScheduleDispatcher(schedulerService, invoker, config);
}
private Schedule newSchedule(String name, String expression, String state) {
Schedule s = new Schedule();
s.setName(name);
s.setGroupName("default");
s.setArn(ARN_PREFIX + name);
s.setState(state);
s.setScheduleExpression(expression);
Target target = new Target();
target.setArn(SQS_TARGET_ARN);
target.setRoleArn("arn:aws:iam::000000000000:role/test");
target.setInput("{\"hello\":\"world\"}");
s.setTarget(target);
s.setCreationDate(Instant.parse("2026-04-21T09:00:00Z"));
return s;
}
@Test
void firesAtScheduleWhenDue() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(invoker, times(1)).invoke(s.getTarget(), "eu-central-1");
}
@Test
void skipsAtScheduleBeforeFireTime() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:17:00Z"));
verify(invoker, never()).invoke(any(), anyString());
}
@Test
void firesAtOnlyOncePerSchedule() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
dispatcher.tick(Instant.parse("2026-04-21T09:19:00Z"));
dispatcher.tick(Instant.parse("2026-04-21T09:20:00Z"));
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
}
@Test
void deletesAtScheduleWhenActionAfterCompletionIsDelete() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
s.setActionAfterCompletion("DELETE");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(schedulerService, times(1)).deleteSchedule("at1", "default", "eu-central-1");
}
@Test
void leavesAtScheduleInPlaceWhenActionAfterCompletionIsNotDelete() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
s.setActionAfterCompletion("NONE");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(schedulerService, never()).deleteSchedule(anyString(), anyString(), anyString());
}
@Test
void skipsDisabledSchedules() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "DISABLED");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(invoker, never()).invoke(any(), anyString());
}
@Test
void skipsBeforeStartDate() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
s.setStartDate(Instant.parse("2026-04-22T00:00:00Z"));
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(invoker, never()).invoke(any(), anyString());
}
@Test
void skipsAfterEndDate() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
s.setEndDate(Instant.parse("2026-04-21T09:17:00Z"));
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(invoker, never()).invoke(any(), anyString());
}
@Test
void ratesFireOnceIntervalHasPassed() {
Schedule s = newSchedule("rate1", "rate(5 minutes)", "ENABLED");
s.setCreationDate(Instant.parse("2026-04-21T09:00:00Z"));
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:04:00Z"));
verify(invoker, never()).invoke(any(), anyString());
dispatcher.tick(Instant.parse("2026-04-21T09:06:00Z"));
verify(invoker, times(1)).invoke(eq(s.getTarget()), anyString());
dispatcher.tick(Instant.parse("2026-04-21T09:11:01Z"));
verify(invoker, times(2)).invoke(eq(s.getTarget()), anyString());
}
@Test
void unsupportedExpressionIsSkippedNotThrown() {
Schedule s = newSchedule("weird", "every 5 minutes", "ENABLED");
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
assertDoesNotThrow(() -> dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z")));
verify(invoker, never()).invoke(any(), anyString());
}
@Test
void missingTargetIsSkipped() {
Schedule s = newSchedule("at1", "at(2026-04-21T09:17:54)", "ENABLED");
s.setTarget(null);
when(schedulerService.listAllSchedules()).thenReturn(List.of(s));
dispatcher.tick(Instant.parse("2026-04-21T09:18:00Z"));
verify(invoker, never()).invoke(any(), anyString());
}
}
@@ -0,0 +1,99 @@
package io.github.hectorvent.floci.services.scheduler;
import io.github.hectorvent.floci.services.scheduler.SchedulerExpressionParser.Kind;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import static org.junit.jupiter.api.Assertions.*;
class SchedulerExpressionParserTest {
@Test
void classifyAtExpression() {
assertEquals(Kind.AT, SchedulerExpressionParser.classify("at(2026-04-21T09:17:54)"));
assertEquals(Kind.AT, SchedulerExpressionParser.classify("AT(2026-04-21T09:17:54)"));
}
@Test
void classifyRateExpression() {
assertEquals(Kind.RATE, SchedulerExpressionParser.classify("rate(5 minutes)"));
assertEquals(Kind.RATE, SchedulerExpressionParser.classify("rate(1 hour)"));
}
@Test
void classifyCronExpression() {
assertEquals(Kind.CRON, SchedulerExpressionParser.classify("cron(0 10 * * ? *)"));
}
@Test
void classifyRejectsUnknown() {
assertThrows(IllegalArgumentException.class,
() -> SchedulerExpressionParser.classify("every 5 minutes"));
assertThrows(IllegalArgumentException.class,
() -> SchedulerExpressionParser.classify(null));
}
@Test
void parseAtInUtc() {
Instant expected = ZonedDateTime.of(2026, 4, 21, 9, 17, 54, 0, ZoneOffset.UTC).toInstant();
assertEquals(expected, SchedulerExpressionParser.parseAt("at(2026-04-21T09:17:54)", null));
assertEquals(expected, SchedulerExpressionParser.parseAt("at(2026-04-21T09:17:54)", "UTC"));
}
@Test
void parseAtInTimezoneShiftsInstant() {
Instant utc = SchedulerExpressionParser.parseAt("at(2026-04-21T09:17:54)", "UTC");
Instant berlin = SchedulerExpressionParser.parseAt("at(2026-04-21T09:17:54)", "Europe/Berlin");
assertTrue(berlin.isBefore(utc),
"09:17 Europe/Berlin should be an earlier instant than 09:17 UTC");
}
@Test
void parseAtRejectsMalformed() {
assertThrows(IllegalArgumentException.class,
() -> SchedulerExpressionParser.parseAt("at(not-a-date)", null));
}
@Test
void parseRateMillis() {
assertEquals(300_000L, SchedulerExpressionParser.parseRateMillis("rate(5 minutes)"));
assertEquals(3_600_000L, SchedulerExpressionParser.parseRateMillis("rate(1 hour)"));
assertEquals(86_400_000L, SchedulerExpressionParser.parseRateMillis("rate(1 day)"));
assertEquals(604_800_000L, SchedulerExpressionParser.parseRateMillis("rate(1 week)"));
}
@Test
void parseRateRejectsZero() {
assertThrows(IllegalArgumentException.class,
() -> SchedulerExpressionParser.parseRateMillis("rate(0 minutes)"));
}
@Test
void nextCronFireComputesFutureInstant() {
Instant from = ZonedDateTime.of(2026, 4, 21, 9, 0, 0, 0, ZoneOffset.UTC).toInstant();
Instant next = SchedulerExpressionParser.nextCronFire("cron(30 10 * * ? *)", from, null);
ZonedDateTime asUtc = next.atZone(ZoneOffset.UTC);
assertEquals(10, asUtc.getHour());
assertEquals(30, asUtc.getMinute());
assertTrue(next.isAfter(from));
}
@Test
void nextCronFireRespectsTimezone() {
Instant from = ZonedDateTime.of(2026, 4, 21, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();
Instant nextUtc = SchedulerExpressionParser.nextCronFire("cron(0 10 * * ? *)", from, "UTC");
Instant nextBerlin = SchedulerExpressionParser.nextCronFire("cron(0 10 * * ? *)", from, "Europe/Berlin");
assertNotEquals(nextUtc, nextBerlin,
"10:00 UTC and 10:00 Europe/Berlin are different absolute instants");
}
@Test
void nextCronFireRejectsWrongFieldCount() {
Instant from = Instant.now();
assertThrows(IllegalArgumentException.class,
() -> SchedulerExpressionParser.nextCronFire("cron(0 10 * * *)", from, null));
}
}