Search in sources :

Example 11 with DistributionQueueItem

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

the class DistributionQueueMBeanTest method verifyMBeanExposedValuesWhenHeadIsNotNull.

@Test
public void verifyMBeanExposedValuesWhenHeadIsNotNull() {
    DistributionQueueStatus status = new DistributionQueueStatus(1, RUNNING);
    Calendar joined = Calendar.getInstance();
    DistributionQueueEntry entry = new DistributionQueueEntry("#entry", new DistributionQueueItem("#package", 1000L, new HashMap<String, Object>()), new DistributionQueueItemStatus(joined, QUEUED, 1, "#queue"));
    DistributionQueue distributionQueue = mock(DistributionQueue.class);
    when(distributionQueue.getName()).thenReturn("#distributionQueue");
    when(distributionQueue.getHead()).thenReturn(entry);
    when(distributionQueue.getType()).thenReturn(PARALLEL);
    when(distributionQueue.getStatus()).thenReturn(status);
    DistributionQueueMBean mBean = new DistributionQueueMBeanImpl(distributionQueue);
    assertEquals(distributionQueue.getName(), mBean.getName());
    assertEquals(distributionQueue.getType().name().toLowerCase(), mBean.getType());
    assertEquals(1, mBean.getSize());
    assertFalse(mBean.isEmpty());
    assertEquals("#entry", mBean.getHeadId());
    assertEquals(1, mBean.getHeadDequeuingAttempts());
    assertEquals(QUEUED.name().toLowerCase(), mBean.getHeadStatus());
    assertEquals(joined, mBean.getHeadEnqueuingDate());
}
Also used : DistributionQueueStatus(org.apache.sling.distribution.queue.DistributionQueueStatus) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) HashMap(java.util.HashMap) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) Calendar(java.util.Calendar) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 12 with DistributionQueueItem

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

the class SingleQueueDistributionStrategyTest method testPackageAdditionWithFailingItemDelivery.

@Test
public void testPackageAdditionWithFailingItemDelivery() throws Exception {
    SingleQueueDispatchingStrategy singleQueueDistributionStrategy = new SingleQueueDispatchingStrategy();
    DistributionPackage distributionPackage = mock(DistributionPackage.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueue queue = mock(DistributionQueue.class);
    DistributionQueueItem queueItem = mock(DistributionQueueItem.class);
    when(queueProvider.getQueue(DistributionQueueDispatchingStrategy.DEFAULT_QUEUE_NAME)).thenReturn(queue);
    DistributionQueueItemStatus state = mock(DistributionQueueItemStatus.class);
    when(queue.add(queueItem)).thenReturn(new DistributionQueueEntry(null, queueItem, state));
    Iterable<DistributionQueueItemStatus> returnedStates = singleQueueDistributionStrategy.add(distributionPackage, queueProvider);
    assertNotNull(returnedStates);
    Iterator<DistributionQueueItemStatus> iterator = returnedStates.iterator();
    assertNotNull(iterator);
    assertTrue(iterator.hasNext());
    assertNotNull(iterator.next());
}
Also used : DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 13 with DistributionQueueItem

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

the class SimpleDistributionAgentQueueProcessor method processQueueItem.

private boolean processQueueItem(String queueName, DistributionQueueEntry queueEntry) throws DistributionException {
    boolean removeItemFromQueue = false;
    ResourceResolver agentResourceResolver = null;
    DistributionPackage distributionPackage = null;
    DistributionQueueItem queueItem = queueEntry.getItem();
    DistributionQueueItemStatus queueItemStatus = queueEntry.getStatus();
    try {
        String callingUser = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_USER, String.class);
        String requestId = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_ID, String.class);
        Long globalStartTime = queueItem.get(DistributionPackageUtils.PACKAGE_INFO_PROPERTY_REQUEST_START_TIME, Long.class);
        agentResourceResolver = DistributionUtils.getResourceResolver(callingUser, authenticationInfo.getAgentService(), authenticationInfo.getSlingRepository(), authenticationInfo.getSubServiceName(), authenticationInfo.getResourceResolverFactory());
        final long startTime = System.currentTimeMillis();
        distributionPackage = distributionPackageExporter.getPackage(agentResourceResolver, queueItem.getPackageId());
        if (distributionPackage != null) {
            final long packageSize = distributionPackage.getSize();
            DistributionPackageUtils.mergeQueueEntry(distributionPackage.getInfo(), queueEntry);
            final DistributionRequestType requestType = distributionPackage.getInfo().getRequestType();
            final String[] paths = distributionPackage.getInfo().getPaths();
            try {
                // import package
                distributionPackageImporter.importPackage(agentResourceResolver, distributionPackage);
                // generated event
                distributionEventFactory.generatePackageEvent(DistributionEventTopics.AGENT_PACKAGE_DISTRIBUTED, DistributionComponentKind.AGENT, agentName, distributionPackage.getInfo());
                removeItemFromQueue = true;
                final long endTime = System.currentTimeMillis();
                distributionLog.info("[{}] PACKAGE-DELIVERED {}: {} paths={}, importTime={}ms, execTime={}ms, size={}B", queueName, requestId, requestType, paths, endTime - startTime, endTime - globalStartTime, packageSize);
            } catch (RecoverableDistributionException e) {
                distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver {}, {}", queueName, requestId, distributionPackage.getId(), e.getMessage());
                distributionLog.debug("could not deliver package {}", distributionPackage.getId(), e);
                log.error("could not deliver package {}", distributionPackage.getId(), e);
            } catch (Throwable e) {
                distributionLog.error("[{}] PACKAGE-FAIL {}: could not deliver package {} {}", queueName, requestId, distributionPackage.getId(), e.getMessage(), e);
                log.error("could not deliver package {} from queue {}", new Object[] { distributionPackage.getId(), queueName }, e);
                if (errorQueueStrategy != null && queueItemStatus.getAttempts() > retryAttempts) {
                    removeItemFromQueue = reEnqueuePackage(distributionPackage);
                    distributionLog.info("[{}] PACKAGE-QUEUED {}: distribution package {} was enqueued to an error queue", queueName, requestId, distributionPackage.getId());
                }
            }
        } else {
            // return success if package does not exist in order to clear the queue.
            removeItemFromQueue = true;
            distributionLog.error("distribution package with id {} does not exist. the package will be skipped.", queueItem.getPackageId());
        }
    } finally {
        if (removeItemFromQueue) {
            DistributionPackageUtils.releaseOrDelete(distributionPackage, queueName);
        } else {
            DistributionPackageUtils.closeSafely(distributionPackage);
        }
        DistributionUtils.ungetResourceResolver(agentResourceResolver);
    }
    // return true if item should be removed from queue
    return removeItemFromQueue;
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DistributionRequestType(org.apache.sling.distribution.DistributionRequestType) RecoverableDistributionException(org.apache.sling.distribution.common.RecoverableDistributionException) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 14 with DistributionQueueItem

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

the class AsyncDeliveryDispatchingStrategyTest method testAddWithOneItemInRunningQueues.

@Test
public void testAddWithOneItemInRunningQueues() throws Exception {
    Map<String, String> deliveryMappings = new HashMap<String, String>();
    String queue1 = "queue1";
    deliveryMappings.put(queue1, "delivery1");
    String queue2 = "queue2";
    deliveryMappings.put(queue2, "delivery2");
    AsyncDeliveryDispatchingStrategy asyncDeliveryDispatchingStrategy = new AsyncDeliveryDispatchingStrategy(deliveryMappings);
    // setup package
    SharedDistributionPackage distributionPackage = mock(SharedDistributionPackage.class);
    when(distributionPackage.getId()).thenReturn("1221312");
    DistributionPackageInfo info = new DistributionPackageInfo("dummy", new HashMap<String, Object>());
    when(distributionPackage.getInfo()).thenReturn(info);
    // setup queues
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionQueue dq1 = mock(DistributionQueue.class);
    DistributionQueueItemStatus status = mock(DistributionQueueItemStatus.class);
    DistributionQueueEntry entry = new DistributionQueueEntry("1242112", new DistributionQueueItem(distributionPackage.getId(), new HashMap<String, Object>()), status);
    when(dq1.add(any(DistributionQueueItem.class))).thenReturn(entry);
    DistributionQueueStatus status1 = new DistributionQueueStatus(1, DistributionQueueState.RUNNING);
    when(dq1.getStatus()).thenReturn(status1);
    when(queueProvider.getQueue(queue1)).thenReturn(dq1);
    DistributionQueue dq2 = mock(DistributionQueue.class);
    when(dq2.add(any(DistributionQueueItem.class))).thenReturn(entry);
    DistributionQueueStatus status2 = new DistributionQueueStatus(1, DistributionQueueState.RUNNING);
    when(dq2.getStatus()).thenReturn(status2);
    when(queueProvider.getQueue(queue2)).thenReturn(dq2);
    Iterable<DistributionQueueItemStatus> statuses = asyncDeliveryDispatchingStrategy.add(distributionPackage, queueProvider);
    assertNotNull(statuses);
}
Also used : DistributionQueueStatus(org.apache.sling.distribution.queue.DistributionQueueStatus) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) HashMap(java.util.HashMap) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) SharedDistributionPackage(org.apache.sling.distribution.packaging.impl.SharedDistributionPackage) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) Test(org.junit.Test)

Example 15 with DistributionQueueItem

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

the class JobHandlingUtilsTest method testFullPropertiesFromPackageCreation.

@Test
public void testFullPropertiesFromPackageCreation() throws Exception {
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("vlt");
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_PATHS, new String[] { "/foo" });
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD);
    packageInfo.put(DistributionPackageInfo.PROPERTY_PACKAGE_TYPE, "vlt");
    DistributionQueueItem queueItem = new DistributionQueueItem("an-id", packageInfo);
    Map<String, Object> fullPropertiesFromPackage = JobHandlingUtils.createFullProperties(queueItem);
    assertNotNull(fullPropertiesFromPackage);
    assertEquals(5, fullPropertiesFromPackage.size());
    assertNotNull(fullPropertiesFromPackage.get("distribution.request.paths"));
    assertNotNull(fullPropertiesFromPackage.get("distribution.item.id"));
    assertNotNull(fullPropertiesFromPackage.get("distribution.package.size"));
    assertNotNull(fullPropertiesFromPackage.get("distribution.package.type"));
    assertNotNull(fullPropertiesFromPackage.get("distribution.request.type"));
}
Also used : DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

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