[Answered ]-What is a proper way to implement a state engine with asynchronous events?

2👍

Suggested refinement of your second option:

  1. Create an intermediary object/database table called something like PendingBookings. Among its data is a foreign key to the main booking and a count of pending legs. I’m assuming here that the number of pending legs will be non-zero at the initial booking.
  2. Whenever a leg is created its save method also increments the corresponding pending leg count in the PendingBookings object.
  3. Whenever a leg is confirmed its save method also decrements the pending leg count.
  4. When the pending leg count reaches 0 its save method launches a transaction that updates the main booking to confirmed status and deletes the PendingBookings instance.

This means you don’t have to poll things repeatedly, and also gives you a single location to find information about any bookings that have started but not finished. You could then poll that much less frequently, perhaps once a day into an OLAP cube to get information about bookings proceeding through the system.

I would also argue that there’s no duplication here – you’re storing useful state information in one place and eliminating it when you no longer need it.

Leave a comment