diff --git a/plugin-script/src/main/java/io/kestra/plugin/scripts/exec/scripts/runners/DockerScriptRunner.java b/plugin-script/src/main/java/io/kestra/plugin/scripts/exec/scripts/runners/DockerScriptRunner.java index ad5c0311..e16d025b 100644 --- a/plugin-script/src/main/java/io/kestra/plugin/scripts/exec/scripts/runners/DockerScriptRunner.java +++ b/plugin-script/src/main/java/io/kestra/plugin/scripts/exec/scripts/runners/DockerScriptRunner.java @@ -45,7 +45,7 @@ public class DockerScriptRunner { private static final ReadableBytesTypeConverter READABLE_BYTES_TYPE_CONVERTER = new ReadableBytesTypeConverter(); - public static final Pattern NEWLINE_PATTERN = Pattern.compile("[\\r\\n]+$"); + public static final Pattern NEWLINE_PATTERN = Pattern.compile("([^\\r\\n]+)[\\r\\n]+"); private final RetryUtils retryUtils; @@ -127,13 +127,21 @@ public void onNext(Frame frame) { String frameStr = new String(frame.getPayload()); Matcher newLineMatcher = NEWLINE_PATTERN.matcher(frameStr); - logBuffers.computeIfAbsent(frame.getStreamType(), streamType -> new StringBuilder()) - .append(newLineMatcher.replaceAll("")); - if (newLineMatcher.reset().find()) { + int lastIndex = 0; + while (newLineMatcher.find()) { + String fragment = newLineMatcher.group(1); + logBuffers.computeIfAbsent(frame.getStreamType(), streamType -> new StringBuilder()) + .append(fragment); + StringBuilder logBuffer = logBuffers.get(frame.getStreamType()); defaultLogConsumer.accept(logBuffer.toString(), frame.getStreamType() == StreamType.STDERR); logBuffer.setLength(0); + lastIndex = newLineMatcher.end(); + } + if (lastIndex < frameStr.length()) { + logBuffers.computeIfAbsent(frame.getStreamType(), streamType -> new StringBuilder()) + .append(frameStr.substring(lastIndex)); } } diff --git a/plugin-script/src/test/java/io/kestra/plugin/scripts/runners/LogConsumerTest.java b/plugin-script/src/test/java/io/kestra/plugin/scripts/runners/LogConsumerTest.java index 96b5875d..2193de84 100644 --- a/plugin-script/src/test/java/io/kestra/plugin/scripts/runners/LogConsumerTest.java +++ b/plugin-script/src/test/java/io/kestra/plugin/scripts/runners/LogConsumerTest.java @@ -55,4 +55,37 @@ public String getType() { assertThat(run.getLogConsumer().getStdOutCount(), is(2)); assertThat(run.getLogConsumer().getOutputs().get("someOutput"), is(outputValue)); } + + @Test + void carryreturn() throws Exception { + Task task = new Task() { + @Override + public String getId() { + return "id"; + } + + @Override + public String getType() { + return "type"; + } + }; + RunContext runContext = TestsUtils.mockRunContext(runContextFactory, task, ImmutableMap.of()); + StringBuilder outputValue = new StringBuilder(); + for (int i = 0; i < 3; i++) { + outputValue.append(Integer.toString(i).repeat(100)).append("\r") + .append(Integer.toString(i).repeat(800)).append("\r") + .append(Integer.toString(i).repeat(2000)).append("\r"); + } + RunnerResult run = new DockerScriptRunner(applicationContext).run( + new CommandsWrapper(runContext).withCommands(List.of( + "/bin/sh", "-c", + "echo " + outputValue + + "echo -n another line" + )), + DockerOptions.builder().image("alpine").build() + ); + + Await.until(() -> run.getLogConsumer().getStdOutCount() == 10, null, Duration.ofSeconds(5)); + assertThat(run.getLogConsumer().getStdOutCount(), is(10)); + } }