Search in sources :

Example 11 with DistributionAgent

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());
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) 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) Matchers.anyString(org.mockito.Matchers.anyString) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Result(org.apache.sling.hc.api.Result) Test(org.junit.Test)

Example 12 with DistributionAgent

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);
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) DistributionQueueItemStatus(org.apache.sling.distribution.queue.DistributionQueueItemStatus) HashMap(java.util.HashMap) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Result(org.apache.sling.hc.api.Result) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with DistributionAgent

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);
    }
}
Also used : DistributionRequest(org.apache.sling.distribution.DistributionRequest) DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DistributionResponse(org.apache.sling.distribution.DistributionResponse)

Example 14 with DistributionAgent

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);
        }
    }
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) ResourceResolver(org.apache.sling.api.resource.ResourceResolver)

Example 15 with DistributionAgent

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);
}
Also used : DistributionAgent(org.apache.sling.distribution.agent.DistributionAgent) DistributionRequest(org.apache.sling.distribution.DistributionRequest) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) DefaultDistributionLog(org.apache.sling.distribution.log.impl.DefaultDistributionLog) Test(org.junit.Test)

Aggregations

DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)15 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)5 ArrayList (java.util.ArrayList)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 Result (org.apache.sling.hc.api.Result)4 DistributionRequest (org.apache.sling.distribution.DistributionRequest)3 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)3 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Map (java.util.Map)2 DistributionComponentKind (org.apache.sling.distribution.component.impl.DistributionComponentKind)2 DefaultDistributionLog (org.apache.sling.distribution.log.impl.DefaultDistributionLog)2 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)2 DistributionResponse (org.apache.sling.distribution.DistributionResponse)1 DistributionAgentState (org.apache.sling.distribution.agent.DistributionAgentState)1 DistributionLog (org.apache.sling.distribution.log.DistributionLog)1 SimplePathInfo (org.apache.sling.distribution.resources.impl.common.SimplePathInfo)1 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)1