gt:             if (cluster == null) {
context: package com.cloudera.cmf.scheduler;
  
  import com.cloudera.cmf.command.CmdArgs;
  import com.cloudera.cmf.command.CmdNoopPropagateException;
  import com.cloudera.cmf.command.SvcCmdArgs;
  import com.cloudera.cmf.model.DbCluster;
  import com.cloudera.cmf.model.DbCommand;
  import com.cloudera.cmf.model.DbCommandSchedule;
  import com.cloudera.cmf.model.DbHost;
  import com.cloudera.cmf.model.DbRole;
  import com.cloudera.cmf.model.DbService;
  import com.cloudera.cmf.persist.CmfEntityManager;
  import com.cloudera.enterprise.JsonUtil2;
  import com.cloudera.enterprise.ThrottlingLogger;
  import com.cloudera.server.cmf.FeatureUnavailableException;
  import com.cloudera.server.cmf.OperationsManager;
  import com.fasterxml.jackson.annotation.JsonAutoDetect;
  import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
  import com.fasterxml.jackson.annotation.JsonCreator;
  import com.fasterxml.jackson.annotation.JsonProperty;
  import com.google.common.annotations.VisibleForTesting;
  import com.google.common.base.Objects;
  import com.google.common.base.Objects.ToStringHelper;
  import com.google.common.base.Preconditions;
  import java.util.Date;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.OptimisticLockException;
  import javax.persistence.PersistenceException;
  import org.hibernate.StaleStateException;
  import org.joda.time.Duration;
  import org.joda.time.Instant;
  import org.quartz.DisallowConcurrentExecution;
  import org.quartz.Job;
  import org.quartz.JobDataMap;
  import org.quartz.JobExecutionContext;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  @DisallowConcurrentExecution
  public class CommandDispatcherJob
    implements Job
  {
    private static final Logger LOG = LoggerFactory.getLogger(CommandDispatcherJob.class);
  
    private static Logger THROTTLED_LOG = new ThrottlingLogger(LOG, Duration.standardMinutes(30L));
    private static final String COMMAND_DISPATCHER_ARGS = "COMMAND_DISPATCHER_ARGS";
  
    @VisibleForTesting
    static final int COMMAND_RETRIES = Integer.getInteger("com.cloudera.cmf.scheduler.command.retries", 25).intValue();
    private final OperationsManager om;
    private final EntityManagerFactory emf;
  
    public CommandDispatcherJob(OperationsManager om, EntityManagerFactory emf)
    {
      Preconditions.checkNotNull(om);
      Preconditions.checkNotNull(emf);
  
      this.om = om;
      this.emf = emf;
    }
  
    public void execute(JobExecutionContext context) {
      JobArgs args;
      try {
        args = getJobArgs(context.getMergedJobDataMap());
      } catch (RuntimeException e) {
        LOG.error("Failed to de-serialize command dispatcher job arguments", e);
        return;
      }
  
      args.commandArgs.setScheduleId(args.scheduleId);
      args.commandArgs.setScheduledTime(new Instant(context.getScheduledFireTime()));
  
      if (args.scheduleId != null)
      {
        CmfEntityManager em = new CmfEntityManager(this.emf);
        DbCommandSchedule schedule = null;
        try {
          em.begin();
  
          schedule = em.findCommandSchedule(args.scheduleId.longValue());
  
          if (!Objects.equal(schedule.getVersion(), args.scheduleVersion))
          {
            LOG.debug("Skipping stale job - job schedule version '{}' vs db schedule version '{}'", args.scheduleVersion, schedule.getVersion());
            return;
          }
  
          Date currentFireTime = context.getScheduledFireTime();
          Preconditions.checkNotNull(currentFireTime);
          schedule.setFireAfterTime(new Instant(currentFireTime));
  
          em.commit();
        } catch (RuntimeException e) {
          if ((e instanceof StaleStateException))
          {
            LOG.debug("Skipping schedule '{}' execution since the state update failed.", args.scheduleId);
          }
          else
          {
            LOG.error(String.format("Skipping schedule execution since the state update failed due to an unexpected error. Schedule details: %s", new Object[] { schedule == null ? args.toString() : schedule.toString() }), e);
          }
  
          em.rollback();
          return;
        }
        finally
        {
          em.close();
        }
  
      }
  
      for (int tries = 0; tries <= COMMAND_RETRIES; tries++) {
        try {
          executeCommand(args);
        } catch (PersistenceException e) {
          if (((e instanceof OptimisticLockException)) || ((e.getCause() instanceof OptimisticLockException)))
          {
            LOG.warn(String.format("Could not commit command (%s) for schedule (%d) on attempt %d", new Object[] { JobArgs.access$300(args), JobArgs.access$000(args), Integer.valueOf(tries + 1) }), e);
          }
          else
          {
            LOG.error(String.format("Failed to invoke command (%s) for schedule (%d)", new Object[] { JobArgs.access$300(args), JobArgs.access$000(args) }), e);
          }
  
        }
        catch (FeatureUnavailableException e)
        {
          THROTTLED_LOG.warn(String.format("Missing feature to run scheduled command (%s)", new Object[] { JobArgs.access$300(args) }));
        }
        catch (CmdNoopPropagateException e)
        {
          LOG.info(String.format("Skipping scheduled command '%s' since it is a noop.", new Object[] { JobArgs.access$300(args) }));
        }
        catch (RuntimeException e)
        {
          LOG.error(String.format("Failed to invoke command (%s) for schedule (%d)", new Object[] { JobArgs.access$300(args), JobArgs.access$000(args) }), e);
        }
  
      }
  
      if (tries > COMMAND_RETRIES)
        LOG.error(String.format("Failed to invoke command (%s) for schedule (%d) even after %d tries", new Object[] { JobArgs.access$300(args), JobArgs.access$000(args), Integer.valueOf(tries) }));
    }
  
    private void executeCommand(JobArgs args)
    {
      CmfEntityManager em = new CmfEntityManager(this.emf);
      try {
        em.begin();
        dispatchCommand(em, args);
        em.commit();
      }
      catch (RuntimeException e)
      {
        throw e;
      } finally {
        em.close();
      }
    }
  
    @VisibleForTesting
    DbCommand dispatchCommand(CmfEntityManager em, JobArgs args) {
      String cmdName = args.commandName;
      CmdArgs cmdArgs = args.commandArgs;
      DbCommand command;
      DbCommand command;
      if (args.hostId != null)
      {
        LOG.debug("Invoking command '{}' on host '{}'", cmdName, args.hostId);
        DbHost host = em.findHost(args.hostId.longValue());
        if (host == null) {
          throw new IllegalStateException(String.format("Host (%d) not found", new Object[] { JobArgs.access$400(args) }));
        }
  
        command = this.om.executeHostCommand(em, host, cmdName, cmdArgs);
      }
      else
      {
        DbCommand command;
        if (args.roleId != null)
        {
          LOG.debug("Invoking command '{}' on role '{}'", cmdName, args.roleId);
          DbRole role = em.findRole(args.roleId.longValue());
          if (role == null) {
            throw new IllegalStateException(String.format("Role (%d) not found", new Object[] { JobArgs.access$500(args) }));
          }
  
          command = this.om.executeRoleCommand(em, role, cmdName, cmdArgs);
        }
        else
        {
          DbCommand command;
          if (args.serviceId != null)
          {
            LOG.debug("Invoking command '{}' on service '{}'", cmdName, args.serviceId);
  
            DbService service = em.findService(args.serviceId.longValue());
            if (service == null) {
              throw new IllegalStateException(String.format("Service (%d) not found", new Object[] { JobArgs.access$600(args) }));
            }
  
            command = this.om.executeServiceCmd(em, service, cmdName, (SvcCmdArgs)cmdArgs);
          }
          else
          {
            DbCommand command;
            if (args.clusterId != null)
            {
              LOG.debug("Invoking command '{}' on cluster '{}'", cmdName, args.clusterId);
  
              DbCluster cluster = em.findCluster(args.clusterId.longValue());
  