Skip to content
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(cli): SubmitQueuedCommand to submit queued executions #2477

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.kestra.cli.commands.sys;

import io.kestra.cli.AbstractCommand;
import io.kestra.core.models.executions.Execution;
import io.kestra.core.models.flows.State;
import io.kestra.core.queues.QueueFactoryInterface;
import io.kestra.core.queues.QueueInterface;
import io.kestra.core.runners.ExecutionQueued;
import io.kestra.jdbc.runner.AbstractJdbcExecutionQueuedStorage;
import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import lombok.extern.slf4j.Slf4j;
import picocli.CommandLine;

import java.util.Optional;

@CommandLine.Command(
name = "submit-queued-execution",
description = {"Submit all queued execution to the executor",
"All queued execution will be submitted to the executor. Warning, if there is still running executions and concurrency limit configured, the executions may be queued again."
}
)
@Slf4j
public class SubmitQueuedCommand extends AbstractCommand {
@Inject
private ApplicationContext applicationContext;

@Inject
@Named(QueueFactoryInterface.EXECUTION_NAMED)
private QueueInterface<Execution> executionQueue;

@Override
public Integer call() throws Exception {
super.call();

Optional<String> queueType = applicationContext.getProperty("kestra.queue.type", String.class);
if (queueType.isEmpty()) {
stdOut("Unable to submit queued executions, the 'kestra.queue.type' configuration is not set");
return 0;
}

int cpt = 0;
if (queueType.get().equals("kafka")) {
stdOut("Unable to submit queued executions, the 'kestra.queue.type' configuration is set to 'kafka', use the corresponding sys-ee command");
return 1;
}
else if (queueType.get().equals("postgres") || queueType.get().equals("mysql") || queueType.get().equals("h2")) {
var executionQueuedStorage = applicationContext.getBean(AbstractJdbcExecutionQueuedStorage.class);

for (ExecutionQueued queued : executionQueuedStorage.getAllForAllTenants()) {
executionQueuedStorage.pop(queued.getTenantId(), queued.getNamespace(), queued.getFlowId(), execution -> executionQueue.emit(execution.withState(State.Type.CREATED)));
cpt++;
}
}
else {
stdOut("Unable to submit queued executions, the 'kestra.queue.type' is set to an unknown type '{0}'", queueType.get());
return 1;
}

stdOut("Successfully submitted {0} queued executions", cpt);
return 0;
}
}
3 changes: 2 additions & 1 deletion cli/src/main/java/io/kestra/cli/commands/sys/SysCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
mixinStandardHelpOptions = true,
subcommands = {
ReindexCommand.class,
DatabaseCommand.class
DatabaseCommand.class,
SubmitQueuedCommand.class
}
)
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jooq.Field;
import org.jooq.impl.DSL;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -45,4 +46,19 @@ public void pop(String tenantId, String namespace, String flowId, Consumer<Execu
});
}

/**
* This method should only be used for administration purpose via a command
*/
public List<ExecutionQueued> getAllForAllTenants() {
return this.jdbcRepository
.getDslContextWrapper()
.transactionResult(configuration -> {
var select = DSL
.using(configuration)
.select(AbstractJdbcRepository.field("value"))
.from(this.jdbcRepository.getTable());

return this.jdbcRepository.fetch(select);
});
}
}