use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SubscriptionProcessor method startSubscriptions.
/**
* Start subscribing to model mutations.
*/
synchronized void startSubscriptions() throws DataStoreException {
int subscriptionCount = modelProvider.modelNames().size() * SubscriptionType.values().length;
// Create a latch with the number of subscriptions are requesting. Each of these will be
// counted down when each subscription's onStarted event is called.
AbortableCountDownLatch<DataStoreException> latch = new AbortableCountDownLatch<>(subscriptionCount);
// Need to create a new buffer so we can properly handle retries and stop/start scenarios.
// Re-using the same buffer has some unexpected results due to the replay aspect of the subject.
buffer = ReplaySubject.create();
Set<Observable<SubscriptionEvent<? extends Model>>> subscriptions = new HashSet<>();
for (ModelSchema modelSchema : modelProvider.modelSchemas().values()) {
for (SubscriptionType subscriptionType : SubscriptionType.values()) {
subscriptions.add(subscriptionObservable(appSync, subscriptionType, latch, modelSchema));
}
}
ongoingOperationsDisposable.add(Observable.merge(subscriptions).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).doOnSubscribe(disposable -> LOG.info("Starting processing subscription events.")).doOnError(failure -> LOG.warn("Reading subscription events has failed.", failure)).doOnComplete(() -> LOG.warn("Reading subscription events is completed.")).subscribe(buffer::onNext, buffer::onError, buffer::onComplete));
boolean subscriptionsStarted;
try {
LOG.debug("Waiting for subscriptions to start.");
subscriptionsStarted = latch.abortableAwait(adjustedTimeoutSeconds, TimeUnit.SECONDS);
} catch (InterruptedException exception) {
LOG.warn("Subscription operations were interrupted during setup.");
return;
}
if (subscriptionsStarted) {
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(DataStoreChannelEventName.SUBSCRIPTIONS_ESTABLISHED));
LOG.info(String.format(Locale.US, "Started subscription processor for models: %s of types %s.", modelProvider.modelNames(), Arrays.toString(SubscriptionType.values())));
} else {
throw new DataStoreException("Timed out waiting for subscription processor to start.", "Retry");
}
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class SyncTimeRegistry method lookupLastSyncTime.
Single<SyncTime> lookupLastSyncTime(@NonNull String modelClassName) {
return Single.create(emitter -> {
QueryPredicate hasMatchingModelClassName = QueryField.field("modelClassName").eq(modelClassName);
localStorageAdapter.query(LastSyncMetadata.class, Where.matches(hasMatchingModelClassName), results -> {
try {
LastSyncMetadata syncMetadata = extractSingleResult(modelClassName, results);
emitter.onSuccess(SyncTime.from(syncMetadata.getLastSyncTime()));
} catch (DataStoreException queryResultFailure) {
emitter.onError(queryResultFailure);
}
}, emitter::onError);
});
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class MutationProcessor method drainMutationOutbox.
private Completable drainMutationOutbox() {
PendingMutation<? extends Model> next;
do {
next = mutationOutbox.peek();
if (next == null) {
return Completable.complete();
}
boolean itemFailedToProcess = !processOutboxItem(next).blockingAwait(ITEM_PROCESSING_TIMEOUT_MS, TimeUnit.MILLISECONDS);
if (itemFailedToProcess) {
return Completable.error(new DataStoreException("Failed to process " + next, "Check your internet connection."));
}
} while (true);
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class Orchestrator method performSynchronized.
private Completable performSynchronized(Action action) {
boolean permitAvailable = startStopSemaphore.availablePermits() > 0;
LOG.debug("Attempting to acquire lock. Permits available = " + permitAvailable);
try {
if (!startStopSemaphore.tryAcquire(LOCAL_OP_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
return Completable.error(new DataStoreException("Timed out acquiring orchestrator lock.", "Retry your request."));
}
} catch (InterruptedException exception) {
return Completable.error(new DataStoreException("Interrupted while acquiring orchestrator lock.", "Retry your request."));
}
LOG.info("Orchestrator lock acquired.");
return Completable.fromAction(action).doFinally(() -> {
startStopSemaphore.release();
LOG.info("Orchestrator lock released.");
});
}
use of com.amplifyframework.datastore.DataStoreException in project amplify-android by aws-amplify.
the class PersistentMutationOutbox method markInFlight.
@NonNull
@Override
public Completable markInFlight(@NonNull TimeBasedUuid pendingMutationId) {
return Completable.create(emitter -> {
PendingMutation<? extends Model> mutation = mutationQueue.getMutationById(pendingMutationId);
if (mutation != null) {
inFlightMutations.add(mutation.getMutationId());
emitter.onComplete();
return;
}
emitter.onError(new DataStoreException("Outbox was asked to mark a mutation with ID = " + pendingMutationId + " as in-flight. " + "However, there was no mutation with that ID in the outbox, to begin with.", AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION));
});
}
Aggregations