use of org.apache.sling.distribution.agent.DistributionAgent in project sling by apache.
the class DistributionQueueHealthCheckTest method testWithNotOkItemInTheQueue.
@Test
public void testWithNotOkItemInTheQueue() throws Exception {
DistributionQueueHealthCheck distributionQueueHealthCheck = new DistributionQueueHealthCheck();
distributionQueueHealthCheck.activate(Collections.<String, Object>emptyMap());
DistributionQueue queue = mock(DistributionQueue.class);
DistributionQueueItem item = mock(DistributionQueueItem.class);
DistributionQueueItemStatus status = mock(DistributionQueueItemStatus.class);
when(status.getAttempts()).thenReturn(10);
when(queue.getItem(any(String.class))).thenReturn(new DistributionQueueEntry(null, item, status));
when(queue.getHead()).thenReturn(new DistributionQueueEntry(null, item, status));
DistributionAgent distributionAgent = mock(DistributionAgent.class);
List<String> queues = new ArrayList<String>();
queues.add("queueName");
when(distributionAgent.getQueueNames()).thenReturn(queues);
when(distributionAgent.getQueue(anyString())).thenReturn(queue);
distributionQueueHealthCheck.bindDistributionAgent(distributionAgent);
Result result = distributionQueueHealthCheck.execute();
assertNotNull(result);
assertFalse(result.isOk());
}
use of org.apache.sling.distribution.agent.DistributionAgent in project sling by apache.
the class DistributionQueueHealthCheck method execute.
public Result execute() {
final FormattingResultLog resultLog = new FormattingResultLog();
Map<String, Integer> failures = new HashMap<String, Integer>();
if (distributionAgents.size() > 0) {
for (DistributionAgent distributionAgent : distributionAgents) {
for (String queueName : distributionAgent.getQueueNames()) {
try {
DistributionQueue q = distributionAgent.getQueue(queueName);
DistributionQueueEntry entry = q.getHead();
if (entry != null) {
DistributionQueueItemStatus status = entry.getStatus();
if (status.getAttempts() <= numberOfRetriesAllowed) {
resultLog.debug("Queue: [{}], first item: [{}], number of retries: {}", q.getName(), entry.getId(), status.getAttempts());
} else {
// the no. of attempts is higher than the configured threshold
resultLog.warn("Queue: [{}], first item: [{}], number of retries: {}, expected number of retries <= {}", q.getName(), entry.getId(), status.getAttempts(), numberOfRetriesAllowed);
failures.put(q.getName(), status.getAttempts());
}
} else {
resultLog.debug("No items in queue [{}]", q.getName());
}
} catch (Exception e) {
resultLog.warn("Exception while inspecting distribution queue [{}]: {}", queueName, e);
}
}
}
} else {
resultLog.debug("No distribution queue providers found");
}
if (failures.size() > 0) {
// a specific log entry (using markdown) to provide a recommended user action
for (Map.Entry<String, Integer> entry : failures.entrySet()) {
resultLog.warn("Distribution queue {}'s first item in the default queue has been retried {} times (threshold: {})", entry.getKey(), entry.getValue(), numberOfRetriesAllowed);
}
}
return new Result(resultLog);
}
use of org.apache.sling.distribution.agent.DistributionAgent in project sling by apache.
the class DistributionAgentServlet method doPost.
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
DistributionRequest distributionRequest = RequestUtils.fromServletRequest(request);
log.debug("distribution request : {}", distributionRequest);
DistributionAgent agent = request.getResource().adaptTo(DistributionAgent.class);
ResourceResolver resourceResolver = request.getResourceResolver();
if (agent != null) {
try {
DistributionResponse distributionResponse = agent.execute(resourceResolver, distributionRequest);
ServletJsonUtils.writeJson(response, distributionResponse);
log.debug("distribution response : {}", distributionResponse);
} catch (Throwable e) {
log.error("an unexpected error has occurred", e);
ServletJsonUtils.writeJson(response, 503, "an unexpected error has occurred", null);
}
} else {
ServletJsonUtils.writeJson(response, 404, "agent not found", null);
}
}
use of org.apache.sling.distribution.agent.DistributionAgent in project sling by apache.
the class DistributionAgentQueueServlet method doPost.
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
DistributionQueue queue = request.getResource().adaptTo(DistributionQueue.class);
ResourceResolver resourceResolver = request.getResourceResolver();
if ("delete".equals(operation)) {
String limitParam = request.getParameter("limit");
String[] idParam = request.getParameterValues("id");
if (idParam != null) {
deleteItems(resourceResolver, queue, idParam);
} else {
int limit = 1;
try {
limit = Integer.parseInt(limitParam);
} catch (NumberFormatException ex) {
log.warn("limit param malformed : " + limitParam, ex);
}
deleteItems(resourceResolver, queue, limit);
}
} else if ("copy".equals(operation)) {
String from = request.getParameter("from");
String[] idParam = request.getParameterValues("id");
if (idParam != null && from != null) {
DistributionAgent agent = request.getResource().getParent().getParent().adaptTo(DistributionAgent.class);
DistributionQueue sourceQueue = getQueueOrThrow(agent, from);
addItems(resourceResolver, queue, sourceQueue, idParam);
}
} else if ("move".equals(operation)) {
String from = request.getParameter("from");
String[] idParam = request.getParameterValues("id");
if (idParam != null && from != null) {
DistributionAgent agent = request.getResource().getParent().getParent().adaptTo(DistributionAgent.class);
DistributionQueue sourceQueue = getQueueOrThrow(agent, from);
addItems(resourceResolver, queue, sourceQueue, idParam);
deleteItems(resourceResolver, sourceQueue, idParam);
}
}
}
use of org.apache.sling.distribution.agent.DistributionAgent in project sling by apache.
the class TriggerAgentRequestHandlerTest method testHandleActive.
@Test
public void testHandleActive() throws Exception {
DistributionAgent agent = mock(DistributionAgent.class);
SimpleDistributionAgentAuthenticationInfo authenticationInfo = mock(SimpleDistributionAgentAuthenticationInfo.class);
DefaultDistributionLog log = mock(DefaultDistributionLog.class);
TriggerAgentRequestHandler triggerAgentRequestHandler = new TriggerAgentRequestHandler(agent, authenticationInfo, log, true);
ResourceResolver resourceResolver = mock(ResourceResolver.class);
DistributionRequest request = mock(DistributionRequest.class);
triggerAgentRequestHandler.handle(resourceResolver, request);
}
Aggregations