analogwp / analogwp-templates

Style Kits for Elementor adds a number of intuitive styling controls in the Elementor editor that allow you to apply styles globally or per page.
https://analogwp.com
27 stars 7 forks source link

Implement Async/Background Tasks for upgrades #382

Closed mauryaratan closed 2 years ago

mauryaratan commented 4 years ago

Upgrades are performed right when we update the plugin. For large sites, it can be troublesome if a user has 100s of posts and if upgrade cost is heavy, this may lead to page taking too much of time during updating.

By adding an async task to update, we can make the upgrades/migrations run in the background after the update.

gvgvgvijayan commented 4 years ago

@mauryaratan could you please share the steps to reproduce in the development system. Develop system not showing the update action under the plugin list page because the code is pulled and up to date with develop branch.

mauryaratan commented 4 years ago

@gvgvgvijayan There is nothing to reproduce here. See upgrade-functions.php to find out how upgrades are performed. They're basically executed during plugin update.

gvgvgvijayan commented 4 years ago

Explored the file upgrade-functions.php.

Changing The Hook:

The function do_automatic_upgrades is hooked to the action admin_init Link: https://github.com/analogwp/analogwp-templates/blob/develop/inc/upgrade-functions.php#L140

The action admin_init is called every 60 seconds as it is defined in wp-admin/admin-ajax.php which call a heartbeat API. Hook's Link: https://github.com/WordPress/WordPress/blob/5.4-branch/wp-admin/admin-ajax.php#L45 Heartbeat API Call Link: https://github.com/WordPress/WordPress/blob/5.4-branch/wp-admin/admin-ajax.php#L157

As you noted the execution is happening through admin-ajax.php so it's already executing as asynchronous HTTP call but iterating, again and again, every one minute the code in the function is executing more than a once I believe that this is not the intentional purpose of this file as per my understanding this is going to execute only on plugin update only.

So what I planned to do is instead of hooking the function do_automatic_upgrades to admin_init, hook this to the hook upgrader_post_install which will trigger after the plugin upgraded. Hook's Link: https://github.com/WordPress/WordPress/blob/5.4-branch/wp-admin/includes/class-wp-upgrader.php#L621 WP Core Upgrade Hook Call Link: https://github.com/WordPress/WordPress/blob/5.4-branch/wp-admin/includes/class-plugin-upgrader.php#L172

Implement Background Task

As explored the source of this file found that in the following functions:

  1. ang_v13_upgrades
  2. version_1_3_15_upgrades
  3. version_1_5_upgrades
  4. version_1_6_6_upgrades

The post's metadata is updating which is a time-consuming process as decoding JSON string, update the value and write to the non-indexed column meta_value which consumes more memory and raise in processing time. So planned to execute all these upgrade chunks of function in a single background task.

For background task implementation going to refer to this elementor class (which is similar to WooCommerce BG task implementation) BG Task Class Link: https://github.com/elementor/elementor/blob/v2.9.13/core/base/background-task.php#L20

@mauryaratan kindly share your thoughts

gvgvgvijayan commented 4 years ago

@mauryaratan Explored the queue process class it's processing the deferred tasks one by one as a first in first out way so I planned to pass the individual version function into separate tasks in the queue. I will test a dummy or mock function outside of the analogwp-template plugin the reason is whenever the upgrade starts the files are overridden by plugin updater once I satisfied with the implementation I will replicate the same to the upgrade-functions.php