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

KOGITO-9522, KOGITO-8841 Add Jobs and process operations to Data Index addons gatewayAPI #1824

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions data-index/data-index-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-routes</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.index.api;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.kie.kogito.index.model.Job;
import org.kie.kogito.index.service.DataIndexServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.quarkus.security.credential.TokenCredential;
import io.quarkus.security.identity.SecurityIdentity;
import io.vertx.core.AsyncResult;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;

import static java.lang.String.format;

@ApplicationScoped
public class KogitoRuntimeCommonClient {

public static final String CANCEL_JOB_PATH = "/jobs/%s";
public static final String RESCHEDULE_JOB_PATH = "/jobs/%s";

public static final String FROM_PROCESS_INSTANCE_WITH_ID = "from ProcessInstance with id: ";

private static final Logger LOGGER = LoggerFactory.getLogger(KogitoRuntimeCommonClient.class);

protected Vertx vertx;

protected SecurityIdentity identity;

protected Map<String, WebClient> serviceWebClientMap = new HashMap<>();

@ConfigProperty(name = "kogito.dataindex.gateway.url")
protected Optional<String> gatewayTargetUrl;

public void setGatewayTargetUrl(Optional<String> gatewayTargetUrl) {
this.gatewayTargetUrl = gatewayTargetUrl;
}

public void addServiceWebClient(String serviceUrl, WebClient webClient) {
serviceWebClientMap.put(serviceUrl, webClient);
}

protected WebClient getWebClient(String runtimeServiceUrl) {
if (runtimeServiceUrl == null) {
throw new DataIndexServiceException("Runtime service URL not defined, please review the kogito.service.url system property to point the public URL for this runtime.");
} else {
return serviceWebClientMap.computeIfAbsent(runtimeServiceUrl, url -> WebClient.create(vertx, getWebClientToURLOptions(runtimeServiceUrl)));
}
}

public WebClientOptions getWebClientToURLOptions(String targetHttpURL) {
try {
URL dataIndexURL = new URL(targetHttpURL);
return new WebClientOptions()
.setDefaultHost(gatewayTargetUrl.orElse(dataIndexURL.getHost()))
.setDefaultPort((dataIndexURL.getPort() != -1 ? dataIndexURL.getPort() : dataIndexURL.getDefaultPort()))
.setSsl(dataIndexURL.getProtocol().compareToIgnoreCase("https") == 0);
} catch (MalformedURLException ex) {
LOGGER.error(String.format("Invalid runtime service URL: %s", targetHttpURL), ex);
return null;
}
}

public CompletableFuture<String> cancelJob(String serviceURL, Job job) {
String requestURI = format(CANCEL_JOB_PATH, job.getId());
LOGGER.debug("Sending DELETE to URI {}", requestURI);
return sendDeleteClientRequest(getWebClient(serviceURL), requestURI, "CANCEL Job with id: " + job.getId());
}

public CompletableFuture<String> rescheduleJob(String serviceURL, Job job, String newJobData) {
String requestURI = format(RESCHEDULE_JOB_PATH, job.getId());
LOGGER.debug("Sending body: {} PATCH to URI {}", newJobData, requestURI);
return sendPatchClientRequest(getWebClient(serviceURL), requestURI,
"RESCHEDULED JOB with id: " + job.getId(), new JsonObject(newJobData));
}

public CompletableFuture sendDeleteClientRequest(WebClient webClient, String requestURI, String logMessage) {
CompletableFuture future = new CompletableFuture<>();
webClient.delete(requestURI)
.putHeader("Authorization", getAuthHeader())
.send(res -> asyncHttpResponseTreatment(res, future, logMessage));
LOGGER.debug("Sending DELETE to URI {}", requestURI);
return future;
}

protected void asyncHttpResponseTreatment(AsyncResult<HttpResponse<Buffer>> res, CompletableFuture future, String logMessage) {
if (res.succeeded() && (res.result().statusCode() == 200 || res.result().statusCode() == 201)) {
future.complete(res.result().bodyAsString() != null ? res.result().bodyAsString() : "Successfully performed: " + logMessage);
} else {
future.completeExceptionally(new DataIndexServiceException(getErrorMessage(logMessage, res.result())));
}
}

public CompletableFuture sendPatchClientRequest(WebClient webClient, String requestURI, String logMessage, JsonObject jsonBody) {
CompletableFuture future = new CompletableFuture<>();
webClient.patch(requestURI)
.putHeader("Authorization", getAuthHeader())
.sendJson(jsonBody, res -> asyncHttpResponseTreatment(res, future, logMessage));
return future;
}

protected String getErrorMessage(String logMessage, HttpResponse<Buffer> result) {
String errorMessage = "FAILED: " + logMessage;
if (result != null) {
errorMessage += " errorCode:" + result.statusCode() +
" errorStatus:" + result.statusMessage() +
" errorMessage:" + (result.body() != null ? result.body().toString() : "-");
}
return errorMessage;
}

public String getAuthHeader() {
if (identity != null && identity.getCredential(TokenCredential.class) != null) {
return "Bearer " + identity.getCredential(TokenCredential.class).getToken();
}
return "";
}

@Inject
public void setIdentity(SecurityIdentity identity) {
this.identity = identity;
}

@Inject
public void setVertx(Vertx vertx) {
this.vertx = vertx;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.kie.kogito.index.model.Node;
import org.kie.kogito.index.model.ProcessInstance;
import org.kie.kogito.index.model.UserTaskInstance;
import org.kie.kogito.index.service.DataIndexServiceException;
import org.kie.kogito.index.storage.DataIndexStorageService;
import org.kie.kogito.persistence.api.Storage;
import org.kie.kogito.persistence.api.query.Query;
Expand All @@ -49,11 +50,21 @@
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;

import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.kie.kogito.persistence.api.query.QueryFilterFactory.equalTo;

public abstract class AbstractGraphQLSchemaManager implements GraphQLSchemaManager {

private static final String ID = "id";
private static final String USER = "user";
private static final String GROUPS = "groups";
private static final String TASK_ID = "taskId";
private static final String COMMENT_ID = "commentId";
private static final String ATTACHMENT_ID = "attachmentId";

private static final String UNABLE_TO_FIND_ERROR_MSG = "Unable to find the instance with %s %s";

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGraphQLSchemaManager.class);

@Inject
Expand Down Expand Up @@ -213,4 +224,176 @@ public void transform(Consumer<GraphQLSchema.Builder> builder) {
schema = schema.transform(builder);
}

public CompletableFuture<String> abortProcessInstance(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().abortProcessInstance(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()), processInstance);
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> retryProcessInstance(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().retryProcessInstance(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()), processInstance);
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> skipProcessInstance(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().skipProcessInstance(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()), processInstance);
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> updateProcessInstanceVariables(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().updateProcessInstanceVariables(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()), processInstance,
env.getArgument("variables"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a few places where env.getArgument is called without checking the value. Can we do these calls with null values? I guess in most cases we would need to check.


}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> triggerNodeInstance(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().triggerNodeInstance(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()),
processInstance,
env.getArgument("nodeId"));
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> retriggerNodeInstance(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().retriggerNodeInstance(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()),
processInstance,
env.getArgument("nodeInstanceId"));
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> cancelNodeInstance(DataFetchingEnvironment env) {
String id = env.getArgument("id");
ProcessInstance processInstance = getCacheService().getProcessInstancesCache().get(id);
if (processInstance != null) {
return getDataIndexApiExecutor().cancelNodeInstance(getServiceUrl(processInstance.getEndpoint(), processInstance.getProcessId()),
processInstance,
env.getArgument("nodeInstanceId"));
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> cancelJob(DataFetchingEnvironment env) {
String id = env.getArgument("id");
Job job = getCacheService().getJobsCache().get(id);
if (job != null) {
return getDataIndexApiExecutor().cancelJob(job.getEndpoint(), job);
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

public CompletableFuture<String> rescheduleJob(DataFetchingEnvironment env) {
String id = env.getArgument("id");
Job job = getCacheService().getJobsCache().get(id);
if (job != null) {
return getDataIndexApiExecutor().rescheduleJob(job.getEndpoint(), job, env.getArgument("data"));
}
return CompletableFuture.failedFuture(new DataIndexServiceException(format(UNABLE_TO_FIND_ERROR_MSG, ID, id)));
}

protected CompletableFuture<String> getUserTaskInstanceSchema(DataFetchingEnvironment env) {
UserTaskInstance userTaskInstance = env.getSource();
return getDataIndexApiExecutor().getUserTaskSchema(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS));
}

protected CompletableFuture<String> updateUserTaskInstance(DataFetchingEnvironment env) {
UserTaskInstance userTaskInstance = getCacheService().getUserTaskInstancesCache().get(env.getArgument(TASK_ID));
return getDataIndexApiExecutor().updateUserTaskInstance(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArguments());
}

protected CompletableFuture<String> createTaskInstanceComment(DataFetchingEnvironment env) {
UserTaskInstance userTaskInstance = getCacheService().getUserTaskInstancesCache().get(env.getArgument(TASK_ID));
return getDataIndexApiExecutor().createUserTaskInstanceComment(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArgument("comment"));
}

protected CompletableFuture<String> createTaskInstanceAttachment(DataFetchingEnvironment env) {
UserTaskInstance userTaskInstance = getCacheService().getUserTaskInstancesCache().get(env.getArgument(TASK_ID));
return getDataIndexApiExecutor().createUserTaskInstanceAttachment(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArgument("name"),
env.getArgument("uri"));
}

protected CompletableFuture<String> updateUserTaskComment(DataFetchingEnvironment env) {
Query<UserTaskInstance> query = getCacheService().getUserTaskInstancesCache().query();
query.filter(singletonList(equalTo("comments.id", env.getArgument(COMMENT_ID))));
UserTaskInstance userTaskInstance = query.execute().get(0);
return getDataIndexApiExecutor().updateUserTaskInstanceComment(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArgument(COMMENT_ID),
env.getArgument("comment"));
}

protected CompletableFuture<String> deleteUserTaskComment(DataFetchingEnvironment env) {
Query<UserTaskInstance> query = getCacheService().getUserTaskInstancesCache().query();
query.filter(singletonList(equalTo("comments.id", env.getArgument(COMMENT_ID))));
UserTaskInstance userTaskInstance = query.execute().get(0);
return getDataIndexApiExecutor().deleteUserTaskInstanceComment(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArgument(COMMENT_ID));
}

protected CompletableFuture<String> updateUserTaskAttachment(DataFetchingEnvironment env) {
Query<UserTaskInstance> query = getCacheService().getUserTaskInstancesCache().query();
query.filter(singletonList(equalTo("attachments.id", env.getArgument(ATTACHMENT_ID))));
UserTaskInstance userTaskInstance = query.execute().get(0);
return getDataIndexApiExecutor().updateUserTaskInstanceAttachment(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArgument(ATTACHMENT_ID),
env.getArgument("name"),
env.getArgument("uri"));
}

protected CompletableFuture<String> deleteUserTaskAttachment(DataFetchingEnvironment env) {
Query<UserTaskInstance> query = getCacheService().getUserTaskInstancesCache().query();
query.filter(singletonList(equalTo("attachments.id", env.getArgument(ATTACHMENT_ID))));
UserTaskInstance userTaskInstance = query.execute().get(0);
return getDataIndexApiExecutor().deleteUserTaskInstanceAttachment(getServiceUrl(userTaskInstance.getEndpoint(), userTaskInstance.getProcessId()),
userTaskInstance,
env.getArgument(USER),
env.getArgument(GROUPS),
env.getArgument(ATTACHMENT_ID));
}

}
Loading
Loading