Mutiny async update - asynchronous

I am trying to update a document asynchronously without need to worry:
persistenceManager.updateElement(datastore, filterParams, fieldsToUpdate)
throw new MyException(ErrorCodes.GONE, errMsg);
updateElement returns a Uni<Integer>. Throwing Exception triggers an aspect (interceptor) to finalize metrics and to log errors in a standard way.
updateElement never updates the element. I tried to await() but Vertx complains the call is blocking. I tried onItem().failWith() no luck it does not interrupt the normal flow. I tried subscribe(), onItem().invoke(x->log...).
How can I be sure at least the update is sent to database before throwing exception and end the call?
It seems the call ends always before the update is even tried.
Thanks

If you want to throw an exception asynchronously, you need to create a failed item:
return persistenceManager
.updateElement(datastore, filterParams, fieldsToUpdate)
.chain( () -> Uni.createFrom().failure(new MyException(ErrorCodes.GONE, errMsg)) );
Or, throw the exception in a following uni:
return persistenceManager
.updateElement(datastore, filterParams, fieldsToUpdate)
.invoke( () -> throw new MyException(ErrorCodes.GONE, errMsg) );
Or, you can use .failWith:
return persistenceManager
.updateElement(datastore, filterParams, fieldsToUpdate)
.failWith( () -> throw new MyException(ErrorCodes.GONE, errMsg) );
Note that in all these functions, I've used the form () -> .... But if you need the item returned by updateElement, this will also work:
item -> ....

Related

Function inside Task.Run doesn't get executed

I have some slow part of my code that does a http request then waits for the answer and calls an adf pipeline so it can update a database parameter at the end. Since this took 2 minutes and my client app seemed so slow for my clients, I wrapped this call inside Task.Run() and set my logic inside. This works good but the last part, update a database parameter, doesn't get executed at all.
Task.Run(async () =>
{
await client.ExecuteAsync(request, CancellationToken.None);
await RunADFPipeline(pipeline, _config);
await _projectRepository.UpdateValue(id, "status", "unlock");
});
I found this example online and I applied it in my code. The first two awaits seem to execute correctly but the third one doesn't execute at all. If I set it outside of Task.Run(), it executes. The third part literally just calls a stored procedure
return await _context.Database.ExecuteSqlRawAsync("EXEC core.UpdateValue #ProjectId, #Name, #Value", parameters);
I also tried by adding ContinueWith() at the end of the Task.Run() but still no luck.
Task.Run(async () =>
{
await client.ExecuteAsync(request, CancellationToken.None);
await _adfHelper.RunADFPipeline(pipeline, _config);
}).ContinueWith((result) => _projectRepository.UpdateValue(id, "status", "unlock"));
All this code is inside a controller. Any thoughts and ideas on how can I achieve my goal and update this parameter after Task.Run() finishes or the functions inside finish?

Kotlin Execution order and result issues

I'm trying to get all files from firebase's storage through listAll.
By the way..
storageReference.listAll().addOnSuccessListener { listResult ->
val image_task : FileDownloadTask
for (fileRef in listResult.items) {
fileRef.downloadUrl.addOnSuccessListener { Uri ->
image_list.add(Uri.toString())
println("size1 : " + image_list.size)
}
}
println("size2 : " + image_list.size)
}//addOnSuccessListener
enter image description here
Why is the execution order like this?
How do I solve it??
When you add a listener or callback to something, the code inside the listener will not be called until sometime later. Everything else in the current function will happen first.
You are adding listeners for each item using your for loop. No code in the listeners is running yet. Then your "size2" println call is made after the for loop. At some later time, all your listeners will fire.
If you want asynchronous code like this to be written sequentially, then you need to use coroutines. That's a huge topic, but your code would look something like this (but probably a little more involved than this if you want to properly handle errors). I'm using lifecycleScope from an Android Activity or Fragment for this example. If you're not on Android, you need to use some other CoroutineScope.
The calls to await() are an alternative to adding success and failure listeners. await() suspends the coroutine and then returns a result or throws an exception on failure.
lifecycleScope.launch {
val results = try {
storageReference.listAll().await()
} catch (e: Exception) {
println("Failed to get list: ${e.message}")
return#launch
}
val uris = try {
results.map { it.downloadUrl.await().toString() }
} catch (e: Exception) {
println("Failed to get at least one URI: ${e.message}")
return#launch
}
image_list.addAll(uris)
}
There is nothing wrong with the execution order here.
fileRef.downloadUrl.addOnSuccessListener { Uri ->
the downloadUrl is an asynchronous action which means it doesn't wait for the action to actually complete in order to move along with the code.
You receive the result with the success listener (at least in this case)
If you want to deal with it in a sequential way, look at coroutines.

Timeout function still executes the operation

I am trying to create a method where user can change it's firebase_authentication display name with a timeout function attached to it:
Future<void> changeDisplayName({String name}) async {
try {
await _auth.currentUser
.updateProfile(displayName: name)
.timeout(Duration(seconds: 10))
.then((value) => _createSnackbar(
'Update Successfull', 'Your new display name is $name'));
} on TimeoutException {
Navigator.pop();
_createSnackbar(
'Timeout Exception', 'Operation stopped.');
} catch (e) {
_createSnackbar('Error Occurred', '${e.toString()}');
}
}
My intention is when the timeout exception is thrown, stop the operation entirely. But even the timeout exception is thrown the profile update is still done after the timeout. How can I avoid doing the operation when timeout is called?
There is no way to cancel the updateProfile operation. Once you make the method call, you will have to wait for it to complete and call then or catch to determines its result.
If you're trying to handle the case where the user's is not on a network, you may want to detect that condition before calling the API. But personally I'd consider just signaling the actual condition to the user instead: "This is taking longer than expected, you might want to try again later."

UnhandledPromiseRejectionWarning when using Fluture `encaseP` on `fetch`

I have just started using Flutures, and I am trying to fetch some remote data for a visualization with d3.
I created a function which accepts a DOM selector (e.g. #my-chart) and a url (e.g. https://example.com/data.json).
If an error occurs when fetching the data, I have a unary function that shows an error message. If everything goes well, I have a unary function that draws the visualization. For the sake of simplicity, let's suppose that these functions are just console.error and console.log.
const fn = async (selector, url) => {
// convert fetch (which returns a Promise) into a function that
returns a Future
const fetchf = Future.encaseP(fetch);
fetchf(url)
.chain(res => Future.tryP(_ => res.json()))
.fork(console.error, console.log);
}
Apparently I am missing something when wrapping fetch in a Future, because I get this warning:
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
If I had to use async/await I would write something like this, which would not give me any warning.
const fn = async (selector, url) => {
let res;
try {
res = await fetch(url);
} catch (err) {
console.error(err);
return;
}
let data;
try {
data = res.json();
} catch (err) {
console.error(err);
return;
}
console.log(data);
};
It seems two things are going on here:
The data.json() function is not supposed to be wrapped inside tryP, because according to your second not broken example, it returns synchronously (there is no await). This would cause Fluture to raise a TypeError (because it expects to see a Promise, but gets a JSON value). Although, knowing the fetch API, data.json() typically does return a Promise, so it might also be your second example is broken, and something else is going on. Whatever it is, I suspect that an unexpected Error is being thrown somewhere. Do you see any other error messages in your console, besides the one you posted?
I did some testing, and it does seems to be true - when Fluture raises or catches a TypeError after a successful encaseP, it seems the original Promise manages to catch that error, and trigger the unhandled rejection. This appears to be a regression bug in Fluture, and I will be fixing it soon. In the meantime, if we get to the bottom of what's throwing your error, you will be able to continue without depending on said fix.
EDIT: I've opened a PR to fix the second issue: https://github.com/fluture-js/Fluture/pull/310
EDIT2: The fix has been released under version 10.3.1. Using that version should give you more insights in what's happening with issue 1.

Meteor collection.insert callback issues

According to the Meteor documentation....
collection.insert(doc, [callback])
callback Function
Optional. If present, called with an error object as the first argument and the _id as the second.
...then later down...
On the server, if you don't provide a callback, then insert blocks until the database acknowledges the write, or throws an exception if something went wrong. If you do provide a callback, insert returns immediately. Once the insert completes (or fails), the callback is called with error and result arguments, same as for methods.
Which is it, error and _id or error and result? I do have Meteor.methods that are firing their callbacks correctly with error, result available to the scope.
I just can't get the callback to work correctly on a collection.insert(doc, [callback])
Either way I can't get my callback to register anything?
function insertPost(args) {
this.unblock;
if(args) {
post_text = args.text.slice(0,140);
var ts = Date.now();
Posts.insert({
post: post_text,
created: ts
}, function(error, _id){
// or try function(error, result) and still get nothing
// console.log('result: ' + result);
console.log('error: ' + error);
console.log('_id: ' + _id); //this._id doesn't work either
});
}
return;
}
What am I doing wrong? I have been up since 2 am coding...6 pm my time zone...I am blurry, so I might (probably) be missing something quite obvious.
Cheers
Steeve
This was a bug, fixed in the next release. Now, if you provide a callback to insert, it will be called with error and result arguments, where result is the ID of the new document, or null if there's an error.
Since this is serverside code you can just do:
var id = Posts.insert({data}); // will block until insert is complete
and the id will be available.

Resources