I have an afterCreate event handler in my business logic that is synchronous. It needs to be synchronous as I have several tasks that need to be completed before sending the response to the user. BUT, there are also several tasks in the afterCreate that I’d like to do asynchronously (for instance sending an email or push notification can sometimes take a while).
What is the appropriate way to accomplish this? I don’t think just creating a new Thread() is going work will it?
Are there any scenarios where an error could occur between when beforeCreate is called and the object is actually created in the data table? I ask this because if it is an extremely small possibility that an error can occur there, then I might move my synchronous tasks in the afterCreate over to the beforeCreate instead (I have it in afterCreate so I can ensure my updates are synched properly).
As to your second question, the possibility that beforeCreate handler proceeds, but the creation itself fails is not extremely small: for example, if some constraints fail, if data object count limit is exceeded, if permissions are denied (of this I’m not 100% sure though), and there’s always a small probability of a software bug.
What happens if I mess up and the thread never completes? Do you guys handle the runaway threads or do I risk slowly bringing everything down and exhausting the thread pool?
The tasks will be terminated anyway after 2 hours, if I remember correctly. So no, you shouldn’t be able to bring anything down with additional threads.