Arun,
Thanks!
SDK version 3.0.10. Curiously, newer SDK versions fail badly in other ways on Ubuntu 20.04 interacting with Couchbase CE 7.0 Beta. I’ve had to stick at 3.0.10, but that’s a discussion for another time. Here’s an excerpt from the code that interacts with Couchbase (separate from the code processing incoming web requests).
Oh, I should mention that the connection to Couchbase is persistent and is a synchronous connection presently for a couple reasons. The first being I could not get async to work (SDK v3.0.10 and various v3.1.x), even following the documentation and attempting a couple permutations based on stuff I found on the web. The second being that I don’t think it really needs to be async in the end because the app itself is being served up via uvicorn async, and all of the enpoints are implemented using fastapi async. So, I don’t think forcing the Couchbase connection to be async really changes anything – comments welcome!
…
def connect(self):
self.cluster = Cluster(self.host, ClusterOptions(PasswordAuthenticator(self.user, self.passwd)))
self.bucket = self.cluster.bucket(self.bucket_name)
self.collection = self.bucket.scope(self.scope_name).collection(self.collection_name)
…
…
def query(self, sql, params=, autocommit=False):
…
try:
result = self.cluster.query(sql, QueryOptions(positional_parameters=params))
…
…
def add_workflow_instance_to_cache(self, user_id, university_id, workflow_instance):
…
opts = InsertOptions(durability=ServerDurability(Durability.MAJORITY_AND_PERSIST_TO_ACTIVE.value))
result = self.collection.insert(str(document_id), workflow, opts)
…
…
def retrieve_all_workflows_for_uni(self, university_id):
rows = self.query(“SELECT *, meta().id AS _id FROM workflows._default.instances WHERE university = $1”,
[university_id])
…
The scenario is that the front end code hits an endpoint which, after validation the incoming request/payload calls ‘add_workflow_instance_to_cache()’. Upon successful response from Couchbase, an HTTP OK is returned to the front end. The front end then makes a call to ‘retrieve_all_workflows_for_uni()’ to get the list of workflow instances.
In theory, the one that was just added should be in the list. In actuality, it is not always present. For the time being, the front end devs have introduced a time delay before launching the GET request. Not a good solution. Basically, our hope/objective is that there is a way to ensure that the newly inserted document is present at the time of return from the POST request that precipitated the INSERTion of a new workflow document.