Automattic / woocommerce-payments

Accept payments via credit card. Manage transactions within WordPress.
https://wordpress.org/plugins/woocommerce-payments/
Other
176 stars 69 forks source link

Audit and optimize ActionScheduler usages #5279

Open marcinbot opened 1 year ago

marcinbot commented 1 year ago

See p7bje6-4BB-p2

Using ActionScheduler is not free. Spawning unnecessary actions can affect the site's performance.

Here's a list of improvements from a quick search for schedule_job across the code:

The above list covers all the async actions from the time of writing, but there might be more in the future. In general:

cc @Automattic/gamma

htdat commented 1 year ago

Regarding wcpay_webhook_fetch_events action, I could provide some insights into it:

marcinbot commented 1 year ago

Regarding wcpay_webhook_fetch_events action, I could provide some insights into it:

@htdat I think these are all fair points with regard to this issue which is about optimizing the AS usage. I have updated the description to remove it.

However, one could still argue that the accounts endpoint is not a great place for such flags (and we have more of those than just the failed webhooks, so this is not a criticism of the webhook mechanism). The endpoint represents more static data that can be cached. Hooking into it, or reading dynamic flags is introducing some side effects that might grow difficult to keep track of.

There's also a context of how often the failed webhooks are re-fetched. The account data can be refreshed as rarely as once a day in some cases, and that might not be great for processing webhooks in a timely manner.

Still, both of these are separate issues, so I'm happy to remove that action from the list here :)

htdat commented 1 year ago

@marcinbot - I agree about those two points, and we can consider improving them :)

dmallory42 commented 10 months ago

Another relevant report was raised in #6662 around the usage of as_unschedule_action:

The WC_Payments_Action_Scheduler_Service::schedule_job() can get called dozens of times during the process of creating an order. Each time the callback is hit, calling as_unschedule_action() is adding quite a bit of overhead. This handling was added in #1295.

https://github.com/Automattic/woocommerce-payments/blob/db6e4446b9f246897c116c56fee90475fb2e29d0/includes/class-wc-payments-action-scheduler-service.php#L147-L153

Do we need to unschedule and reschedule the job, or can we simply call as_has_scheduled_action() instead?

as_has_scheduled_action() doesn't require that the query for the scheduled action be ordered by the date, so it doesn't have the same query issues with requiring a filesort that the normal query to get the next action does.

Mentioning it here just in case it gets lost between the two issues.