feat: using process lock time to avoid concurrences #457

This commit is contained in:
vnmedeiros 2021-03-16 15:06:14 -03:00
parent 7b6ed432ef
commit 250a471479
2 changed files with 8 additions and 10 deletions

View File

@ -279,17 +279,15 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process {
*/
protected function handle() {
$this->lock_process();
// while we are debugging performance
$newRequest = true;
$batch = $this->get_batch();
if ($newRequest) {
$this->write_log($batch->key, ['New Request']);
$newRequest = false;
if($this->process_lock_in_time != get_site_transient( $this->identifier . '_process_lock' )) {
$this->write_log($batch->key, ['New request has ignored']);
wp_die();
}
$this->write_log($batch->key, ['New Request']);
register_shutdown_function(function() use($batch) {
$error = error_get_last();

View File

@ -242,10 +242,10 @@
$this->debug('locking process: ' . $this->identifier);
$this->start_time = time(); // Set start time of current process.
$max_execution_time = ini_get('max_execution_time');
$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : ( empty($max_execution_time) ? 60 : $max_execution_time ); // 1 minute
$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : ( empty($max_execution_time) ? 60 : ($max_execution_time * 1.5) ); // 1 minute
$lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );
set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
$this->process_lock_in_time = microtime();
set_site_transient( $this->identifier . '_process_lock', $this->process_lock_in_time, $lock_duration );
}
/**