@Override public void initialize(RuntimeContext ctx) { ctx.addInvoker(new SpringCloudFunctionInvoker(ctx.getMethod().getTargetClass()),FunctionInvoker.Phase.Call); } }
public ThumbnailsFunction(RuntimeContext ctx) { storageUrl = ctx.getConfigurationByKey("OBJECT_STORAGE_URL") .orElseThrow(() -> new RuntimeException("Missing configuration: OBJECT_STORAGE_URL")); storageAccessKey = ctx.getConfigurationByKey("OBJECT_STORAGE_ACCESS") .orElseThrow(() -> new RuntimeException("Missing configuration: OBJECT_STORAGE_ACCESS")); storageSecretKey = ctx.getConfigurationByKey("OBJECT_STORAGE_SECRET") .orElseThrow(() -> new RuntimeException("Missing configuration: OBJECT_STORAGE_SECRET")); resize128ID = ctx.getConfigurationByKey("RESIZE_128_FN_ID") .orElseThrow(() -> new RuntimeException("Missing configuration: RESIZE_128_FN_ID")); resize256ID = ctx.getConfigurationByKey("RESIZE_256_FN_ID") .orElseThrow(() -> new RuntimeException("Missing configuration: RESIZE_256_FN_ID")); resize512ID = ctx.getConfigurationByKey("RESIZE_512_FN_ID") .orElseThrow(() -> new RuntimeException("Missing configuration: RESIZE_512_FN_ID")); }
private static ObjectMapper objectMapper(InvocationContext ctx) { Optional<ObjectMapper> omo = ctx.getRuntimeContext().getAttribute(OM_KEY, ObjectMapper.class); if (!omo.isPresent()) { ObjectMapper om = new ObjectMapper(); ctx.getRuntimeContext().setAttribute(OM_KEY, om); return om; } else { return omo.get(); } }
/** * Set an {@link FunctionInvoker} for this function. The invoker will override * the built in function invoker, although the cloud threads invoker will still * have precedence so that cloud threads can be used from functions using custom invokers. * * @param invoker The {@link FunctionInvoker} to add. * @deprecated this is equivalent to {@link #addInvoker(FunctionInvoker, FunctionInvoker.Phase)} with a phase of {@link FunctionInvoker.Phase#Call} */ default void setInvoker(FunctionInvoker invoker) { addInvoker(invoker, FunctionInvoker.Phase.Call); }
private Optional<OutputEvent> coerceReturnValue(InvocationContext ctx, MethodWrapper method, Object rawResult) { try { return Optional.of((ctx.getRuntimeContext()).getOutputCoercions(method.getTargetMethod()) .stream() .map((c) -> c.wrapFunctionResult(ctx, method, rawResult)) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new FunctionOutputHandlingException("No coercion found for return type"))); } catch (RuntimeException e) { throw new FunctionOutputHandlingException("An exception was thrown during Output Coercion: " + e.getMessage(), e); } }
private Object coerceParameter(InvocationContext ctx, MethodWrapper targetMethod, int param, InputEvent evt) { RuntimeContext runtimeContext = ctx.getRuntimeContext(); return runtimeContext.getInputCoercions(targetMethod, param) .stream() .map((c) -> c.tryCoerceParam(ctx, param, evt, targetMethod)) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new FunctionInputHandlingException("No type coercion for argument " + param + " of " + targetMethod + " of found")); }
/** * Invoke the function wrapped by this loader * * @param evt The function event * @return the function response * @throws InternalFunctionInvocationException if the invocation fails */ @Override public Optional<OutputEvent> tryInvoke(InvocationContext ctx, InputEvent evt) throws InternalFunctionInvocationException { FunctionRuntimeContext runtimeContext = (FunctionRuntimeContext) ctx.getRuntimeContext(); MethodWrapper method = runtimeContext.getMethodWrapper(); Object[] userFunctionParams = coerceParameters(ctx, method, evt); Object rawResult; try { rawResult = method.getTargetMethod().invoke(ctx.getRuntimeContext().getInvokeInstance().orElse(null), userFunctionParams); } catch (IllegalAccessException | InvocationTargetException e) { throw new InternalFunctionInvocationException(e.getCause().getMessage(), e.getCause()); } return coerceReturnValue(ctx, method, rawResult); }
@Override public synchronized Flow currentFlow() { if (runtime == null) { String functionId = ctx.getRuntimeContext().getFunctionID(); CompleterClientFactory factory = getOrCreateCompleterClientFactory(completerBaseUrl); final FlowId flowId = factory.getCompleterClient().createFlow(functionId); runtime = new RemoteFlow(flowId); InvocationListener flowInvocationListener = new InvocationListener() { @Override public void onSuccess() { factory.getCompleterClient().commit(flowId); } public void onFailure() { factory.getCompleterClient().commit(flowId); } }; ctx.addListener(flowInvocationListener); } return runtime; } };
@Override public void initialize(RuntimeContext context){ FunctionInvoker invoker = new FlowContinuationInvoker(); context.addInvoker(invoker,FunctionInvoker.Phase.PreCall); } }
protected Optional<OutputEvent> coerceReturnValue(InvocationContext ctx, MethodWrapper method, Object rawResult) { try { return Optional.of(ctx.getRuntimeContext().getOutputCoercions(method.getTargetMethod()) .stream() .map((c) -> c.wrapFunctionResult(ctx, method, rawResult)) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new FunctionOutputHandlingException("No coercion found for return type"))); } catch (RuntimeException e) { throw new FunctionOutputHandlingException("An exception was thrown during Output Coercion: " + e.getMessage(), e); } }
private Object coerceParameter(InvocationContext ctx, MethodWrapper targetMethod, int param, InputEvent evt) { RuntimeContext runtimeContext = ctx.getRuntimeContext(); return runtimeContext.getInputCoercions(targetMethod, param) .stream() .map((c) -> c.tryCoerceParam(ctx, param, evt, targetMethod)) .filter(Optional::isPresent) .map(Optional::get) .findFirst() .orElseThrow(() -> new FunctionInputHandlingException("No type coercion for argument " + param + " of " + targetMethod + " of found")); }
Optional<String> graphIdOption = evt.getHeaders().get(FLOW_ID_HEADER); final String completerBaseUrl = ctx.getRuntimeContext().getConfigurationByKey(COMPLETER_BASE_URL).orElse(DEFAULT_COMPLETER_BASE_URL);