Search in sources :

Example 1 with PublishJob

use of org.commonjava.maven.galley.spi.transport.PublishJob in project galley by Commonjava.

the class UploadHandler method joinOrStart.

private boolean joinOrStart(final ConcreteResource resource, final int timeoutSeconds, final InputStream stream, final long length, final String contentType, final Transport transport) throws TransferException {
    if (transport == null) {
        return false;
    }
    Future<PublishJob> future;
    synchronized (pending) {
        future = pending.get(resource);
        if (future == null) {
            final PublishJob job = transport.createPublishJob(resource, stream, length, timeoutSeconds);
            future = executor.submit(job);
            pending.put(resource, future);
        }
    }
    int waitSeconds = (int) (timeoutSeconds * config.getTimeoutOverextensionFactor());
    int tries = 1;
    try {
        while (tries > 0) {
            tries--;
            try {
                final PublishJob job = future.get(timeoutSeconds, TimeUnit.SECONDS);
                if (job.getError() != null) {
                    throw job.getError();
                }
                nfc.clearMissing(resource);
                return job.isSuccessful();
            } catch (final InterruptedException e) {
                throw new TransferException("Interrupted publish: {}. Reason: {}", e, resource, e.getMessage());
            } catch (final ExecutionException e) {
                throw new TransferException("Failed to publish: {}. Reason: {}", e, resource, e.getMessage());
            } catch (final TimeoutException e) {
                Long size = transferSizes.get(resource);
                if (tries > 0) {
                    continue;
                } else if (size != null && size > config.getThresholdWaitRetrySize()) {
                    logger.debug("Publishing a large file: {}. Retrying Future.get() up to {} times.", size, tries);
                    tries = (int) (size / config.getWaitRetryScalingIncrement());
                    continue;
                } else {
                    throw new TransferTimeoutException(resource, "Timed out waiting for execution of: {}", e, resource);
                }
            } catch (final TransferException e) {
                throw e;
            } catch (final Exception e) {
                throw new TransferException("Failed listing: {}. Reason: {}", e, resource, e.getMessage());
            }
        }
    } finally {
        transferSizes.remove(resource);
        //            logger.info( "Marking download complete: {}", url );
        pending.remove(resource);
    }
    return false;
}
Also used : TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) TransferException(org.commonjava.maven.galley.TransferException) ExecutionException(java.util.concurrent.ExecutionException) PublishJob(org.commonjava.maven.galley.spi.transport.PublishJob) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException) TransferLocationException(org.commonjava.maven.galley.TransferLocationException) TransferException(org.commonjava.maven.galley.TransferException) TimeoutException(java.util.concurrent.TimeoutException) TransferTimeoutException(org.commonjava.maven.galley.TransferTimeoutException)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 TransferException (org.commonjava.maven.galley.TransferException)1 TransferLocationException (org.commonjava.maven.galley.TransferLocationException)1 TransferTimeoutException (org.commonjava.maven.galley.TransferTimeoutException)1 PublishJob (org.commonjava.maven.galley.spi.transport.PublishJob)1