Search in sources :

Example 1 with ScriptExecutionRequestPayload

use of org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload in project apex-malhar by apache.

the class InterpreterThread method processCommand.

/**
 * Responsible for polling the request queue and formatting the request payload to make it compatible to the
 *  individual processing logic of the functionalities provided by the interpreter API methods.
 * @param <T> Java templating signature enforcement
 * @throws ApexPythonInterpreterException if an unrecognized command is issued.
 * @throws InterruptedException if interrupted while trying to wait for a request from request queue
 */
private <T> void processCommand() throws ApexPythonInterpreterException, InterruptedException {
    PythonRequestResponse requestResponseHandle = requestQueue.poll(timeOutToPollFromRequestQueue, timeUnitsToPollFromRequestQueue);
    if (requestResponseHandle != null) {
        LOG.debug("Processing command " + requestResponseHandle.getPythonInterpreterRequest().getCommandType());
        busyFlag = true;
        if (errorEncountered) {
            LOG.debug("Error state detected from a previous command. Resetting state to  the previous" + " state of the error");
            try {
                JEP_INSTANCE.eval(null);
                errorEncountered = false;
            } catch (JepException e) {
                LOG.error("Error while trying to clear the state of the interpreter due to previous command" + " " + e.getMessage(), e);
            }
        }
        PythonInterpreterRequest<T> request = requestResponseHandle.getPythonInterpreterRequest();
        PythonInterpreterResponse<T> response = requestResponseHandle.getPythonInterpreterResponse();
        Map<String, Boolean> commandStatus = new HashMap<>(1);
        switch(request.getCommandType()) {
            case EVAL_COMMAND:
                EvalCommandRequestPayload evalPayload = request.getEvalCommandRequestPayload();
                T responseVal = eval(evalPayload.getEvalCommand(), evalPayload.getVariableNameToExtractInEvalCall(), evalPayload.getParamsForEvalCommand(), evalPayload.isDeleteVariableAfterEvalCall(), request.getExpectedReturnType());
                response.setResponse(responseVal);
                if (responseVal != null) {
                    commandStatus.put(evalPayload.getEvalCommand(), Boolean.TRUE);
                } else {
                    commandStatus.put(evalPayload.getEvalCommand(), Boolean.FALSE);
                }
                response.setCommandStatus(commandStatus);
                break;
            case SCRIPT_COMMAND:
                ScriptExecutionRequestPayload scriptPayload = request.getScriptExecutionRequestPayload();
                if (executeScript(scriptPayload.getScriptName())) {
                    commandStatus.put(scriptPayload.getScriptName(), Boolean.TRUE);
                } else {
                    commandStatus.put(scriptPayload.getScriptName(), Boolean.FALSE);
                }
                response.setCommandStatus(commandStatus);
                break;
            case METHOD_INVOCATION_COMMAND:
                MethodCallRequestPayload requestpayload = request.getMethodCallRequest();
                response.setResponse(executeMethodCall(requestpayload.getNameOfMethod(), requestpayload.getArgs(), request.getExpectedReturnType()));
                if (response.getResponse() == null) {
                    commandStatus.put(requestpayload.getNameOfMethod(), Boolean.FALSE);
                } else {
                    commandStatus.put(requestpayload.getNameOfMethod(), Boolean.TRUE);
                }
                response.setCommandStatus(commandStatus);
                break;
            case GENERIC_COMMANDS:
                response.setCommandStatus(runCommands(request.getGenericCommandsRequestPayload().getGenericCommands()));
                break;
            default:
                throw new ApexPythonInterpreterException(new Exception("Unspecified Interpreter command"));
        }
        requestResponseHandle.setRequestCompletionTime(System.currentTimeMillis());
        responseQueue.put(requestResponseHandle);
        LOG.debug("Submitted the response and executed " + response.getCommandStatus().size() + " instances of command");
    }
    busyFlag = false;
}
Also used : EvalCommandRequestPayload(org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload) HashMap(java.util.HashMap) PythonRequestResponse(org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse) JepException(jep.JepException) JepException(jep.JepException) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException) ScriptExecutionRequestPayload(org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException) MethodCallRequestPayload(org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)

Example 2 with ScriptExecutionRequestPayload

use of org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload in project apex-malhar by apache.

the class BaseJEPTest method setCommonConstructsForRequestResponseObject.

private void setCommonConstructsForRequestResponseObject(PythonCommandType commandType, PythonInterpreterRequest request, PythonRequestResponse requestResponse) throws ApexPythonInterpreterException {
    request.setCommandType(commandType);
    requestResponse.setRequestStartTime(System.currentTimeMillis());
    requestResponse.setRequestId(1L);
    requestResponse.setWindowId(1L);
    switch(commandType) {
        case EVAL_COMMAND:
            EvalCommandRequestPayload payload = new EvalCommandRequestPayload();
            request.setEvalCommandRequestPayload(payload);
            break;
        case METHOD_INVOCATION_COMMAND:
            MethodCallRequestPayload methodCallRequest = new MethodCallRequestPayload();
            request.setMethodCallRequest(methodCallRequest);
            break;
        case SCRIPT_COMMAND:
            ScriptExecutionRequestPayload scriptPayload = new ScriptExecutionRequestPayload();
            request.setScriptExecutionRequestPayload(scriptPayload);
            break;
        case GENERIC_COMMANDS:
            GenericCommandsRequestPayload payloadForGenericCommands = new GenericCommandsRequestPayload();
            request.setGenericCommandsRequestPayload(payloadForGenericCommands);
            break;
        default:
            throw new ApexPythonInterpreterException("Unsupported command type");
    }
}
Also used : EvalCommandRequestPayload(org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload) GenericCommandsRequestPayload(org.apache.apex.malhar.python.base.requestresponse.GenericCommandsRequestPayload) ScriptExecutionRequestPayload(org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload) ApexPythonInterpreterException(org.apache.apex.malhar.python.base.ApexPythonInterpreterException) MethodCallRequestPayload(org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)

Example 3 with ScriptExecutionRequestPayload

use of org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload in project apex-malhar by apache.

the class PythonRequestResponseUtil method buildRequestForScriptCallCommand.

/**
 * Builds a request object that can be used for executing the script call commands.
 * @param scriptPath Full path to the file name containing the script
 * @param timeOut The time that can be used to complete the execution of the script
 * @param timeUnit Unit of time for time out parameter
 * @param clazz The class that can be used to represent the return type
 * @param <T> Java template for type inference
 * @return The Request object that can be used for a script call invocation
 */
public static <T> PythonInterpreterRequest<T> buildRequestForScriptCallCommand(String scriptPath, long timeOut, TimeUnit timeUnit, Class<T> clazz) {
    PythonInterpreterRequest<T> request = new PythonInterpreterRequest<>(clazz);
    ScriptExecutionRequestPayload scriptExecutionRequestPayload = new ScriptExecutionRequestPayload();
    scriptExecutionRequestPayload.setScriptName(scriptPath);
    request.setTimeUnit(timeUnit);
    request.setTimeout(timeOut);
    request.setScriptExecutionRequestPayload(scriptExecutionRequestPayload);
    return request;
}
Also used : ScriptExecutionRequestPayload(org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload) PythonInterpreterRequest(org.apache.apex.malhar.python.base.requestresponse.PythonInterpreterRequest)

Aggregations

ScriptExecutionRequestPayload (org.apache.apex.malhar.python.base.requestresponse.ScriptExecutionRequestPayload)3 ApexPythonInterpreterException (org.apache.apex.malhar.python.base.ApexPythonInterpreterException)2 EvalCommandRequestPayload (org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload)2 MethodCallRequestPayload (org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload)2 HashMap (java.util.HashMap)1 JepException (jep.JepException)1 GenericCommandsRequestPayload (org.apache.apex.malhar.python.base.requestresponse.GenericCommandsRequestPayload)1 PythonInterpreterRequest (org.apache.apex.malhar.python.base.requestresponse.PythonInterpreterRequest)1 PythonRequestResponse (org.apache.apex.malhar.python.base.requestresponse.PythonRequestResponse)1