Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore) release startStopSemaphore when start returns, not when API sync completes #1027

Merged
merged 3 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private AWSDataStorePlugin(
sqliteStorageAdapter,
AppSyncClient.via(api),
() -> pluginConfiguration,
() -> api.getPlugins().isEmpty() ? Orchestrator.Mode.LOCAL_ONLY : Orchestrator.Mode.SYNC_VIA_API
() -> api.getPlugins().isEmpty() ? Orchestrator.State.LOCAL_ONLY : Orchestrator.State.SYNC_VIA_API
);
this.userProvidedConfiguration = userProvidedConfiguration;
}
Expand Down Expand Up @@ -235,47 +235,39 @@ private Completable initializeStorageAdapter(Context context) {
));
}

private void waitForInitialization(@NonNull Action onComplete, @NonNull Consumer<DataStoreException> onError) {
Completable.create(emitter -> {
categoryInitializationsPending.await();
emitter.onComplete();
})
.timeout(LIFECYCLE_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.subscribe(
onComplete::call,
throwable -> onError.accept(new DataStoreException("Request failed because DataStore is not " +
"initialized.", throwable, "Retry your request."))
);
private Completable waitForInitialization() {
return Completable.fromAction(() -> categoryInitializationsPending.await())
.timeout(LIFECYCLE_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io())
.doOnError(error -> LOG.error("DataStore initialization timed out.", error));
}

/**
* {@inheritDoc}
*/
@Override
public void start(@NonNull Action onComplete, @NonNull Consumer<DataStoreException> onError) {
waitForInitialization(() -> {
try {
orchestrator.start();
} catch (DataStoreException exception) {
onError.accept(exception);
return;
}
onComplete.call();
}, onError);
waitForInitialization()
.andThen(orchestrator.start())
.subscribeOn(Schedulers.io())
.subscribe(
onComplete::call,
error -> onError.accept(new DataStoreException("Failed to start DataStore.", error, "Retry."))
);
}

/**
* {@inheritDoc}
*/
@Override
public void stop(@NonNull Action onComplete, @NonNull Consumer<DataStoreException> onError) {
waitForInitialization(() -> orchestrator.stop()
.subscribeOn(Schedulers.io())
.subscribe(
onComplete::call,
error -> onError.accept(new DataStoreException("Failed to stop DataStore.", error,
"Retry your request."))), onError);
waitForInitialization()
richardmcclellan marked this conversation as resolved.
Show resolved Hide resolved
.andThen(orchestrator.stop())
.subscribeOn(Schedulers.io())
.subscribe(
onComplete::call,
error -> onError.accept(new DataStoreException("Failed to stop DataStore.", error, "Retry."))
);
}

/**
Expand All @@ -296,19 +288,6 @@ public void clear(@NonNull Action onComplete, @NonNull Consumer<DataStoreExcepti
onError);
}

/**
* Terminate use of the plugin.
*/
synchronized void terminate() {
try {
orchestrator.stop()
.andThen(Completable.fromAction(sqliteStorageAdapter::terminate))
.blockingAwait(LIFECYCLE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (Throwable throwable) {
LOG.warn("An error occurred while terminating the DataStore plugin.", throwable);
}
}

/**
* {@inheritDoc}
*/
Expand Down
Loading