Search in sources :

Example 1 with RetryJobException

use of org.candlepin.pinsetter.core.RetryJobException in project candlepin by candlepin.

the class RefreshPoolsJob method toExecute.

/**
 * {@inheritDoc}
 *
 * Executes {@link PoolManager#refreshPools(org.candlepin.model.Owner)}
 * as a pinsetter job.
 *
 * @param context the job's execution context
 */
public void toExecute(JobExecutionContext context) throws JobExecutionException {
    try {
        JobDataMap map = context.getMergedJobDataMap();
        String ownerKey = map.getString(JobStatus.TARGET_ID);
        Boolean lazy = map.getBoolean(LAZY_REGEN);
        Owner owner = ownerCurator.lookupByKey(ownerKey);
        if (owner == null) {
            context.setResult("Nothing to do. Owner no longer exists");
            return;
        }
        // Assume that we verified the request in the resource layer:
        poolManager.getRefresher(this.subAdapter, this.ownerAdapter, lazy).setUnitOfWork(unitOfWork).add(owner).run();
        context.setResult("Pools refreshed for owner " + owner.getDisplayName());
    } catch (PersistenceException e) {
        throw new RetryJobException("RefreshPoolsJob encountered a problem.", e);
    } catch (RuntimeException e) {
        Throwable cause = e.getCause();
        while (cause != null) {
            if (SQLException.class.isAssignableFrom(cause.getClass())) {
                log.warn("Caught a runtime exception wrapping an SQLException.");
                throw new RetryJobException("RefreshPoolsJob encountered a problem.", e);
            }
            cause = cause.getCause();
        }
        // Otherwise throw as we would normally for any generic Exception:
        log.error("RefreshPoolsJob encountered a problem.", e);
        context.setResult(e.toString());
        throw new JobExecutionException(e.toString(), e, false);
    }// cleaned up on failure.
     catch (Exception e) {
        log.error("RefreshPoolsJob encountered a problem.", e);
        context.setResult(e.toString());
        throw new JobExecutionException(e.toString(), e, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Owner(org.candlepin.model.Owner) JobExecutionException(org.quartz.JobExecutionException) SQLException(java.sql.SQLException) PersistenceException(javax.persistence.PersistenceException) RetryJobException(org.candlepin.pinsetter.core.RetryJobException) JobExecutionException(org.quartz.JobExecutionException) SQLException(java.sql.SQLException) PersistenceException(javax.persistence.PersistenceException) RetryJobException(org.candlepin.pinsetter.core.RetryJobException)

Example 2 with RetryJobException

use of org.candlepin.pinsetter.core.RetryJobException in project candlepin by candlepin.

the class UndoImportsJob method toExecute.

/**
 * {@inheritDoc}
 *
 * Executes {@link PoolManager#refreshPools(org.candlepin.model.Owner)}
 * as a pinsetter job.
 *
 * @param context the job's execution context
 */
@Transactional
public void toExecute(JobExecutionContext context) throws JobExecutionException {
    try {
        JobDataMap map = context.getMergedJobDataMap();
        String ownerId = map.getString(JobStatus.TARGET_ID);
        String ownerKey = map.getString(OWNER_KEY);
        Owner owner = this.ownerCurator.lockAndLoadById(ownerId);
        Boolean lazy = map.getBoolean(LAZY_REGEN);
        Principal principal = (Principal) map.get(PinsetterJobListener.PRINCIPAL_KEY);
        if (owner == null) {
            log.debug("Owner no longer exists: {}", ownerKey);
            context.setResult("Nothing to do; owner no longer exists: " + ownerKey);
            return;
        }
        String displayName = owner.getDisplayName();
        // Remove imports
        ExporterMetadata metadata = this.exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner);
        if (metadata == null) {
            log.debug("No imports exist for owner {}", displayName);
            context.setResult("Nothing to do; imports no longer exist for owner: " + displayName);
            return;
        }
        log.info("Deleting all pools originating from manifests for owner/org: {}", displayName);
        List<Pool> pools = this.poolManager.listPoolsByOwner(owner).list();
        for (Pool pool : pools) {
            if (this.poolManager.isManaged(pool)) {
                this.poolManager.deletePool(pool);
            }
        }
        // Clear out upstream ID so owner can import from other distributors:
        UpstreamConsumer uc = owner.getUpstreamConsumer();
        owner.setUpstreamConsumer(null);
        this.exportCurator.delete(metadata);
        this.recordManifestDeletion(owner, principal.getUsername(), uc);
        context.setResult("Imported pools removed for owner " + displayName);
    } catch (PersistenceException e) {
        throw new RetryJobException("UndoImportsJob encountered a problem.", e);
    } catch (RuntimeException e) {
        Throwable cause = e.getCause();
        while (cause != null) {
            if (SQLException.class.isAssignableFrom(cause.getClass())) {
                log.warn("Caught a runtime exception wrapping an SQLException.");
                throw new RetryJobException("UndoImportsJob encountered a problem.", e);
            }
            cause = cause.getCause();
        }
        // Otherwise throw as we would normally for any generic Exception:
        log.error("UndoImportsJob encountered a problem.", e);
        context.setResult(e.getMessage());
        throw new JobExecutionException(e.getMessage(), e, false);
    }// cleaned up on failure.
     catch (Exception e) {
        log.error("UndoImportsJob encountered a problem.", e);
        context.setResult(e.getMessage());
        throw new JobExecutionException(e.getMessage(), e, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Owner(org.candlepin.model.Owner) SQLException(java.sql.SQLException) ExporterMetadata(org.candlepin.model.ExporterMetadata) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ImportUpstreamConsumer(org.candlepin.model.ImportUpstreamConsumer) RetryJobException(org.candlepin.pinsetter.core.RetryJobException) SQLException(java.sql.SQLException) JobExecutionException(org.quartz.JobExecutionException) PersistenceException(javax.persistence.PersistenceException) RetryJobException(org.candlepin.pinsetter.core.RetryJobException) JobExecutionException(org.quartz.JobExecutionException) PersistenceException(javax.persistence.PersistenceException) Pool(org.candlepin.model.Pool) Principal(org.candlepin.auth.Principal) Transactional(com.google.inject.persist.Transactional)

Aggregations

SQLException (java.sql.SQLException)2 PersistenceException (javax.persistence.PersistenceException)2 Owner (org.candlepin.model.Owner)2 RetryJobException (org.candlepin.pinsetter.core.RetryJobException)2 JobDataMap (org.quartz.JobDataMap)2 JobExecutionException (org.quartz.JobExecutionException)2 Transactional (com.google.inject.persist.Transactional)1 Principal (org.candlepin.auth.Principal)1 ExporterMetadata (org.candlepin.model.ExporterMetadata)1 ImportUpstreamConsumer (org.candlepin.model.ImportUpstreamConsumer)1 Pool (org.candlepin.model.Pool)1 UpstreamConsumer (org.candlepin.model.UpstreamConsumer)1