Package com.twitter.common.base

Examples of com.twitter.common.base.Command


   * @param schedulerActive Event.
   */
  @Subscribe
  public void schedulerActive(PubsubEvent.SchedulerActive schedulerActive) {
    startAsync();
    shutdownRegistry.addAction(new Command() {
      @Override
      public void execute() {
        CronLifecycle.this.stopAsync().awaitTerminated();
      }
    });
View Full Code Here


    // an unrelated task state change in the scheduler fires an event that requires this intrinsic
    // lock. This causes a deadlock when the executor tries to acquire a lock held by the event
    // fired.

    pruner = prunerWithRealExecutor();
    Command onDeleted = new Command() {
      @Override
      public void execute() {
        // The goal is to verify that the call does not deadlock. We do not care about the outcome.
        IScheduledTask b = makeTask("b", ASSIGNED);
View Full Code Here

          return (state == stateMachine.getState()) ? 1 : 0;
        }
      });
    }

    shutdownRegistry.addAction(new Command() {
      @Override
      public void execute() {
        stateMachine.transition(State.DEAD);
      }
    });
View Full Code Here

  private static ScheduledExecutorService createThreadPool(ShutdownRegistry shutdownRegistry) {
    final ScheduledThreadPoolExecutor executor =
        AsyncUtil.singleThreadLoggingScheduledExecutor("TaskScheduler-%d", LOG);

    Stats.exportSize("schedule_queue_size", executor.getQueue());
    shutdownRegistry.addAction(new Command() {
      @Override
      public void execute() {
        new ExecutorServiceShutdown(executor, Amount.of(1L, Time.SECONDS)).execute();
      }
    });
View Full Code Here

            }
          }
        };

    // To be called on a task transitioning into the FINISHED state.
    final Command rescheduleIfService = new Command() {
      @Override
      public void execute() {
        if (task.get().getAssignedTask().getTask().isIsService()) {
          addFollowup(RESCHEDULE);
        }
      }
    };

    // To be called on a task transitioning into the FAILED state.
    final Command incrementFailuresMaybeReschedule = new Command() {
      @Override
      public void execute() {
        addFollowup(INCREMENT_FAILURES);

        // Max failures is ignored for service task.
        boolean isService = task.get().getAssignedTask().getTask().isIsService();

        // Max failures is ignored when set to -1.
        int maxFailures = task.get().getAssignedTask().getTask().getMaxTaskFailures();
        boolean belowMaxFailures =
            maxFailures == -1 || task.get().getFailureCount() < (maxFailures - 1);
        if (isService || belowMaxFailures) {
          addFollowup(RESCHEDULE);
        } else {
          LOG.info("Task " + name + " reached failure limit, not rescheduling");
        }
      }
    };

    final Closure<Transition<TaskState>> deleteIfKilling =
        Closures.filter(Transition.to(KILLING), addFollowupClosure(DELETE));

    stateMachine = StateMachine.<TaskState>builder(name)
        .logTransitions()
        .initialState(initialState)
        .addState(
            Rule.from(INIT)
                .to(PENDING, THROTTLED))
        .addState(
            Rule.from(PENDING)
                .to(ASSIGNED, KILLING)
                .withCallback(deleteIfKilling))
        .addState(
            Rule.from(THROTTLED)
            .to(PENDING, KILLING)
            .withCallback(deleteIfKilling))
        .addState(
            Rule.from(ASSIGNED)
                .to(STARTING, RUNNING, FINISHED, FAILED, RESTARTING, DRAINING,
                    KILLED, KILLING, LOST, PREEMPTING)
                .withCallback(
                    new Closure<Transition<TaskState>>() {
                      @Override
                      public void execute(Transition<TaskState> transition) {
                        switch (transition.getTo()) {
                          case FINISHED:
                            rescheduleIfService.execute();
                            break;

                          case PREEMPTING:
                            addFollowup(KILL);
                            break;

                          case FAILED:
                            incrementFailuresMaybeReschedule.execute();
                            break;

                          case RESTARTING:
                            addFollowup(KILL);
                            break;

                          case DRAINING:
                            addFollowup(KILL);
                            break;

                          case KILLED:
                            addFollowup(RESCHEDULE);
                            break;

                          case LOST:
                            addFollowup(RESCHEDULE);
                            addFollowup(KILL);
                            break;

                          case KILLING:
                            addFollowup(KILL);
                            break;

                          default:
                            // No-op.
                        }
                      }
                    }
                ))
        .addState(
            Rule.from(STARTING)
                .to(RUNNING, FINISHED, FAILED, RESTARTING, DRAINING, KILLING,
                    KILLED, LOST, PREEMPTING)
                .withCallback(
                    new Closure<Transition<TaskState>>() {
                      @Override
                      public void execute(Transition<TaskState> transition) {
                        switch (transition.getTo()) {
                          case FINISHED:
                            rescheduleIfService.execute();
                            break;

                          case RESTARTING:
                            addFollowup(KILL);
                            break;

                          case DRAINING:
                            addFollowup(KILL);
                            break;

                          case PREEMPTING:
                            addFollowup(KILL);
                            break;

                          case FAILED:
                            incrementFailuresMaybeReschedule.execute();
                            break;

                          case KILLED:
                            addFollowup(RESCHEDULE);
                            break;

                          case KILLING:
                            addFollowup(KILL);
                            break;

                          case LOST:
                            addFollowup(RESCHEDULE);
                            break;

                          case SANDBOX_DELETED:
                            // The slave previously acknowledged that it had the task, and now
                            // stopped reporting it.
                            addFollowupTransition(LOST);
                            break;

                          default:
                            // No-op.
                        }
                      }
                    }
                ))
        .addState(
            Rule.from(RUNNING)
                .to(FINISHED, RESTARTING, DRAINING, FAILED, KILLING, KILLED, LOST, PREEMPTING)
                .withCallback(
                    new Closure<Transition<TaskState>>() {
                      @Override
                      public void execute(Transition<TaskState> transition) {
                        switch (transition.getTo()) {
                          case FINISHED:
                            rescheduleIfService.execute();
                            break;

                          case PREEMPTING:
                            addFollowup(KILL);
                            break;

                          case RESTARTING:
                            addFollowup(KILL);
                            break;

                          case DRAINING:
                            addFollowup(KILL);
                            break;

                          case FAILED:
                            incrementFailuresMaybeReschedule.execute();
                            break;

                          case KILLED:
                            addFollowup(RESCHEDULE);
                            break;
View Full Code Here

      this.shutdownRegistry = shutdownRegistry;
    }

    @Override
    public void execute() {
      shutdownRegistry.addAction(new Command() {
        @Override
        public void execute() {
          Thread thread = Thread.currentThread();
          String message = new StringBuilder()
              .append("Thread: ").append(thread.getName())
View Full Code Here

            }
          }
        };

    // To be called on a task transitioning into the FINISHED state.
    final Command rescheduleIfService = new Command() {
      @Override
      public void execute() {
        if (task.get().getAssignedTask().getTask().isIsService()) {
          addFollowup(RESCHEDULE);
        }
      }
    };

    // To be called on a task transitioning into the FAILED state.
    final Command incrementFailuresMaybeReschedule = new Command() {
      @Override
      public void execute() {
        addFollowup(INCREMENT_FAILURES);

        // Max failures is ignored for service task.
        boolean isService = task.get().getAssignedTask().getTask().isIsService();

        // Max failures is ignored when set to -1.
        int maxFailures = task.get().getAssignedTask().getTask().getMaxTaskFailures();
        boolean belowMaxFailures =
            maxFailures == -1 || task.get().getFailureCount() < (maxFailures - 1);
        if (isService || belowMaxFailures) {
          addFollowup(RESCHEDULE);
        } else {
          LOG.info("Task " + name + " reached failure limit, not rescheduling");
        }
      }
    };

    final Closure<Transition<TaskState>> deleteIfKilling =
        Closures.filter(Transition.to(KILLING), addFollowupClosure(DELETE));

    stateMachine = StateMachine.<TaskState>builder(name)
        .logTransitions()
        .initialState(initialState)
        .addState(
            Rule.from(INIT)
                .to(PENDING, THROTTLED))
        .addState(
            Rule.from(PENDING)
                .to(ASSIGNED, KILLING)
                .withCallback(deleteIfKilling))
        .addState(
            Rule.from(THROTTLED)
            .to(PENDING, KILLING)
            .withCallback(deleteIfKilling))
        .addState(
            Rule.from(ASSIGNED)
                .to(STARTING, RUNNING, FINISHED, FAILED, RESTARTING, DRAINING,
                    KILLED, KILLING, LOST, PREEMPTING)
                .withCallback(
                    new Closure<Transition<TaskState>>() {
                      @Override
                      public void execute(Transition<TaskState> transition) {
                        switch (transition.getTo()) {
                          case FINISHED:
                            rescheduleIfService.execute();
                            break;

                          case PREEMPTING:
                            addFollowup(KILL);
                            break;

                          case FAILED:
                            incrementFailuresMaybeReschedule.execute();
                            break;

                          case RESTARTING:
                            addFollowup(KILL);
                            break;

                          case DRAINING:
                            addFollowup(KILL);
                            break;

                          case KILLED:
                            addFollowup(RESCHEDULE);
                            break;

                          case LOST:
                            addFollowup(RESCHEDULE);
                            addFollowup(KILL);
                            break;

                          case KILLING:
                            addFollowup(KILL);
                            break;

                          default:
                            // No-op.
                        }
                      }
                    }
                ))
        .addState(
            Rule.from(STARTING)
                .to(RUNNING, FINISHED, FAILED, RESTARTING, DRAINING, KILLING,
                    KILLED, LOST, PREEMPTING)
                .withCallback(
                    new Closure<Transition<TaskState>>() {
                      @Override
                      public void execute(Transition<TaskState> transition) {
                        switch (transition.getTo()) {
                          case FINISHED:
                            rescheduleIfService.execute();
                            break;

                          case RESTARTING:
                            addFollowup(KILL);
                            break;

                          case DRAINING:
                            addFollowup(KILL);
                            break;

                          case PREEMPTING:
                            addFollowup(KILL);
                            break;

                          case FAILED:
                            incrementFailuresMaybeReschedule.execute();
                            break;

                          case KILLED:
                            addFollowup(RESCHEDULE);
                            break;

                          case KILLING:
                            addFollowup(KILL);
                            break;

                          case LOST:
                            addFollowup(RESCHEDULE);
                            break;

                          default:
                            // No-op.
                        }
                      }
                    }
                ))
        .addState(
            Rule.from(RUNNING)
                .to(FINISHED, RESTARTING, DRAINING, FAILED, KILLING, KILLED, LOST, PREEMPTING)
                .withCallback(
                    new Closure<Transition<TaskState>>() {
                      @Override
                      public void execute(Transition<TaskState> transition) {
                        switch (transition.getTo()) {
                          case FINISHED:
                            rescheduleIfService.execute();
                            break;

                          case PREEMPTING:
                            addFollowup(KILL);
                            break;

                          case RESTARTING:
                            addFollowup(KILL);
                            break;

                          case DRAINING:
                            addFollowup(KILL);
                            break;

                          case FAILED:
                            incrementFailuresMaybeReschedule.execute();
                            break;

                          case KILLED:
                            addFollowup(RESCHEDULE);
                            break;
View Full Code Here

  private static ScheduledExecutorService createThreadPool(ShutdownRegistry shutdownRegistry) {
    final ScheduledThreadPoolExecutor executor =
        AsyncUtil.singleThreadLoggingScheduledExecutor("TaskScheduler-%d", LOG);

    Stats.exportSize("schedule_queue_size", executor.getQueue());
    shutdownRegistry.addAction(new Command() {
      @Override
      public void execute() {
        new ExecutorServiceShutdown(executor, Amount.of(1L, Time.SECONDS)).execute();
      }
    });
View Full Code Here

      this.shutdownRegistry = shutdownRegistry;
    }

    @Override
    public void execute() {
      shutdownRegistry.addAction(new Command() {
        @Override
        public void execute() {
          Thread thread = Thread.currentThread();
          String message = new StringBuilder()
              .append("Thread: ").append(thread.getName())
View Full Code Here

        server.start();
      } catch (Exception e) {
        throw new LaunchException("Failed to start jetty server: " + e, e);
      }

      Command shutdown = new Command() {
        @Override public void execute() {
          LOG.info("Shutting down embedded http server");
          try {
            server.stop();
          } catch (Exception e) {
View Full Code Here

TOP

Related Classes of com.twitter.common.base.Command

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.