Background Jobs

The plugin keeps each site’s user records up to date with a user sync: every user gets a last activity date, and every user with a role on the site gets a BuddyPress blog record and a profile name. Since 1.1.3 the sync is scheduled through LH_Buddypress_Multi_Network_Queue (includes/class-lh-buddypress-multi-network-queue.php). It runs on Action Scheduler when the current site has it, in the lh-buddypress-multi-network group (listed under Tools → Scheduled Actions), and on WP-Cron otherwise. The same hooks are used on both runners, and on Action Scheduler any WP-Cron copy of a job is cleared so it runs once.

How a sync runs

Before 1.1.4 the whole sync ran in one request, which on a large site could outlive the host’s time limit and be killed. Now start_sync() records a run in the lh_bmn_sync_state option (run id, phase, cursor) and queues the first batch, and run_batch() works through two phases, walking user IDs upwards from the stored cursor: first the activity phase, which gives every user a last activity row, then the members phase, which records the site’s users in its blogs table and fills in missing names. Progress is saved after each chunk, and every step is safe to repeat, so a batch killed part-way redoes at most one chunk. The option is deleted when the run finishes.

Starting a sync while one is running starts a new run from the beginning; the batch already running notices at its next save and stops. This is cheap, because users already handled drop out of both phases. The hourly schedule check also queues a batch for any run that has made no progress for 15 minutes, so a run whose request was killed carries on.

A user added to a site is handled while WordPress is switched to that site, so that sync goes to the site’s WP-Cron: Action Scheduler is only safe for the site the request is running on. On deactivation the whole Action Scheduler group is cancelled on the current site, every WP-Cron instance of the plugin’s hooks is removed (including the retired lh_bmn_process) and any run in progress is cleared; on network deactivation WP-Cron is also cleared on every other site.

Filters