Search in sources :

Example 1 with FileMigrationsApi

use of ch.cyberduck.core.brick.io.swagger.client.api.FileMigrationsApi in project cyberduck by iterate-ch.

the class BrickFileMigrationFeature method poll.

protected void poll(final BrickApiClient client, final FileActionEntity entity) throws BackgroundException {
    final CountDownLatch signal = new CountDownLatch(1);
    final ScheduledThreadPool scheduler = new ScheduledThreadPool();
    final long timeout = preferences.getLong("brick.migration.interrupt.ms");
    final long start = System.currentTimeMillis();
    final AtomicReference<BackgroundException> failure = new AtomicReference<>();
    try {
        scheduler.repeat(() -> {
            try {
                if (System.currentTimeMillis() - start > timeout) {
                    failure.set(new ConnectionCanceledException(String.format("Interrupt polling for migration key after %d", timeout)));
                    signal.countDown();
                    return;
                }
                // Poll status
                final FileMigrationEntity.StatusEnum migration = new FileMigrationsApi(client).getFileMigrationsId(entity.getFileMigrationId()).getStatus();
                switch(migration) {
                    case COMPLETE:
                        signal.countDown();
                        return;
                    default:
                        log.warn(String.format("Wait for copy to complete with current status %s", migration));
                        break;
                }
            } catch (ApiException e) {
                log.warn(String.format("Failure processing scheduled task. %s", e.getMessage()), e);
                failure.set(new BrickExceptionMappingService().map(e));
                signal.countDown();
            }
        }, preferences.getLong("brick.migration.interval.ms"), TimeUnit.MILLISECONDS);
        Uninterruptibles.awaitUninterruptibly(signal);
        if (null != failure.get()) {
            throw failure.get();
        }
    } finally {
        scheduler.shutdown();
    }
}
Also used : FileMigrationsApi(ch.cyberduck.core.brick.io.swagger.client.api.FileMigrationsApi) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) ScheduledThreadPool(ch.cyberduck.core.threading.ScheduledThreadPool) AtomicReference(java.util.concurrent.atomic.AtomicReference) FileMigrationEntity(ch.cyberduck.core.brick.io.swagger.client.model.FileMigrationEntity) CountDownLatch(java.util.concurrent.CountDownLatch) BackgroundException(ch.cyberduck.core.exception.BackgroundException) ApiException(ch.cyberduck.core.brick.io.swagger.client.ApiException)

Aggregations

ApiException (ch.cyberduck.core.brick.io.swagger.client.ApiException)1 FileMigrationsApi (ch.cyberduck.core.brick.io.swagger.client.api.FileMigrationsApi)1 FileMigrationEntity (ch.cyberduck.core.brick.io.swagger.client.model.FileMigrationEntity)1 BackgroundException (ch.cyberduck.core.exception.BackgroundException)1 ConnectionCanceledException (ch.cyberduck.core.exception.ConnectionCanceledException)1 ScheduledThreadPool (ch.cyberduck.core.threading.ScheduledThreadPool)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1