christianwach / civicrm-wp-member-sync

CiviCRM WordPress Member Sync plugin keeps a WordPress user in sync with a CiviCRM membership by granting either a role or capabilities to a WordPress user who has that membership.
https://wordpress.org/plugins/civicrm-wp-member-sync/
GNU General Public License v2.0
17 stars 10 forks source link

Sync job results in heavy database activity #41

Open wmortada opened 2 years ago

wmortada commented 2 years ago

We noticed some unusual database activity on a website that has this plugin installed. We believe that this was due to the civi_wp_member_sync_refresh job which syncs the status for every member. This code appears to loop through every user account and perform the sync. For this particular site there are more than 90,000 users so this is quite a resource intensive operation.

In this particular case we decided to simply turn this job off and rely on the sync occurring when the user logs in or an admin updates the CiviCRM contact record. However, we think this code could be optimised to work for sites with a large number of users. Potentially the job could be set to run in batches.

https://github.com/christianwach/civicrm-wp-member-sync/blob/b2efc83178c2d7d6178a0facd3ab9192632939e4/includes/civi-wp-ms-members.php#L351-L380

The above function also appears to overlap with the functionality of sync_all_civicrm_memberships. I'm wondering if they could be combined? Particularly as the latter has the ability to process contacts in batches.

https://github.com/christianwach/civicrm-wp-member-sync/blob/b2efc83178c2d7d6178a0facd3ab9192632939e4/includes/civi-wp-ms-members.php#L126

christianwach commented 2 years ago

@wmortada Thanks for the report - yes sync_all_wp_user_memberships() is really old code predating my fork of the original plugin. I'd recommend running the plugin without using the WordPress pseudo-cron whether or not there are lots of WordPress Users. It's generally not that useful since the changes are largely on the CiviCRM side and synced to WordPress.

I'll leave this issue open as a cookie trail for others - along with the warning that this functionality is likely to be (a) removed or (b) rewritten.

wmortada commented 2 years ago

Thanks @christianwach

wmortada commented 2 years ago

Just to check, I presume that when a membership expires in CiviCRM, this won't be synced to the WordPress user until they next log in?

christianwach commented 2 years ago

It should sync the moment the CiviCRM Membership status changes as a result of the cronjob. So when they log in, they will do so with their "expired" role or capabilities.

wmortada commented 2 years ago

Thanks for confirming. I wasn't sure if the cron job would trigger it.

christianwach commented 2 years ago

I've begun to address this in a79b23249b31ab10bb1876fdb7f3a6e2186df2d6 and will remove in future versions since it's not actually that useful.

wmortada commented 2 years ago

The website in questions is running with significantly less load on the database since we switched this job off.

christianwach commented 2 years ago

Good to hear @wmortada thanks for reporting back. The setting will be off by default for new installs. I have yet to decide how to handle existing ones.