I have a query making a few changes to the database (changing some collections on a couple of documents), then spawning a function.
The spawned function does not see the changes made in the spawning query.
If the spawned function spawn itself, then it does see the changes the second time it runs (and then do not spawn itself again).
My bet is that the task server picks up the spawned function straight away, right before the MVCC mechanism picks its own timestamp for the end of the transaction.
So I get two related questions:
1) how to make sure the spawned function waits for the end of the spawning transaction?
2) is it possible to spawn a function, but to be actually put on the task server queue only if the transaction succeeds (and if it fails, does not put it on the task server, resulting in a kind of rollback)?
The one solution I can think of now is to use a post-commit trigger, but that sounds a bit convoluted, and my hope is that there is simply an option to do that when spawning the function.
1) how to make sure the spawned function waits for the end of the spawning transaction?
You can't. As you correctly pointed out, the spawning transaction doesn't finish until after the spawned transaction is started. The only way to do this I can think of is by evaling your collection update code in a separate, isolated transaction before you spawn the next task.
2) is it possible to spawn a function, but to be actually put on the task server queue only if the transaction succeeds (and if it fails, does not put it on the task server, resulting in a kind of rollback)?
Not that I know of. Best practice is to make spawning the task the last thing you do so if there is a failure you catch it early on before the subsequent task is spawned.
A third option here is to look at some kind of external orchestration to run these queries. For instance, you could issue a query for step 1, return the URIs for it and then issue a query for step 2. I prefer this approach so you may more easily stop your code in the case that you generate too many tasks and slow down your cluster. Heavy task spawning makes me very nervous. This could also make it easier to track which step in your process failed.