I'm working on a webshop app where I manage an order class with states for Received and Finished. The business rules state that I should only update the order to Finished in the database if the notification to an external API is successful. Here's the catch: what if the API notification succeeds but the database update fails? In that case, on a retry from a background job, I'd end up re-notifying the API, which is not ideal. I was thinking about introducing a new state called Notifying to handle this, but I'm wondering if there's a simpler way to achieve this without adding complexity. Any suggestions?
4 Answers
You really don't need to complicate it by adding a substate. I think you can simply use a job queue that retries to update the database state until it succeeds. If the API notification was already successful, there's no reason to re-notify the service again.
I totally agree! Using a simple boolean flag can act like a substate without the need for an entire state machine overhaul. It just keeps everything cleaner.
Honestly, instead of overcomplicating your state machine, just use a boolean flag like notification_sent. It's a straightforward solution that keeps things manageable without the overhead of a substate.
What if the API call succeeds but you have a temporary issue updating the database? You'd still need to have a way to record that the notification was requested, and maybe have a background job that checks those records and verifies whether the API call went through.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically