Search in sources :

Example 16 with DistributionQueueItem

use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.

the class ErrorQueueDispatchingStrategy method add.

@Override
public Iterable<DistributionQueueItemStatus> add(@Nonnull DistributionPackage distributionPackage, @Nonnull DistributionQueueProvider queueProvider) throws DistributionException {
    List<DistributionQueueItemStatus> result = new ArrayList<DistributionQueueItemStatus>();
    String originQueue = DistributionPackageUtils.getQueueName(distributionPackage.getInfo());
    if (!queueNames.contains(originQueue)) {
        return result;
    }
    String errorQueueName = ERROR_PREFIX + originQueue;
    DistributionQueue errorQueue = queueProvider.getQueue(errorQueueName);
    DistributionQueueItemStatus status = new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, errorQueueName);
    DistributionQueueItem queueItem = DistributionPackageUtils.toQueueItem(distributionPackage);
    DistributionPackageUtils.acquire(distributionPackage, errorQueueName);
    DistributionQueueEntry queueEntry = errorQueue.add(queueItem);
    if (queueEntry != null) {
        status = queueEntry.getStatus();
    } else {
        DistributionPackageUtils.releaseOrDelete(distributionPackage, errorQueueName);
        log.error("cannot add package {} to queue {}", distributionPackage.getId(), errorQueueName);
    }
    result.add(status);
    return result;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) ArrayList(java.util.ArrayList) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem)

Example 17 with DistributionQueueItem

use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.

the class MultipleQueueDispatchingStrategy method add.

public Iterable<DistributionQueueItemStatus> add(@Nonnull DistributionPackage distributionPackage, @Nonnull DistributionQueueProvider queueProvider) throws DistributionException {
    if (!(distributionPackage instanceof SharedDistributionPackage) && queueNames.length > 1) {
        throw new DistributionException("distribution package must be a shared package to be added in multiple queues");
    }
    DistributionQueueItem queueItem = getItem(distributionPackage);
    List<DistributionQueueItemStatus> result = new ArrayList<DistributionQueueItemStatus>();
    // first acquire the package for all queues
    DistributionPackageUtils.acquire(distributionPackage, queueNames);
    // second add the package to all queues
    for (String queueName : queueNames) {
        DistributionQueue queue = queueProvider.getQueue(queueName);
        DistributionQueueItemStatus status = new DistributionQueueItemStatus(DistributionQueueItemState.ERROR, queue.getName());
        DistributionQueueEntry queueEntry = queue.add(queueItem);
        if (queueEntry != null) {
            status = queueEntry.getStatus();
        } else {
            DistributionPackageUtils.release(distributionPackage, queueName);
            log.error("cannot add package {} to queue {}", distributionPackage.getId(), queueName);
        }
        result.add(status);
    }
    return result;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) ArrayList(java.util.ArrayList) SharedDistributionPackage(org.apache.sling.distribution.packaging.impl.SharedDistributionPackage) DistributionException(org.apache.sling.distribution.common.DistributionException) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem)

Example 18 with DistributionQueueItem

use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.

the class AgentDistributionPackageExporter method exportPackages.

public void exportPackages(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest distributionRequest, @Nonnull DistributionPackageProcessor packageProcessor) throws DistributionException {
    if (DistributionRequestType.TEST.equals(distributionRequest.getRequestType())) {
        packageProcessor.process(new SimpleDistributionPackage(distributionRequest, PACKAGE_TYPE));
        return;
    }
    if (!DistributionRequestType.PULL.equals(distributionRequest.getRequestType())) {
        throw new DistributionException("request type not supported " + distributionRequest.getRequestType());
    }
    DistributionPackage distributionPackage = null;
    try {
        log.debug("getting packages from queue {}", queueName);
        DistributionQueue queue = getQueueOrThrow(queueName);
        DistributionQueueEntry entry = queue.getHead();
        if (entry != null) {
            DistributionQueueItem queueItem = entry.getItem();
            DistributionPackageInfo info = DistributionPackageUtils.fromQueueItem(queueItem);
            DistributionPackageBuilder packageBuilder = packageBuilderProvider.getPackageBuilder(info.getType());
            if (packageBuilder != null) {
                distributionPackage = packageBuilder.getPackage(resourceResolver, queueItem.getPackageId());
                if (distributionPackage != null) {
                    distributionPackage.getInfo().putAll(info);
                    log.debug("item {} fetched from the queue", info);
                    packageProcessor.process(new AgentDistributionPackage(distributionPackage, queue, entry.getId()));
                } else {
                    log.warn("cannot get package {}", info);
                }
            } else {
                log.warn("cannot find package builder with type {}", info.getType());
            }
        }
    } catch (Exception ex) {
        log.error("Error exporting package", ex);
    } finally {
        DistributionPackageUtils.closeSafely(distributionPackage);
    }
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) SimpleDistributionPackage(org.apache.sling.distribution.packaging.impl.SimpleDistributionPackage) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionPackageBuilder(org.apache.sling.distribution.packaging.DistributionPackageBuilder) DistributionException(org.apache.sling.distribution.common.DistributionException) SimpleDistributionPackage(org.apache.sling.distribution.packaging.impl.SimpleDistributionPackage) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) DistributionException(org.apache.sling.distribution.common.DistributionException)

Example 19 with DistributionQueueItem

use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.

the class JobHandlingDistributionQueue method getStatus.

@Override
@Nonnull
public DistributionQueueStatus getStatus() {
    List<Job> jobs = getJobs(0, -1);
    Job firstJob = jobs.size() > 0 ? jobs.get(0) : null;
    DistributionQueueItem firstItem = firstJob != null ? JobHandlingUtils.getItem(firstJob) : null;
    DistributionQueueItemStatus firstItemStatus = firstJob != null ? JobHandlingUtils.getStatus(firstJob) : null;
    DistributionQueueState state = DistributionQueueUtils.calculateState(firstItem, firstItemStatus);
    if (!isActive) {
        state = DistributionQueueState.PASSIVE;
    }
    int itemsCount = jobs.size();
    return new DistributionQueueStatus(itemsCount, state);
}
Also used : DistributionQueueStatus(org.apache.sling.distribution.queue.DistributionQueueStatus) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) Job(org.apache.sling.event.jobs.Job) DistributionQueueState(org.apache.sling.distribution.queue.DistributionQueueState) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Nonnull(javax.annotation.Nonnull)

Example 20 with DistributionQueueItem

use of org.apache.sling.distribution.queue.DistributionQueueItem in project sling by apache.

the class ExtendedDistributionServiceResourceProvider method getItemProperties.

private Map<String, Object> getItemProperties(DistributionQueueEntry entry) {
    Map<String, Object> result = new HashMap<String, Object>();
    if (entry != null) {
        result.put(SLING_RESOURCE_TYPE, DistributionResourceTypes.AGENT_QUEUE_ITEM_RESOURCE_TYPE);
        DistributionQueueItem item = entry.getItem();
        DistributionPackageInfo packageInfo = DistributionPackageUtils.fromQueueItem(item);
        result.put("id", entry.getId());
        result.put("size", item.getSize());
        result.put("paths", packageInfo.getPaths());
        result.put("action", packageInfo.getRequestType());
        result.put("userid", packageInfo.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_USER, String.class));
        DistributionQueueItemStatus status = entry.getStatus();
        result.put("attempts", status.getAttempts());
        result.put("time", status.getEntered().getTime());
        result.put("state", status.getItemState().name());
    }
    return result;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HashMap(java.util.HashMap) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem)

Aggregations

DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)30 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)18 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)18 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)18 Test (org.junit.Test)15 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)7 HashMap (java.util.HashMap)5 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 DistributionException (org.apache.sling.distribution.common.DistributionException)4 Matchers.anyString (org.mockito.Matchers.anyString)4 SharedDistributionPackage (org.apache.sling.distribution.packaging.impl.SharedDistributionPackage)3 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)3 CheckForNull (javax.annotation.CheckForNull)2 Nonnull (javax.annotation.Nonnull)2 ScheduleOptions (org.apache.sling.commons.scheduler.ScheduleOptions)2 DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)2