Change WC_Background_Updater to allow in-task resource monitoring

This change makes it possible to monitor resource usage inside each of the database update functions. Useful when doing resource intensive updates to stop the execution and requeue before the execution is aborted.
This commit is contained in:
Rodrigo Primo 2018-04-16 09:40:38 -03:00
parent 35998e56cb
commit c77b2f20f4
1 changed files with 20 additions and 3 deletions

View File

@ -99,18 +99,26 @@ class WC_Background_Updater extends WC_Background_Process {
wc_maybe_define_constant( 'WC_UPDATING', true );
$logger = wc_get_logger();
$result = null;
include_once dirname( __FILE__ ) . '/wc-update-functions.php';
if ( is_callable( $callback ) ) {
$logger->info( sprintf( 'Running %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
call_user_func( $callback );
$logger->info( sprintf( 'Finished %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
$result = call_user_func( $callback, $this );
if ( -1 === $result ) {
$message = sprintf( 'Requeuing %s callback.', $callback );
} else {
$message = sprintf( 'Finished %s callback.', $callback );
}
$logger->info( $message, array( 'source' => 'wc_db_updates' ) );
} else {
$logger->notice( sprintf( 'Could not find %s callback', $callback ), array( 'source' => 'wc_db_updates' ) );
}
return false;
return -1 === $result ? $callback : false;
}
/**
@ -125,4 +133,13 @@ class WC_Background_Updater extends WC_Background_Process {
WC_Install::update_db_version();
parent::complete();
}
/**
* See if the batch limit has been exceeded.
*
* @return bool
*/
public function is_batch_limit_exceeded() {
return $this->batch_limit_exceeded();
}
}