-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: new getLog task #1059
Merged
Merged
feat: new getLog task #1059
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c23bf35
fix(build): missing dependsOn for gradle 8
tchiotludo 05cd340
feat: new getLog task
Skraye f4e378c
fix(build): missing dependsOn for gradle 8
tchiotludo 0e101c7
feat: new getLog task
Skraye 01c8eec
cleanup
tchiotludo 30c72c9
Merge branch 'develop' into feat/get-log-task
tchiotludo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package io.kestra.core.tasks.log; | ||
|
||
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.repositories.LogRepositoryInterface; | ||
import io.kestra.core.runners.RunContext; | ||
import io.kestra.core.serializers.FileSerde; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.*; | ||
import lombok.experimental.SuperBuilder; | ||
import org.slf4j.event.Level; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.OutputStream; | ||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import static io.kestra.core.utils.Rethrow.throwConsumer; | ||
|
||
@SuperBuilder | ||
@ToString | ||
@EqualsAndHashCode | ||
@Getter | ||
@NoArgsConstructor | ||
@Schema( | ||
title = "Output execution logs in a file.", | ||
description = "This task is useful to propagate your logs." | ||
) | ||
@Plugin( | ||
examples = { | ||
@Example( | ||
code = { | ||
"level: ERROR", | ||
} | ||
), | ||
@Example( | ||
code = { | ||
"level: WARN", | ||
"tasksId: ", | ||
" - \"previous-task-id\"" | ||
} | ||
), | ||
@Example( | ||
code = { | ||
"level: WARN", | ||
"executionId: \"{{execution.id}}\"" | ||
} | ||
) | ||
} | ||
) | ||
public class Fetch extends Task implements RunnableTask<Fetch.Output> { | ||
@Schema( | ||
title = "Filter on specific execution", | ||
description = "If not set, will use the current execution" | ||
) | ||
@PluginProperty(dynamic = true) | ||
private String executionId; | ||
|
||
@Schema( | ||
title = "Filter on specific task(s)" | ||
) | ||
@PluginProperty | ||
private Collection<String> tasksId; | ||
|
||
@Schema( | ||
title = "Minimum log level you want to fetch" | ||
) | ||
@Builder.Default | ||
@PluginProperty | ||
private Level level = Level.INFO; | ||
|
||
@Override | ||
public Output run(RunContext runContext) throws Exception { | ||
String executionId = this.executionId != null ? runContext.render(this.executionId) : (String) new HashMap<>((Map<String, Object>) runContext.getVariables().get("execution")).get("id"); | ||
LogRepositoryInterface logRepository = runContext.getApplicationContext().getBean(LogRepositoryInterface.class); | ||
|
||
File tempFile = runContext.tempFile(".ion").toFile(); | ||
AtomicLong count = new AtomicLong(); | ||
|
||
try (OutputStream output = new FileOutputStream(tempFile)) { | ||
if (this.tasksId != null) { | ||
for (String taskId : tasksId) { | ||
logRepository | ||
.findByExecutionIdAndTaskId(executionId, taskId, level) | ||
.forEach(throwConsumer(log -> { | ||
count.incrementAndGet(); | ||
FileSerde.write(output, log); | ||
})); | ||
} | ||
} else { | ||
logRepository | ||
.findByExecutionId(executionId, level) | ||
.forEach(throwConsumer(log -> { | ||
count.incrementAndGet(); | ||
FileSerde.write(output, log); | ||
})); | ||
} | ||
} | ||
|
||
return Output | ||
.builder() | ||
.uri(runContext.putTempFile(tempFile)) | ||
.size(count.get()) | ||
.build(); | ||
} | ||
|
||
@Builder | ||
@Getter | ||
public static class Output implements io.kestra.core.models.tasks.Output { | ||
@Schema( | ||
title = "The size of the fetched rows" | ||
) | ||
private Long size; | ||
|
||
@Schema( | ||
title = "The uri of stored results", | ||
description = "File format is ion" | ||
) | ||
private URI uri; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.kestra.core.tasks; | ||
|
||
import io.kestra.core.models.executions.Execution; | ||
import io.kestra.core.models.executions.TaskRun; | ||
import io.kestra.core.models.flows.State; | ||
import io.kestra.core.repositories.FlowRepositoryInterface; | ||
import io.kestra.core.runners.AbstractMemoryRunnerTest; | ||
import jakarta.inject.Inject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class FetchTest extends AbstractMemoryRunnerTest { | ||
@Inject | ||
FlowRepositoryInterface flowRepository; | ||
|
||
@Test | ||
void fetch() throws Exception { | ||
Execution execution = runnerUtils.runOne("io.kestra.tests", "get-log"); | ||
|
||
assertThat(execution.getState().getCurrent(), is(State.Type.SUCCESS)); | ||
assertThat(execution.getTaskRunList(), hasSize(3)); | ||
TaskRun fetch = execution.getTaskRunList().get(2); | ||
assertThat(fetch.getOutputs().get("size"), is(2)); | ||
} | ||
|
||
@Test | ||
void fetchWithTaskId() throws Exception { | ||
Execution execution = runnerUtils.runOne("io.kestra.tests", "get-log-taskid"); | ||
|
||
assertThat(execution.getState().getCurrent(), is(State.Type.SUCCESS)); | ||
assertThat(execution.getTaskRunList(), hasSize(3)); | ||
TaskRun fetch = execution.getTaskRunList().get(2); | ||
assertThat(fetch.getOutputs().get("size"), is(1)); | ||
} | ||
|
||
@Test | ||
void fetchWithExecutionId() throws Exception { | ||
Execution execution = runnerUtils.runOne("io.kestra.tests", "get-log-executionid"); | ||
|
||
assertThat(execution.getState().getCurrent(), is(State.Type.SUCCESS)); | ||
assertThat(execution.getTaskRunList(), hasSize(3)); | ||
TaskRun fetch = execution.getTaskRunList().get(2); | ||
assertThat(fetch.getOutputs().get("size"), is(2)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/test/resources/flows/valids/get-log-executionid.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
id: get-log-executionid | ||
namespace: io.kestra.tests | ||
tasks: | ||
- type: io.kestra.core.tasks.debugs.Echo | ||
id: task-1 | ||
format: task 1 | ||
- type: io.kestra.core.tasks.debugs.Echo | ||
id: task-2 | ||
format: task 2 | ||
- type: io.kestra.core.tasks.log.Fetch | ||
id: get-log-task | ||
executionId: "{{execution.id}}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
id: get-log-taskid | ||
namespace: io.kestra.tests | ||
tasks: | ||
- type: io.kestra.core.tasks.debugs.Echo | ||
id: task-1 | ||
format: task 1 | ||
- type: io.kestra.core.tasks.debugs.Echo | ||
id: task-2 | ||
format: task 2 | ||
- type: io.kestra.core.tasks.log.Fetch | ||
id: get-log-task | ||
tasksId: | ||
- task-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
id: get-log | ||
namespace: io.kestra.tests | ||
tasks: | ||
- type: io.kestra.core.tasks.debugs.Echo | ||
id: task-1 | ||
format: task 1 | ||
- type: io.kestra.core.tasks.debugs.Echo | ||
id: task-2 | ||
format: task 2 | ||
- type: io.kestra.core.tasks.log.Fetch | ||
id: get-log-task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it needed or is it a leftover of something else ?
Seems not related to this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably leftover of this : f4e378c