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());
}
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();
}
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;
}
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;
}
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);
}
Aggregations