Race condition between incoming webhook and existence of a page

I am trying to implement a payment method for Merx and I am having the following issue. There is a race condition between the time an incoming webhook arrives and Kirby saving a page. The webhook expects the page (a customer order in this case) to exist, and this is not always true. The service sending the webhook suggests placing the webhook payload in a queue, and retrying later. Any ideas on how to implement that?

There was a plugin for Kirby 3 and 4: Queue | Kirby CMS Plugins. Maybe it also works with Kirby 5 or can be made compatible easily, or you can at least use that as a basis for your own implementation.

But the general idea would be to have a way to store jobs somewhere (as job files, in a database etc.), have a script that executes those jobs and handles errors (retry, send notifications if needed) and a cron job that executes the script at a given interval.

1 Like

Before trying the plugin (which seems great for this situation), I tried a quick and dirty solution: I made the webhook handler sleep for 3 seconds, and also send a response status of 400 in the case the page does not yet exist (this causes the webhook to retry in an hour). I don’t know if using sleep() with a magic number is acceptable, but it seems to work.

As a “cheap” solution, I’d also suggested that.