private static Task<?>[] asTasks(TaskAdaptable<?> ...tasks) { Task<?>[] result = new Task<?>[tasks.length]; for (int i=0; i<tasks.length; i++) result[i] = tasks[i].asTask(); return result; }
private TaskQueueingResult(TaskAdaptable<T> task, boolean wasQueued) { this.task = task.asTask(); this.wasQueued = wasQueued; } @Override
public static void addTagsDynamically(TaskAdaptable<?> task, final Object tag1, final Object ...tags) { ((BasicTask<?>)task.asTask()).applyTagModifier(new Function<Set<Object>, Void>() { @Override public Void apply(@Nullable Set<Object> input) { input.add(tag1); for (Object tag: tags) input.add(tag); return null; } }); }
public static void addTagDynamically(TaskAdaptable<?> task, final Object tag) { ((BasicTask<?>)task.asTask()).applyTagModifier(new Function<Set<Object>, Void>() { @Override public Void apply(@Nullable Set<Object> input) { input.add(tag); return null; } }); }
public static boolean isQueuedOrSubmitted(TaskAdaptable<?> task) { return ((TaskInternal<?>)task.asTask()).isQueuedOrSubmitted(); }
@Override public T get() { return task.asTask().getUnchecked(); } };
public static boolean isQueued(TaskAdaptable<?> task) { return ((TaskInternal<?>)task.asTask()).isQueued(); }
/** @see #submit(Map, Runnable) */ @Override public <T> Task<T> submit(Map<?,?> properties, TaskAdaptable<T> task) { return submitInternal(properties, task.asTask()); }
/** @see #submit(Map, Runnable) */ @Override public <T> Task<T> submit(TaskAdaptable<T> task) { return submitInternal(Maps.newLinkedHashMap(), task.asTask()); }
public static boolean isSubmitted(TaskAdaptable<?> task) { return ((TaskInternal<?>)task.asTask()).isSubmitted(); }
protected static Task<String> executeSqlOnNodeAsync(MySqlNode node, String commands) { return DynamicTasks.queue(Effectors.invocation(node, MySqlNode.EXECUTE_SCRIPT, ImmutableMap.of("commands", commands))).asTask(); }
/** * Adds the given task to the given context. Does not throw an exception if the addition fails. * @return true if the task was added, false otherwise. */ public static boolean tryQueueing(TaskQueueingContext adder, TaskAdaptable<?> task) { if (task==null || isQueued(task)) return false; try { adder.queue(task.asTask()); return true; } catch (Exception e) { if (log.isDebugEnabled()) log.debug("Could not add task "+task+" at "+adder+": "+e); return false; } }
/** * Submits a task to run at the entity. * * @return the task passed in, for fluency */ public static <T extends TaskAdaptable<?>> T submit(final Entity entity, final T task) { final ExecutionContext executionContext = ((EntityInternal)entity).getExecutionContext(); executionContext.submit(task.asTask()); return task; }
@Override public <T> Task<T> submit(Map<?,?> flags, TaskAdaptable<T> task) { if (!(task instanceof Task)) task = task.asTask(); synchronized (task) { if (((TaskInternal<?>)task).getInternalFuture()!=null) return (Task<T>)task; return submitNewTask(flags, (Task<T>) task); } }
/** submits/queues the given task if needed, and gets the result (unchecked) * only permitted in a queueing context (ie a DST main job) if the task is not yet submitted */ // things get really confusing if you try to queueInTaskHierarchy -- easy to cause deadlocks! public static <T> T get(TaskAdaptable<T> t) { return queueIfNeeded(t).asTask().getUnchecked(); }
/** Breaks the parent-child relation between Tasks.current() and the task passed, * making the new task a top-level one at the target entity. * To make it visible in the UI, also tag the task with: * .tag(BrooklynTaskTags.tagForContextEntity(entity)) * .tag(BrooklynTaskTags.NON_TRANSIENT_TASK_TAG) */ public static <T> Task<T> submitTopLevelTask(TaskAdaptable<T> task, Entity entity) { Task<?> currentTask = BasicExecutionManager.getPerThreadCurrentTask().get(); BasicExecutionManager.getPerThreadCurrentTask().set(null); try { return Entities.submit(entity, task).asTask(); } finally { BasicExecutionManager.getPerThreadCurrentTask().set(currentTask); } }
@Override public Entity addExistingMachine(MachineLocation machine) { LOG.info("Adding additional machine to {}: {}", this, machine); Entity added = addNode(machine, MutableMap.of(REMOVABLE, false)); Map<String, ?> args = ImmutableMap.of("locations", ImmutableList.of(machine)); Task<Void> task = Effectors.invocation(added, Startable.START, args).asTask(); DynamicTasks.queueIfPossible(task).orSubmitAsync(this); return added; }
@Override public T call(ConfigBag parameters) { TaskAdaptable<T> t = DynamicTasks.queue(effectorTaskFactory.newTask(entity, effector, parameters)); return t.asTask().getUnchecked(); } }).newTask(entity, effector, parameters);
/** * Submits a {@link TaskFactory} to construct its task at the entity (in a precursor task) and then to submit it. * <p> * Important if task construction relies on an entity being in scope (in tags, via {@link BrooklynTaskTags}) */ public static <T extends TaskAdaptable<?>> T submit(final Entity entity, final TaskFactory<T> taskFactory) { // TODO it is messy to have to do this, but not sure there is a cleaner way :( final Semaphore s = new Semaphore(0); final AtomicReference<T> result = new AtomicReference<T>(); final ExecutionContext executionContext = ((EntityInternal)entity).getExecutionContext(); executionContext.execute(new Runnable() { // TODO could give this task a name, like "create task from factory" @Override public void run() { T t = taskFactory.newTask(); result.set(t); s.release(); } }); try { s.acquire(); } catch (InterruptedException e) { throw Exceptions.propagate(e); } executionContext.submit(result.get().asTask()); return result.get(); }