Skip to content

Commit

Permalink
[cron] Improved the scheduling of the install sync cron job.
Browse files Browse the repository at this point in the history
  • Loading branch information
fajardoleo authored and swashata committed Nov 26, 2024
1 parent 3cbfe00 commit fb0ca9d
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions includes/class-freemius.php
Original file line number Diff line number Diff line change
Expand Up @@ -5451,7 +5451,7 @@ function _plugin_code_type_changed() {

if ( $this->is_registered() ) {
// Schedule code type changes event.
$this->schedule_install_sync();
$this->maybe_schedule_install_sync_cron();
}

/**
Expand Down Expand Up @@ -6514,6 +6514,33 @@ private function maybe_schedule_sync_cron() {
$this->schedule_sync_cron();
}

/**
* Instead of running blocking install sync event, execute non blocking scheduled cron job.
*
* @param int $except_blog_id Since 2.0.0 when running in a multisite network environment, the cron execution is consolidated. This param allows excluding specified blog ID from being the cron job executor.
*
* @author Leo Fajardo (@leorw)
* @since 2.9.1
*/
private function maybe_schedule_install_sync_cron( $except_blog_id = 0 ) {
if ( ! $this->is_user_in_admin() ) {
return;
}

if ( $this->is_clone() ) {
return;
}

if (
// The event has been properly scheduled, so no need to reschedule it.
is_numeric( $this->next_install_sync() )
) {
return;
}

$this->schedule_cron( 'install_sync', 'install_sync', 'single', WP_FS__SCRIPT_START_TIME, false, $except_blog_id );
}

/**
* @author Vova Feldman (@svovaf)
* @since 1.1.7.3
Expand Down Expand Up @@ -6611,22 +6638,6 @@ private function get_install_sync_cron_blog_id() {
return $this->get_cron_blog_id( 'install_sync' );
}

/**
* Instead of running blocking install sync event, execute non blocking scheduled wp-cron.
*
* @author Vova Feldman (@svovaf)
* @since 1.1.7.3
*
* @param int $except_blog_id Since 2.0.0 when running in a multisite network environment, the cron execution is consolidated. This param allows excluding excluded specified blog ID from being the cron executor.
*/
private function schedule_install_sync( $except_blog_id = 0 ) {
if ( $this->is_clone() ) {
return;
}

$this->schedule_cron( 'install_sync', 'install_sync', 'single', WP_FS__SCRIPT_START_TIME, false, $except_blog_id );
}

/**
* Unix timestamp for previous install sync cron execution or false if never executed.
*
Expand Down Expand Up @@ -7432,7 +7443,7 @@ function _activate_plugin_event_hook() {

// Schedule re-activation event and sync.
// $this->sync_install( array(), true );
$this->schedule_install_sync();
$this->maybe_schedule_install_sync_cron();

// If activating the premium module version, add an admin notice to congratulate for an upgrade completion.
if ( $is_premium_version_activation ) {
Expand Down Expand Up @@ -8623,7 +8634,7 @@ private function update_plugin_version_event() {
return;
}

$this->schedule_install_sync();
$this->maybe_schedule_install_sync_cron();
// $this->sync_install( array(), true );
}

Expand Down Expand Up @@ -15981,7 +15992,7 @@ private function update_multisite_data_after_site_deactivation( $context_blog_id
if ( $this->is_install_sync_scheduled() &&
$context_blog_id == $this->get_install_sync_cron_blog_id()
) {
$this->schedule_install_sync( $context_blog_id );
$this->maybe_schedule_install_sync_cron( $context_blog_id );
}
}

Expand Down

0 comments on commit fb0ca9d

Please sign in to comment.