@Override
  public void run() {
    JobConfig jobConfig = null;
    Serializable taskArgs = null;
    try {
      jobConfig = (JobConfig) SerializationUtils.deserialize(
          mRunTaskCommand.getJobConfig().toByteArray());
      if (mRunTaskCommand.hasTaskArgs()) {
        taskArgs = SerializationUtils.deserialize(mRunTaskCommand.getTaskArgs().toByteArray());
      }
    } catch (IOException | ClassNotFoundException e) {
      fail(e, jobConfig, null);
    }

    PlanDefinition<JobConfig, Serializable, Serializable> definition;
    try {
      definition = PlanDefinitionRegistry.INSTANCE.getJobDefinition(jobConfig);
    } catch (JobDoesNotExistException e) {
      LOG.error("The job definition for config {} does not exist.", jobConfig.getName());
      fail(e, jobConfig, taskArgs);
      return;
    }

    mTaskExecutorManager.notifyTaskRunning(mJobId, mTaskId);
    Serializable result;
    try {
      result = definition.runTask(jobConfig, taskArgs, mContext);
    } catch (InterruptedException | CancelledException e) {
      // Cleanup around the interruption should already have been handled by a different thread
      Thread.currentThread().interrupt();
      return;
    } catch (Throwable t) {
      fail(t, jobConfig, taskArgs);
      return;
    }
    mTaskExecutorManager.notifyTaskCompletion(mJobId, mTaskId, result);
  }
@Test
  public void runFailure() throws Exception {
    long jobId = 1;
    long taskId = 2;
    JobConfig jobConfig = new SleepJobConfig(10);
    Serializable taskArgs = Lists.newArrayList(1);
    RunTaskContext context = mock(RunTaskContext.class);
    @SuppressWarnings("unchecked")
    PlanDefinition<JobConfig, Serializable, Serializable> planDefinition =
        mock(PlanDefinition.class);
    when(mRegistry.getJobDefinition(eq(jobConfig))).thenReturn(planDefinition);
    when(planDefinition.runTask(eq(jobConfig), any(Serializable.class), any(RunTaskContext.class)))
        .thenThrow(new UnsupportedOperationException("failure"));

    RunTaskCommand command = RunTaskCommand.newBuilder()
        .setJobConfig(ByteString.copyFrom(SerializationUtils.serialize(jobConfig)))
        .setTaskArgs(ByteString.copyFrom(SerializationUtils.serialize(taskArgs))).build();

    TaskExecutor executor =
        new TaskExecutor(jobId, taskId, command, context, mTaskExecutorManager);
    executor.run();

    verify(mTaskExecutorManager).notifyTaskFailure(eq(jobId), eq(taskId), any());
  }