Пример #1
0
  @SuppressWarnings("deprecation")
  protected void beforeSubmit(Map flags, Task<?> task) {
    incompleteTaskCount.incrementAndGet();

    Task currentTask = getCurrentTask();
    if (currentTask != null) ((BasicTask) task).submittedByTask = currentTask;
    ((BasicTask) task).submitTimeUtc = System.currentTimeMillis();

    if (flags.get("tag") != null) ((BasicTask) task).tags.add(flags.remove("tag"));
    if (flags.get("tags") != null)
      ((BasicTask) task).tags.addAll((Collection) flags.remove("tags"));

    for (Object tag : ((BasicTask) task).tags) {
      getMutableTasksWithTag(tag).add(task);
    }

    List tagLinkedPreprocessors = new ArrayList();
    for (Object tag : ((BasicTask) task).tags) {
      TaskPreprocessor p = getTaskPreprocessorForTag(tag);
      if (p != null) tagLinkedPreprocessors.add(p);
    }
    flags.put("tagLinkedPreprocessors", tagLinkedPreprocessors);
    for (Object ppo : tagLinkedPreprocessors) {
      TaskPreprocessor t = (TaskPreprocessor) ppo;
      t.onSubmit(flags, task);
    }
  }
Пример #2
0
 public void run() {
   while (true) {
     synchronized (this) {
       task = (tasks.isEmpty()) ? idleTask : (BasicTask) tasks.removeFirst();
       task.setActive();
       notifyAll();
     }
     task.run();
     task.setComplete();
   }
 }
Пример #3
0
 /**
  * convenience for setting "blocking details" on any task where the current thread is running,
  * while the passed code is executed; often used from groovy as
  *
  * <pre>{@code withBlockingDetails("sleeping 5s") { Thread.sleep(5000); } }</pre>
  *
  * If code block is null, the description is set until further notice (not cleareed).
  */
 @SuppressWarnings("rawtypes")
 public static <T> T withBlockingDetails(String description, Callable<T> code) throws Exception {
   Task current = current();
   if (code == null) {
     log.warn("legacy invocation of withBlockingDetails with null code block, ignoring");
     return null;
   }
   if (current instanceof BasicTask) ((BasicTask) current).setBlockingDetails(description);
   try {
     return code.call();
   } finally {
     if (current instanceof BasicTask) ((BasicTask) current).setBlockingDetails(null);
   }
 }
Пример #4
0
  @SuppressWarnings("deprecation")
  protected void afterEnd(Map flags, Task<?> task) {
    activeTaskCount.decrementAndGet();
    incompleteTaskCount.decrementAndGet();

    if (log.isTraceEnabled()) log.trace(this + " afterEnd, task: " + task);
    ExecutionUtils.invoke(flags.get("newTaskEndCallback"), task);
    List l = (List) flags.get("tagLinkedPreprocessors");
    Collections.reverse(l);
    for (Object li : l) {
      TaskPreprocessor t = (TaskPreprocessor) li;
      t.onEnd(flags, task);
    }

    PerThreadCurrentTaskHolder.perThreadCurrentTask.remove();
    ((BasicTask) task).endTimeUtc = System.currentTimeMillis();
    // clear thread _after_ endTime set, so we won't get a null thread when there is no end-time
    if (RENAME_THREADS) {
      String newThreadName = "brooklyn-" + LanguageUtils.newUid();
      ((BasicTask) task).thread.setName(newThreadName);
    }
    ((BasicTask) task).thread = null;
    synchronized (task) {
      task.notifyAll();
    }

    ExpirationPolicy expirationPolicy = (ExpirationPolicy) flags.get("expirationPolicy");
    if (expirationPolicy == null) expirationPolicy = ExpirationPolicy.IMMEDIATE;
    if (expirationPolicy == ExpirationPolicy.IMMEDIATE) {
      for (Object t : ((BasicTask) task).tags) {
        getMutableTasksWithTag(t).remove(task);
      }
    }
  }
Пример #5
0
  @SuppressWarnings("deprecation")
  protected void beforeStart(Map flags, Task<?> task) {
    activeTaskCount.incrementAndGet();

    // set thread _before_ start time, so we won't get a null thread when there is a start-time
    if (log.isTraceEnabled()) log.trace("" + this + " beforeStart, task: " + task);
    if (!task.isCancelled()) {
      ((BasicTask) task).thread = Thread.currentThread();
      if (RENAME_THREADS) {
        String newThreadName =
            "brooklyn-"
                + CaseFormat.LOWER_HYPHEN.to(
                    CaseFormat.LOWER_CAMEL, task.getDisplayName().replace(" ", ""))
                + "-"
                + task.getId().substring(0, 8);
        ((BasicTask) task).thread.setName(newThreadName);
      }
      PerThreadCurrentTaskHolder.perThreadCurrentTask.set(task);
      ((BasicTask) task).startTimeUtc = System.currentTimeMillis();
    }
    for (Object to : (Collection) flags.get("tagLinkedPreprocessors")) {
      TaskPreprocessor t = (TaskPreprocessor) to;
      t.onStart(flags, task);
    }
    ExecutionUtils.invoke(flags.get("newTaskStartCallback"), task);
  }
Пример #6
0
 /** Finish everything and return */
 public void finish() {
   while (true) {
     synchronized (this) {
       task.finish();
       if (tasks.isEmpty()) {
         return;
       } else {
         try {
           wait();
         } catch (InterruptedException e) {
         }
       }
     }
   }
 }
Пример #7
0
  protected <T> Task<T> submitNewTask(final Map flags, final Task<T> task) {
    if (task instanceof ScheduledTask) return submitNewScheduledTask(flags, (ScheduledTask) task);

    totalTaskCount.incrementAndGet();

    beforeSubmit(flags, task);

    if (((BasicTask) task).job == null)
      throw new NullPointerException(
          "Task " + task + " submitted with with null job: job must be supplied.");

    Callable job =
        new Callable() {
          public Object call() {
            Object result = null;
            Throwable error = null;
            String oldThreadName = Thread.currentThread().getName();
            try {
              if (RENAME_THREADS) {
                String newThreadName =
                    oldThreadName
                        + "-"
                        + task.getDisplayName()
                        + "["
                        + task.getId().substring(0, 8)
                        + "]";
                Thread.currentThread().setName(newThreadName);
              }
              beforeStart(flags, task);
              if (!task.isCancelled()) {
                result = ((BasicTask) task).job.call();
              } else throw new CancellationException();
            } catch (Throwable e) {
              error = e;
            } finally {
              if (RENAME_THREADS) {
                Thread.currentThread().setName(oldThreadName);
              }
              afterEnd(flags, task);
            }
            if (error != null) {
              log.warn(
                  "Error while running task " + task + " (rethrowing): " + error.getMessage(),
                  error);
              throw Throwables.propagate(error);
            }
            return result;
          }
        };
    ((BasicTask) task).initExecutionManager(this);

    // If there's a scheduler then use that; otherwise execute it directly
    Set<TaskScheduler> schedulers = null;
    for (Object tago : ((BasicTask) task).tags) {
      TaskScheduler scheduler = getTaskSchedulerForTag(tago);
      if (scheduler != null) {
        if (schedulers == null) schedulers = new LinkedHashSet(2);
        schedulers.add(scheduler);
      }
    }
    Future future;
    if (schedulers != null && !schedulers.isEmpty()) {
      if (schedulers.size() > 1)
        log.warn(
            "multiple schedulers detected, using only the first, for " + task + ": " + schedulers);
      future = schedulers.iterator().next().submit(job);
    } else {
      future = runner.submit(job);
    }

    ((BasicTask) task).initResult(future);
    return task;
  }
Пример #8
0
 public int getFramePosition() {
   synchronized (this) {
     return task.getFramePosition();
   }
 }
Пример #9
0
 /** Abort everything and return */
 public synchronized void abort() {
   tasks.clear();
   task.abort();
   notifyAll();
 }
Пример #10
0
 /**
  * convenience for setting "blocking details" on any task where the current thread is running;
  * typically invoked prior to a wait, for transparency to a user; then invoked with 'null' just
  * after the wait
  */
 public static void setBlockingDetails(String description) {
   Task current = current();
   if (current instanceof BasicTask) ((BasicTask) current).setBlockingDetails(description);
 }
Пример #11
0
 /**
  * sets extra status details on the current task, if possible (otherwise does nothing). the extra
  * status is presented in Task.getStatusDetails(true)
  */
 public static void setExtraStatusDetails(String notes) {
   Task current = current();
   if (current instanceof BasicTask) ((BasicTask) current).setExtraStatusText(notes);
 }