Search in sources :

Example 1 with DistributionQueueEntry

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

the class SimpleDistributionQueue method remove.

@CheckForNull
public DistributionQueueEntry remove(@Nonnull String id) {
    DistributionQueueEntry toRemove = getItem(id);
    boolean removed = false;
    if (toRemove != null) {
        removed = queue.remove(toRemove.getItem());
    }
    log.debug("item with id {} removed from the queue: {}", id, removed);
    if (removed) {
        return toRemove;
    } else {
        return null;
    }
}
Also used : DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) CheckForNull(javax.annotation.CheckForNull)

Example 2 with DistributionQueueEntry

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

the class SimpleDistributionQueueCheckpoint method run.

@Override
public void run() {
    String fileName = queue.getName() + "-checkpoint";
    File checkpointFile = new File(checkpointDirectory, fileName + "-new");
    log.debug("started checkpointing");
    try {
        if (checkpointFile.exists()) {
            assert checkpointFile.delete();
        }
        assert checkpointFile.createNewFile();
        Collection<String> lines = new LinkedList<String>();
        FileOutputStream fileOutputStream = new FileOutputStream(checkpointFile);
        for (DistributionQueueEntry queueEntry : queue.getItems(0, -1)) {
            DistributionQueueItem item = queueEntry.getItem();
            String packageId = item.getPackageId();
            StringWriter w = new StringWriter();
            JsonGenerator jsonWriter = Json.createGenerator(w);
            jsonWriter.writeStartObject();
            for (Map.Entry<String, Object> entry : item.entrySet()) {
                Object value = entry.getValue();
                boolean isArray = value instanceof String[];
                if (isArray) {
                    jsonWriter.writeStartArray(entry.getKey());
                    for (String s : ((String[]) value)) {
                        jsonWriter.write(s);
                    }
                    jsonWriter.writeEnd();
                } else {
                    jsonWriter.write(entry.getKey(), (String) value);
                }
            }
            jsonWriter.writeEnd();
            jsonWriter.close();
            lines.add(packageId + " " + w.toString());
        }
        log.debug("parsed {} items", lines.size());
        IOUtils.writeLines(lines, Charset.defaultCharset().name(), fileOutputStream, Charset.defaultCharset());
        fileOutputStream.flush();
        fileOutputStream.close();
        boolean success = checkpointFile.renameTo(new File(checkpointDirectory, fileName));
        log.debug("checkpoint succeeded: {}", success);
    } catch (Exception e) {
        log.error("failed checkpointing for queue {}", queue.getName());
    }
}
Also used : DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) LinkedList(java.util.LinkedList) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) StringWriter(java.io.StringWriter) FileOutputStream(java.io.FileOutputStream) JsonGenerator(javax.json.stream.JsonGenerator) File(java.io.File) Map(java.util.Map)

Example 3 with DistributionQueueEntry

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

the class SimpleDistributionQueueTest method testPackageAdditionAndRemoval.

@Test
public void testPackageAdditionAndRemoval() throws Exception {
    DistributionQueue queue = new SimpleDistributionQueue("agentName", "default");
    DistributionQueueItem pkg = mock(DistributionQueueItem.class);
    when(pkg.getPackageId()).thenReturn("id");
    assertNotNull(queue.add(pkg));
    assertFalse(queue.getStatus().isEmpty());
    assertNotNull(queue.remove(pkg.getPackageId()));
    assertTrue(queue.getStatus().isEmpty());
    DistributionQueueEntry entry = queue.getItem(pkg.getPackageId());
    assertNull(entry);
}
Also used : DistributionQueue(org.apache.sling.distribution.queue.DistributionQueue) DistributionQueueEntry(org.apache.sling.distribution.queue.DistributionQueueEntry) DistributionQueueItem(org.apache.sling.distribution.queue.DistributionQueueItem) Test(org.junit.Test)

Example 4 with DistributionQueueEntry

use of org.apache.sling.distribution.queue.DistributionQueueEntry 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 5 with DistributionQueueEntry

use of org.apache.sling.distribution.queue.DistributionQueueEntry 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)

Aggregations

DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)30 DistributionQueueItem (org.apache.sling.distribution.queue.DistributionQueueItem)20 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)18 DistributionQueueItemStatus (org.apache.sling.distribution.queue.DistributionQueueItemStatus)16 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)6 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)6 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 DistributionQueueProvider (org.apache.sling.distribution.queue.DistributionQueueProvider)5 File (java.io.File)4 DistributionException (org.apache.sling.distribution.common.DistributionException)4 Map (java.util.Map)3 CheckForNull (javax.annotation.CheckForNull)3 DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)3 DistributionPackageInfo (org.apache.sling.distribution.packaging.DistributionPackageInfo)3 SharedDistributionPackage (org.apache.sling.distribution.packaging.impl.SharedDistributionPackage)3 DistributionQueueStatus (org.apache.sling.distribution.queue.DistributionQueueStatus)3 Result (org.apache.sling.hc.api.Result)3 DistributionPackageBuilder (org.apache.sling.distribution.packaging.DistributionPackageBuilder)2