Skip to content

Commit

Permalink
feat(core): new sleep task
Browse files Browse the repository at this point in the history
close #4879 
Co-authored-by: YannC <ycoornaert@kestra.io>
  • Loading branch information
yoyounik authored Oct 23, 2024
1 parent d3ccf4f commit b99d9d4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
57 changes: 57 additions & 0 deletions core/src/main/java/io/kestra/plugin/core/flow/Sleep.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.kestra.plugin.core.flow;

import io.kestra.core.models.annotations.Example;
import io.kestra.core.models.annotations.Plugin;
import io.kestra.core.models.annotations.PluginProperty;
import io.kestra.core.models.tasks.RunnableTask;
import io.kestra.core.models.tasks.Task;
import io.kestra.core.models.tasks.VoidOutput;
import io.kestra.core.runners.RunContext;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.SuperBuilder;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

@SuperBuilder
@ToString
@EqualsAndHashCode
@Getter
@NoArgsConstructor
@Schema(
title = "A task that sleep for a specified duration before proceeding."
)
@Plugin(
examples = {
@Example(
code = """
id: sleep
type: io.kestra.plugin.core.flow.Sleep
duration: "PT5S"
"""
)
}
)
public class Sleep extends Task implements RunnableTask<VoidOutput> {
@Schema(
title = "Duration to sleep",
description = "The time duration in ISO-8601 format (e.g., `PT5S` for 5 seconds)."
)
@PluginProperty
@NotNull
private Duration duration;

public VoidOutput run(RunContext runContext) throws Exception {
runContext.logger().info("Waiting for {}", duration);

// Wait for the specified duration
TimeUnit.MILLISECONDS.sleep(duration.toMillis());

return null;
}
}
23 changes: 23 additions & 0 deletions core/src/test/java/io/kestra/plugin/core/flow/SleepTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.kestra.plugin.core.flow;

import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.flows.State;
import io.kestra.core.queues.QueueException;
import io.kestra.core.runners.AbstractMemoryRunnerTest;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeoutException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class SleepTest extends AbstractMemoryRunnerTest {
@Test
void sleepTaskTest() throws TimeoutException, QueueException {
Execution execution = runnerUtils.runOne(null, "io.kestra.tests", "sleep-task-flow");

assertThat(execution.getState().getCurrent(), is(State.Type.SUCCESS));
}


}
9 changes: 9 additions & 0 deletions core/src/test/resources/flows/valids/sleep-task-flow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id: sleep-task-flow
namespace: io.kestra.tests

tasks:
- id: sleep
type: io.kestra.plugin.core.flow.Sleep
duration: PT2S


0 comments on commit b99d9d4

Please sign in to comment.