Search in sources :

Example 6 with DistributionQueueItem

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

the class JobHandlingDistributionQueueTest method testPackageAdditionAndStatusCheck.

@SuppressWarnings("unchecked")
@Test
public void testPackageAdditionAndStatusCheck() throws Exception {
    JobManager jobManager = mock(JobManager.class);
    JobBuilder builder = mock(JobBuilder.class);
    when(builder.properties(any(Map.class))).thenReturn(builder);
    Job job = mock(Job.class);
    when(job.getId()).thenReturn("id-123");
    when(builder.add()).thenReturn(job);
    String topic = JobHandlingDistributionQueue.DISTRIBUTION_QUEUE_TOPIC + "/aname";
    when(jobManager.createJob(topic)).thenReturn(builder);
    when(jobManager.getJobById(anyString())).thenReturn(job);
    when(builder.properties(any(Map.class))).thenReturn(builder);
    DistributionQueue queue = new JobHandlingDistributionQueue("aname", topic, jobManager, true, DistributionQueueType.ORDERED);
    DistributionPackageInfo packageInfo = new DistributionPackageInfo("type");
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_PATHS, new String[] { "/foo" });
    packageInfo.put(DistributionPackageInfo.PROPERTY_REQUEST_TYPE, DistributionRequestType.ADD);
    DistributionQueueItem distributionQueueItem = new DistributionQueueItem("an-id", packageInfo);
    assertNotNull(queue.add(distributionQueueItem));
    DistributionQueueItemStatus status = queue.getItem(job.getId()).getStatus();
    assertNotNull(status);
    assertEquals(DistributionQueueItemState.QUEUED, status.getItemState());
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionPackageInfo(org.apache.sling.distribution.packaging.DistributionPackageInfo) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) JobBuilder(org.apache.sling.event.jobs.JobBuilder) JobManager(org.apache.sling.event.jobs.JobManager) Matchers.anyString(org.mockito.Matchers.anyString) Job(org.apache.sling.event.jobs.Job) Map(java.util.Map) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 7 with DistributionQueueItem

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

the class SimpleDistributionQueueProcessorTest method testRunWithOneNonEmptyQueue.

@Test
public void testRunWithOneNonEmptyQueue() throws Exception {
    SimpleDistributionQueueProvider queueProvider = mock(SimpleDistributionQueueProvider.class);
    Collection<SimpleDistributionQueue> queues = new LinkedList<SimpleDistributionQueue>();
    SimpleDistributionQueue queue = mock(SimpleDistributionQueue.class);
    DistributionQueueItem item = mock(DistributionQueueItem.class);
    when(queue.getHead()).thenReturn(new DistributionQueueEntry(null, item, null)).thenReturn(null);
    queues.add(queue);
    when(queueProvider.getQueues()).thenReturn(queues);
    DistributionQueueProcessor queueProcessor = mock(DistributionQueueProcessor.class);
    SimpleDistributionQueueProcessor simpleDistributionQueueProcessor = new SimpleDistributionQueueProcessor(queue, queueProcessor);
    simpleDistributionQueueProcessor.run();
}
Also used : DistributionQueueProcessor(org.apache.sling.distribution.queue.DistributionQueueProcessor) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) LinkedList(java.util.LinkedList) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 8 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 9 with DistributionQueueItem

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

the class AgentDistributionPackageExporter method getPackage.

public DistributionPackage getPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull String distributionPackageId) {
    try {
        log.debug("getting package from queue {}", queueName);
        DistributionQueue queue = getQueueOrThrow(queueName);
        DistributionQueueEntry entry = queue.getItem(distributionPackageId);
        DistributionPackage distributionPackage;
        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());
                distributionPackage.getInfo().putAll(info);
                log.debug("item {} fetched from the queue", info);
                if (distributionPackage != null) {
                    return new AgentDistributionPackage(distributionPackage, queue, entry.getId());
                }
            } else {
                log.warn("cannot find package builder with type {}", info.getType());
            }
        }
    } catch (Exception ex) {
        log.error("Error exporting package", ex);
    }
    return null;
}
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) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) DistributionException(org.apache.sling.distribution.common.DistributionException)

Example 10 with DistributionQueueItem

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

the class SimpleDistributionAgentQueueProcessorTest method testProcess.

@Test
public void testProcess() throws Exception {
    DistributionPackageExporter packageExporter = mock(DistributionPackageExporter.class);
    DistributionPackageImporter packageImporter = mock(DistributionPackageImporter.class);
    int retryAttempts = 3;
    DefaultDistributionLog log = mock(DefaultDistributionLog.class);
    DistributionQueueProvider queueProvider = mock(DistributionQueueProvider.class);
    DistributionEventFactory eventFactory = mock(DistributionEventFactory.class);
    SimpleDistributionAgentAuthenticationInfo authenticationInfo = mock(SimpleDistributionAgentAuthenticationInfo.class);
    String agentName = "dummy-a";
    SimpleDistributionAgentQueueProcessor queueProcessor = new SimpleDistributionAgentQueueProcessor(packageExporter, packageImporter, retryAttempts, null, log, queueProvider, eventFactory, authenticationInfo, agentName);
    String id = "123-456";
    DistributionQueueItem item = new DistributionQueueItem("pckg-123", new HashMap<String, Object>());
    String queueName = "queue-1";
    DistributionQueueItemStatus status = new DistributionQueueItemStatus(DistributionQueueItemState.QUEUED, queueName);
    DistributionQueueEntry entry = new DistributionQueueEntry(id, item, status);
    queueProcessor.process(queueName, entry);
}
Also used : DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) DistributionPackageExporter(org.apache.sling.distribution.packaging.DistributionPackageExporter) DistributionEventFactory(org.apache.sling.distribution.event.impl.DistributionEventFactory) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) DistributionQueueProvider(org.apache.sling.distribution.queue.DistributionQueueProvider) DistributionPackageImporter(org.apache.sling.distribution.packaging.DistributionPackageImporter) 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