question

miculkaj avatar image
miculkaj asked

Best practices - Cloud Script error handling and rollbacks

Hi,

I have one complex function in my CloudScript and after I started to deal with error handling I came to few questions regarding proper error handling solution.

I plan to use try/catch on every server API call and if error occurs, I simply return error statement and go out of function. The problem is that I have two main server calls in my function - GrantItemsToUsers (called just once) and AddUserVirtualCurrency (called X-times depending on the number of currencies added). My questions follows:

1) I presume that parameters to server calls are ok. In that case, what else can possibly go wrong during server call which consequently leads to error?

2) Let´s say that GrantItemsToUsers works ok and some of the items are granted to the player, but during the call of AddUserVirtualCurrency something goes wrong and the method fails. Then I need to rollback changes I made with item grants - is there some better technique than remembering granted items ID´s and calling RevokeInventoryItem on each of them?

3) Is it even necessary and common to implement these rollbacks? I presume that my code works fine and then it should fail only if there are some general server problems. But in that case I presume that other server calls will not work too, so no changes will be made to DB and/or I won´t be able to revoke items with API call, so is there a point to have some rollback code? Generally, writing rollbacks means a lot more code than I have right now (for example, granted items are bundles, so I need to get bundled items ID´s, also if some of the currency add fails I need to rollback all previously added currencies within this call... and these "service functions" will grow with more and more code) and that leads me to question if I am on the good way and if there are some best practices with error handling dealing.

Thanks in advance for your answers!

CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image
brendan answered

First, we'd recommend moving this logic to the economy wherever possible. You can create Bundles and Containers for complex sets of items. However, assuming you're doing something even more complex, like crafting/alchemy system (we'll be adding one to the service later, but for now this would indeed require server-side logic), then try/catch is definitely a good start.

Assuming you mean "as long as the parameters are correct", things that could go wrong with those two calls would be along the lines of a general failure (an AWS server crash - very rare, but possible), hitting a limit (too many inventory items, trying to add more VC than can be handled in a 32 bit value), or a database error due to trying to perform simultaneous actions on the same data. Overwhelmingly, the situation would not be that other calls would fail, however. That would only be the case if there were a full outage, so rollback is certainly an option. Whether or not you do implement rollback is really up to you - apart from bugs in the call itself, the other error conditions should be rare. But that said, it's still important to ask the question of what the state would be for the player account if the script failed at each point in its logic, so that you can decide how important rollback is for the handler in question.

Now, it's also important to bear in mind that scripts (especially those triggered by a PlayStream Rule or Segment) are limited in execution time, so trying to perform a lot of Server calls isn't really viable. It's not possible to run all of PlayFab on a single server - each call to the Server API is a Web API call from the Cloud Script logic server. It's in the same datacenter, so latency is minimal, but it's not zero. For very complex operations, you should really consider having a custom game server that manages that logic.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hamza Lazaar avatar image
Hamza Lazaar answered

@miculkaj thank you for the wonderful interrogations.

you have no guarantee rollbacks will succeed, you would need to retry them in case of failures, how many times and for how long?

I'm like you learning how to not think about the less probable worst until it happens.

Otherwise, pessimist perfectionist defensive programming is a never ending infinite loop.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.