com.disney.groovity.tags.Await Maven / Gradle / Ivy
/*******************************************************************************
* © 2018 Disney | ABC Television Group
*
* Licensed under the Apache License, Version 2.0 (the "Apache License")
* with the following modification; you may not use this file except in
* compliance with the Apache License and the following modification to it:
* Section 6. Trademarks. is deleted and replaced with:
*
* 6. Trademarks. This License does not grant permission to use the trade
* names, trademarks, service marks, or product names of the Licensor
* and its affiliates, except as required to comply with Section 4(c) of
* the License and to reproduce the content of the NOTICE file.
*
* You may obtain a copy of the Apache License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Apache License with the above modification is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the Apache License for the specific
* language governing permissions and limitations under the Apache License.
*******************************************************************************/
package com.disney.groovity.tags;
import java.io.CharArrayWriter;
import java.io.Writer;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.disney.groovity.Groovity;
import com.disney.groovity.GroovityConstants;
import com.disney.groovity.Taggable;
import com.disney.groovity.doc.Attr;
import com.disney.groovity.doc.Tag;
import com.disney.groovity.stats.GroovityStatistics;
import com.disney.groovity.stats.GroovityStatistics.Execution;
import com.disney.groovity.util.DeadlockFreeExecutor;
import com.disney.groovity.util.InterruptFactory;
import com.disney.groovity.util.ScriptHelper;
import groovy.lang.Binding;
import groovy.lang.Closure;
/**
* Await and aggregate the results of all async tags in the body; also buffers and replays asynchronous output in order with synchronous output
*
* param(
* - var:
* the name of the variable to bind the results list,
* - timeout:
* number of seconds to wait before throwing an exception,
* - pool:
* max number of threads to occupy - allocates an isolated pool allocates an isolated pool unless already running in an isolated pool,
*
{
* // script containing asynchronous calls to wait upon
* });
*
* returns An ordered list of the return values of futures generated by the body
*
*
Sample
*
* await{async{3*4} async{5*6}}.sum(0)
*
* @author Alex Vigdor
*/
@Tag(
info = "Await and aggregate the results of all async tags in the body; also buffers and replays asynchronous output in order with synchronous output",
body = "script containing asynchronous calls to wait upon",
sample="await{async{3*4} async{5*6}}.sum(0)",
returns="An ordered list of the return values of futures generated by the body",
attrs = {
@Attr(
name = GroovityConstants.VAR,
info="the name of the variable to bind the results list",
required = false
),
@Attr(
name = GroovityConstants.TIMEOUT,
info="number of seconds to wait before throwing an exception",
required = false
),
@Attr(
name = GroovityConstants.POOL,
info="max number of threads to occupy - allocates an isolated pool unless already running in an isolated pool",
required = false
)
}
)
public class Await implements Taggable {
final static String ASYNC_CONTEXT_BINDING = INTERNAL_BINDING_PREFIX.concat("Await$Context");
InterruptFactory interruptFactory;
public void setGroovity(Groovity groovity) {
this.interruptFactory = groovity.getInterruptFactory();
}
@SuppressWarnings({"rawtypes","unchecked"})
@Override
public Object tag(final Map attributes, final Closure body) throws Exception {
final Integer timeoutSeconds = resolve(attributes, TIMEOUT, Integer.class);
final ScriptHelper scriptHelper = getScriptHelper(body);
final Binding binding = scriptHelper.getBinding();
final Map variables = binding.getVariables();
DeadlockFreeExecutor createdThreadPool = null;
DeadlockFreeExecutor oldThreadPool = null;
if(!variables.containsKey(Async.EXECUTOR_BINDING)){
Integer numThreads = resolve(attributes,POOL,Integer.class);
if(numThreads!=null){
createdThreadPool = new DeadlockFreeExecutor(interruptFactory, numThreads);
oldThreadPool = (DeadlockFreeExecutor) variables.put(Async.EXECUTOR_BINDING, createdThreadPool);
}
}
final Writer origOut = (Writer) variables.get(OUT);
StringBuilder label = new StringBuilder();
Object o = GroovityStatistics.currentStackKey();
if(o!=null) {
label.append(o.toString());
}
label.append("[await]");
final String el = label.toString();
GroovityStatistics.startExecution(el);
final AwaitContext awaitContext = AwaitContext.create(variables);
try{
Collection