Search in sources :

Example 46 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class MagicCommand method executeLastFrame.

@Override
public void executeLastFrame(Code code, KernelFunctionality kernel, Message message, int executionCount) {
    MagicCommandOutcomeItem execute = execute(code, executionCount, true);
    sendRepliesWithStatus(singletonList(execute), kernel, message, executionCount);
    TryResult result = execute.getResult();
    SimpleEvaluationObject seo = execute.getSimpleEvaluationObject();
    handleResult(seo, result);
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) MagicCommandOutcomeItem(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem)

Example 47 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class BeakerCellExecutor method executeTask.

@Override
public TryResult executeTask(Callable<TryResult> tsk) {
    Future<TryResult> ret;
    try {
        theLock.lock();
        ret = worker.submit(tsk);
    } catch (Throwable t) {
        t.printStackTrace();
        return TryResult.createError(t.getMessage());
    } finally {
        theLock.unlock();
    }
    TryResult o = null;
    try {
        o = ret.get();
    } catch (Exception e) {
        e.printStackTrace();
        return TryResult.createError(e.getMessage());
    }
    if (ret.isCancelled())
        return TryResult.createError("Cancelled");
    return o;
}
Also used : TryResult(com.twosigma.beakerx.TryResult)

Example 48 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class ClojureWorkerThread method call.

@Override
public TryResult call() throws Exception {
    TryResult r = null;
    try {
        j.outputObject.started();
        r = clojureEvaluator.executeTask(new ClojureCodeRunner(clojureEvaluator, j.codeToBeExecuted, j.outputObject));
    } catch (Throwable e) {
        logger.error(e.getMessage());
        r = TryResult.createError(e.getLocalizedMessage());
    }
    return r;
}
Also used : TryResult(com.twosigma.beakerx.TryResult)

Example 49 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class GroovyCodeRunner method call.

@Override
public TryResult call() throws Exception {
    ClassLoader oldld = Thread.currentThread().getContextClassLoader();
    TryResult either;
    String scriptName = SCRIPT_NAME;
    try {
        Object result = null;
        theOutput.setOutputHandler();
        Thread.currentThread().setContextClassLoader(groovyEvaluator.getGroovyClassLoader());
        scriptName += System.currentTimeMillis();
        Class<?> parsedClass = groovyEvaluator.getGroovyClassLoader().parseClass(theCode, scriptName);
        Script instance = (Script) parsedClass.newInstance();
        if (GroovyEvaluator.LOCAL_DEV) {
            groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, new HashMap<String, Object>());
        } else {
            groovyEvaluator.getScriptBinding().setVariable(Evaluator.BEAKER_VARIABLE_NAME, NamespaceClient.getBeaker(groovyEvaluator.getSessionId()));
        }
        instance.setBinding(groovyEvaluator.getScriptBinding());
        InternalVariable.setValue(theOutput);
        result = instance.run();
        if (GroovyEvaluator.LOCAL_DEV) {
            logger.info("Result: {}", result);
            logger.info("Variables: {}", groovyEvaluator.getScriptBinding().getVariables());
        }
        either = TryResult.createResult(result);
    } catch (Throwable e) {
        if (GroovyEvaluator.LOCAL_DEV) {
            logger.warn(e.getMessage());
            e.printStackTrace();
        }
        // unwrap ITE
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        if (e instanceof InterruptedException || e instanceof InvocationTargetException || e instanceof ThreadDeath) {
            either = TryResult.createError(INTERUPTED_MSG);
        } else {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            StackTraceUtils.sanitize(e).printStackTrace(pw);
            String value = sw.toString();
            value = printStacktrace(scriptName, value);
            either = TryResult.createError(value);
        }
    } finally {
        theOutput.clrOutputHandler();
        Thread.currentThread().setContextClassLoader(oldld);
    }
    return either;
}
Also used : Script(groovy.lang.Script) TryResult(com.twosigma.beakerx.TryResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) StringWriter(java.io.StringWriter) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) PrintWriter(java.io.PrintWriter)

Example 50 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class GroovyWorkerThread method call.

@Override
public TryResult call() throws Exception {
    NamespaceClient nc = null;
    TryResult r;
    try {
        if (!GroovyEvaluator.LOCAL_DEV) {
            nc = NamespaceClient.getBeaker(groovyEvaluator.getSessionId());
            nc.setOutputObj(j.outputObject);
        }
        j.outputObject.started();
        String code = j.codeToBeExecuted;
        r = groovyEvaluator.executeTask(new GroovyCodeRunner(groovyEvaluator, code, j.outputObject));
        if (nc != null) {
            nc.setOutputObj(null);
            nc = null;
        }
    } catch (Throwable e) {
        if (e instanceof GroovyNotFoundException) {
            logger.warn(e.getLocalizedMessage());
            r = TryResult.createError(e.getLocalizedMessage());
        } else {
            e.printStackTrace();
            r = TryResult.createError(e.getLocalizedMessage());
        }
    } finally {
        if (nc != null) {
            nc.setOutputObj(null);
            nc = null;
        }
    }
    return r;
}
Also used : NamespaceClient(com.twosigma.beakerx.NamespaceClient) TryResult(com.twosigma.beakerx.TryResult)

Aggregations

TryResult (com.twosigma.beakerx.TryResult)55 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)41 Test (org.junit.Test)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 NamespaceClient (com.twosigma.beakerx.NamespaceClient)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 KernelTest (com.twosigma.beakerx.KernelTest)4 EvaluatorParameters (com.twosigma.beakerx.kernel.EvaluatorParameters)3 PlainCode.createSimpleEvaluationObject (com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject)3 MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)3 HashMap (java.util.HashMap)3 Plot (com.twosigma.beakerx.chart.xychart.Plot)2 MagicCommandOutcomeItem (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem)2 Message (com.twosigma.beakerx.message.Message)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 ReplEvalResult (org.jetbrains.kotlin.cli.common.repl.ReplEvalResult)2 Code (com.twosigma.beakerx.kernel.Code)1